Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
radtriste committed Oct 8, 2021
1 parent abd2b7e commit d0f9c48
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 22 deletions.
29 changes: 13 additions & 16 deletions .ci/jenkins/Jenkinsfile.deploy-artifacts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@ pipeline {
// For parameters, check into ./dsl/jobs.groovy file
// }

environment {
// Static env is defined into ./dsl/jobs.groovy file

// TODO move to env var of job dsl
PIPELINES_COMMON_SCRIPT_PATH = '.ci/jenkins/scripts/helper.groovy'
}
// environment {
// Static env is defined into ./dsl/jobs.groovy file
// }

stages {
stage('Initialize') {
Expand All @@ -42,6 +39,10 @@ pipeline {
pipelinesCommon = load "${WORKSPACE}/${PIPELINES_COMMON_SCRIPT_PATH}"
pipelinesCommon.init()

if (env.PIPELINE_CHECKS_SCRIPT_PATH) {
pipelinesCommon.loadAndExecuteScript(env.PIPELINE_CHECKS_SCRIPT_PATH)
}

pipelinesCommon.checkoutRepo()
}
}
Expand All @@ -52,17 +53,13 @@ pipeline {
// TODO Build before and then deploy ?
// That would allow to add some extra script commands if needed

dir(getRepoName()) {
getCommands(env.PRE_DEPLOY_COMMANDS).each {
sh "${it}"
}
}
deployArtifacts(getMavenCommand())
dir(getRepoName()) {
getCommands(env.POST_DEPLOY_COMMANDS).each {
sh "${it}"
}
pipelinesCommon.loadAndExecuteScript(env.PRE_DEPLOY_SCRIPT_PATH)
if (env.DEPLOY_SCRIPT_PATH) {
pipelinesCommon.loadAndExecuteScript(env.DEPLOY_SCRIPT_PATH)
} else {
deployArtifacts(getMavenCommand())
}
pipelinesCommon.loadAndExecuteScript(env.POST_DEPLOY_SCRIPT_PATH)
}
}
}
Expand Down
121 changes: 121 additions & 0 deletions .ci/jenkins/Jenkinsfile.update-version
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import org.jenkinsci.plugins.workflow.libs.Library
@Library('jenkins-pipeline-shared-libraries')_

import org.kie.jenkins.MavenCommand

pipeline {
agent {
label 'kie-rhel7'
}

tools {
maven 'kie-maven-3.6.3'
jdk 'kie-jdk11'
}

options {
timestamps()
timeout(time: 60, unit: 'MINUTES')
}

// parameters {
// For parameters, check into ./dsl/jobs.groovy file
// }

environment {
// Static env is defined into ./dsl/jobs.groovy file

// Keep here for visitibility
MAVEN_OPTS = '-Xms1024m -Xmx4g'

// TODO move to env var of job dsl
PIPELINES_REPO_NAME = 'kogito-pipelines'
PIPELINES_AUTHOR = 'radtriste'
PIPELINES_BRANCH = 'split_nightly_jobs'
PIPELINES_COMMON_SCRIPT_PATH = '.ci/jenkins/scripts/helper.groovy'
}

stages {
stage('Initialize') {
steps {
script {
assert env.UPDATE_VERSION_SCRIPT_PATH

cleanWs()

checkout scm
pipelinesCommon = load "${WORKSPACE}/${PIPELINES_COMMON_SCRIPT_PATH}"
pipelinesCommon.init()

if (env.PIPELINE_CHECKS_SCRIPT_PATH) {
pipelinesCommon.loadAndExecuteScript(env.PIPELINE_CHECKS_SCRIPT_PATH)
}

pipelinesCommon.checkoutRepo()
}
}
}
stage('Prepare for PR') {
when {
expression { return pipelinesCommon.isCreatePr() }
}
steps {
script {
pipelinesCommon.prepareForPR()
}
}
}
stage('Update version') {
steps {
script {
pipelinesCommon.loadAndExecuteScript(env.UPDATE_VERSION_SCRIPT_PATH)
}
}
}
stage('Create PR') {
when {
expression { return pipelinesCommon.isCreatePR() }
}
steps {
script {
msg = pipelinesCommon.getCommitMessageForVersionUpdate()
pipelinesCommon.commitChanges(msg)
pipelinesCommon.createPRForVersionUpdate(msg)
}
}
}
stage('Merge PR') {
when {
expression { return pipelinesCommon.isPRMergedAutomatically() }
}
steps {
script {
pipelinesCommon.checkoutRepo()
pipelinesCommon.mergeAndPushPR()
}
}
}
stage('Tag repository') {
when {
expression { return pipelinesCommon.getGitTag() }
}
steps {
script {
pipelinesCommon.tagRepository()
}
}
}
}
post {
always {
script {
pipelinesCommon.archivePipelineProperties()
}
}
cleanup {
script {
util.cleanNode('docker')
}
}
}
}
86 changes: 81 additions & 5 deletions .ci/jenkins/scripts/helper.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import org.kie.jenkins.MavenCommand
import org.kie.jenkins.MavenStagingHelper

prBranchHash = util.generateHash(10)
mavenDeployLocalDir = "${WORKSPACE}/maven_deploy_dir"
Expand All @@ -25,6 +24,22 @@ void sendNotification(String body = '', String jobId = getJobNameId()) {
}
}

void loadAndExecuteScript(String scriptPath, String directory = getRepoName()) {
if (scriptPath) {
dir(directory) {
echo "Execute script ${scriptPath} in directory ${directory}"
if (scriptPath.endsWith('.sh')) {
sh "${scriptPath}"
} else if (scriptPath.endsWith('.groovy')) {
def preDeployScript = load "${scriptPath}"
preDeployScript.execute(this)
} else {
error "Unsupported script to be executed ${scriptPath}"
}
}
}
}

////////////////////////////////////////////////////////////////////////////
// Maven

Expand Down Expand Up @@ -53,6 +68,7 @@ void checkoutRepo(String repoName = getRepoName()) {
dir(repoName) {
deleteDir()
checkout(githubscm.resolveRepository(repoName, getGitAuthor(), getGitBranch(), false))
sh "git checkout ${getGitBranch()}"
}
}

Expand Down Expand Up @@ -85,7 +101,7 @@ void commitChanges(String commitMsg, boolean failOnNoChanges = false, Closure st
}
}

def createPR(String prMsg, boolean shouldBeMergedAutomatically = true, String repoName = getRepoName()) {
def createPR(String prMsg, String repoName = getRepoName()) {
if (!prepareForPRRepos.containsKey(repoName)) {
error "Repository ${repoName} has not been prepared for PR"
}
Expand All @@ -109,11 +125,28 @@ def createPR(String prMsg, boolean shouldBeMergedAutomatically = true, String re
setPipelinePropertyIfNeeded("${repoName}.pr.target.uri", "https://github.com/${getGitAuthor()}/${repoName}")
setPipelinePropertyIfNeeded("${repoName}.pr.target.ref", getGitBranch())

if(shouldBeMergedAutomatically) {
// TODO
return prLink
}

void mergeAndPushPR(String repoName = getRepoName()) {
String prLink = getPrLink(repoName)
if (prLink) {
dir(repoName) {
githubscm.mergePR(prLink, getGitAuthorCredsId())
githubscm.pushObject('origin', getGitBranch(), getGitAuthorCredsID())
}
} else {
echo "No PR found for repository ${repoName}"
}
}

return prLink
void tagRepository(String repoName = getRepoName()) {
// TODO check for already existing repository ?
if (getGitTag()) {
dir(repoName) {
githubscm.tagLocalAndRemoteRepository('origin', getGitTag(), getGitAuthorCredsID(), env.BUILD_TAG, true)
}
}
}

////////////////////////////////////////////////////////////////////////////
Expand All @@ -133,6 +166,41 @@ void archivePipelineProperties() {
archiveArtifacts(artifacts: 'pipeline.properties')
}

void readPipelineProperties() {
String propsUrl = params.DEPLOY_BUILD_URL
if (propsUrl != '') {
if (!propsUrl.endsWith('/')) {
propsUrl += '/'
}
sh "wget ${propsUrl}artifact/${PROPERTIES_FILE_NAME} -O ${PROPERTIES_FILE_NAME}"
pipelineProperties = readProperties file: PROPERTIES_FILE_NAME
// echo all properties
echo pipelineProperties.collect { entry -> "${entry.key}=${entry.value}" }.join('\n')
}
}

boolean hasPipelineProperty(String key) {
return pipelineProperties[key] != null
}

String getPipelineProperty(String key) {
if (hasPipelineProperty(key)) {
return pipelineProperties[key]
}
return ''
}

String getParamOrPipelineProperty(String paramKey, String deployPropertyKey) {
if (params[paramKey] != '') {
return params[paramKey]
}
return getPipelineProperty(deployPropertyKey)
}

String getPrLink(String repoName = getRepoName()) {
return getParamOrPipelineProperty("${repoName}.pr.link")
}

///////////////////////////////////////////////////////////////////////////
// Getters

Expand All @@ -148,6 +216,10 @@ boolean isCreatePR() {
return "${CREATE_PR}".toBoolean()
}

boolean isPRMergedAutomatically() {
return "${MERGE_PR_AUTOMATICALLY}".toBoolean()
}

String getRepoName() {
return "${REPO_NAME}"
}
Expand Down Expand Up @@ -185,4 +257,8 @@ String getPRBranch() {
return params.PR_BRANCH ?: "${getGitBranch()}-${prBranchHash}"
}

String getGitTag() {
return params.GIT_TAG
}

return this
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,53 @@ class KogitoJobUtils {
env('NEXUS_BUILD_PROMOTION_PROFILE_ID', Utils.getBindingValue(script, 'MAVEN_NEXUS_BUILD_PROMOTION_PROFILE_ID'))
}
}

env('PIPELINES_COMMON_SCRIPT_PATH', Utils.getPipelinesJenkinsHelperScript(script))
jobConfig.scripts?.each { name, value ->
env("${name}_SCRIPT_PATH".toUpperCase(), value)
}
}
}
}

static def createUpdateVersionJob(def script, String repository, String jobFolder, KogitoJobType jobType, Map jobConfig) {
def jobParams = getBasicJobParams(script,
KogitoConstants.KOGITO_PIPELINES_REPOSITORY,
"${repository}.update-version",
jobFolder,
Utils.getPipelinesJenkinsfilePath(script, 'Jenkinsfile.update-version'),
"Update version for ${repository}")
// Setup correct checkout branch for pipelines
jobParams.git.branch = VersionUtils.getProjectTargetBranch(KogitoConstants.KOGITO_PIPELINES_REPOSITORY, jobParams.git.branch, repository)

return KogitoJobTemplate.createPipelineJob(script, jobParams).with {
parameters {
stringParam('DISPLAY_NAME', '', 'Setup a specific build display name')

stringParam('GIT_BRANCH', Utils.getGitBranch(script), 'Set the Git branch to checkout')
stringParam('GIT_AUTHOR', Utils.getGitAuthor(script), 'Set the Git author to checkout')

jobConfig.versions?.each { vs ->
stringParam("${vs}_VERSION".toUpperCase(), '', "Set the ${vs} Version")
}

booleanParam('CREATE_PR', false, 'Should we create a PR with the changes ?')
booleanParam('MERGE_PR_AUTOMATICALLY', false, 'Should the created PR be merged automatically ?')
stringParam('GIT_TAG', '', 'If you also need to tag the repository. Empty will not do anything.')
}

environmentVariables {
env('REPO_NAME', "${repository}")

env('RELEASE', jobType == KogitoJobType.RELEASE)

env('MAVEN_SETTINGS_CONFIG_FILE_ID', Utils.getBindingValue(script, 'MAVEN_SETTINGS_FILE_ID'))
env('MAVEN_DEPENDENCIES_REPOSITORY', Utils.getBindingValue(script, 'MAVEN_ARTIFACTS_REPOSITORY'))

env('PIPELINES_COMMON_SCRIPT_PATH', Utils.getPipelinesJenkinsHelperScript(script))
jobConfig.scripts?.each { name, value ->
env("${name}_SCRIPT_PATH".toUpperCase(), value)
}
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion dsl/seed/src/main/groovy/org/kie/jenkins/jobdsl/Utils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,15 @@ class Utils {
}

static String getPipelinesJenkinsfilePath(def script, String jenkinsfileName) {
return "${getJenkinsConfigPath(script, KogitoConstants.KOGITO_PIPELINES_REPOSITORY)}/${jenkinsfileName}"
return "${getPipelinesJenkinsConfigPath(script)}/${jenkinsfileName}"
}

static String getPipelinesJenkinsConfigPath(def script) {
return getJenkinsConfigPath(script, KogitoConstants.KOGITO_PIPELINES_REPOSITORY)
}

static String getPipelinesJenkinsHelperScript(def script) {
return "${getPipelinesJenkinsConfigPath(script)}/scripts/helper.groovy"
}

static String getJenkinsEmailCredsId(def script) {
Expand Down

0 comments on commit d0f9c48

Please sign in to comment.