| import org.jenkinsci.plugins.workflow.libs.Library |
| |
| @Library('jenkins-pipeline-shared-libraries')_ |
| |
| // Deploy jobs |
| RUNTIMES_DEPLOY = 'kogito-runtimes.build-and-deploy' |
| APPS_DEPLOY = 'kogito-apps.build-and-deploy' |
| EXAMPLES_DEPLOY = 'kogito-examples.build-and-deploy' |
| QUARKUS_PLATFORM_DEPLOY = 'quarkus-platform.deploy' |
| |
| // Map of executed jobs |
| // See https://javadoc.jenkins.io/plugin/workflow-support/org/jenkinsci/plugins/workflow/support/steps/build/RunWrapper.html |
| // for more options on built job entity |
| JOBS = [:] |
| |
| FAILED_STAGES = [:] |
| UNSTABLE_STAGES = [:] |
| |
| // Should be multibranch pipeline |
| pipeline { |
| agent { |
| label util.avoidFaultyNodes('ubuntu') |
| } |
| |
| options { |
| timeout(time: 720, unit: 'MINUTES') |
| } |
| |
| // parameters { |
| // For parameters, check into ./dsl/jobs.groovy file |
| // } |
| |
| environment { |
| // Some generated env is also defined into ./dsl/jobs.groovy file |
| KOGITO_CI_EMAIL_TO = credentials("${JENKINS_EMAIL_CREDS_ID}") |
| |
| // Use branch name in nightly tag as we may have parallel main and release branch builds |
| NIGHTLY_TAG = """${getBuildBranch()}-${sh( |
| returnStdout: true, |
| script: 'date -u "+%Y-%m-%d"' |
| ).trim()}""" |
| } |
| |
| stages { |
| stage('Initialize') { |
| steps { |
| script { |
| echo "nightly tag is ${env.NIGHTLY_TAG}" |
| |
| currentBuild.displayName = env.NIGHTLY_TAG |
| } |
| } |
| } |
| |
| stage('Build & Deploy artifacts') { |
| parallel { |
| stage('Build & Deploy Kogito Runtimes') { |
| steps { |
| script { |
| def buildParams = getDefaultBuildParams() |
| addSkipTestsParam(buildParams) |
| addSkipIntegrationTestsParam(buildParams) |
| |
| // images and operator deploy testing will use older working artifacts if that one fails |
| buildJob(RUNTIMES_DEPLOY, buildParams) |
| } |
| } |
| post { |
| failure { |
| addFailedStage(RUNTIMES_DEPLOY) |
| } |
| } |
| } |
| |
| stage('Build & Deploy Kogito Apps') { |
| steps { |
| script { |
| def buildParams = getDefaultBuildParams() |
| addSkipTestsParam(buildParams) |
| addSkipIntegrationTestsParam(buildParams) |
| |
| // images and operator deploy testing will use older working artifacts if that one fails |
| buildJob(APPS_DEPLOY, buildParams) |
| } |
| } |
| post { |
| failure { |
| addFailedStage(APPS_DEPLOY) |
| } |
| } |
| } |
| |
| stage('Deploy Quarkus platform') { |
| steps { |
| script { |
| def buildParams = getDefaultBuildParams() |
| buildJob(QUARKUS_PLATFORM_DEPLOY, buildParams) |
| } |
| } |
| post { |
| failure { |
| addFailedStage(QUARKUS_PLATFORM_DEPLOY) |
| } |
| } |
| } |
| |
| stage('Build & Deploy Kogito Examples') { |
| steps { |
| script { |
| def buildParams = getDefaultBuildParams() |
| addSkipTestsParam(buildParams) |
| addSkipIntegrationTestsParam(buildParams) |
| |
| // images and operator deploy testing will use older working artifacts if that one fails |
| buildJob(EXAMPLES_DEPLOY, buildParams) |
| |
| if (isJobSucceeded(EXAMPLES_DEPLOY) || isJobUnstable(EXAMPLES_DEPLOY)) { |
| // Update examples nightly branch |
| dir('kogito-examples') { |
| deleteDir() |
| checkout(githubscm.resolveRepository('incubator-kie-kogito-examples', getGitAuthor(), getBuildBranch(), false, getGitAuthorCredsId())) |
| String nightlyBranch = "nightly-${getBuildBranch()}" |
| githubscm.createBranch(nightlyBranch) |
| githubscm.pushObject('origin', nightlyBranch, getGitAuthorPushCredsId()) |
| } |
| } |
| } |
| } |
| post { |
| failure { |
| addFailedStage(EXAMPLES_DEPLOY) |
| } |
| } |
| } |
| } |
| } |
| |
| stage('Start Cloud nightly') { |
| when { |
| expression { !params.SKIP_CLOUD_NIGHTLY } |
| } |
| steps { |
| script { |
| def buildParams = getDefaultBuildParams() |
| addSkipTestsParam(buildParams) |
| build(job: './0-kogito-nightly-cloud', wait: false, parameters: buildParams, propagate: false) |
| } |
| } |
| } |
| } |
| post { |
| unsuccessful { |
| sendPipelineErrorNotification() |
| } |
| } |
| } |
| |
| def buildJob(String jobName, List buildParams, String jobKey = jobName) { |
| echo "[${jobKey}] Build ${jobName} with params ${buildParams}" |
| |
| def job = build(job: "${jobName}", wait: true, parameters: buildParams, propagate: false) |
| JOBS[jobKey] = job |
| |
| // Set Unstable if job did not succeed |
| if (!isJobSucceeded(jobKey)) { |
| addUnstableStage(jobKey) |
| unstable("Job ${jobName} finished with result ${job.result}") |
| } |
| return job |
| } |
| |
| def getJob(String jobKey) { |
| return JOBS[jobKey] |
| } |
| |
| String getJobUrl(String jobKey) { |
| echo "getJobUrl for ${jobKey}" |
| return getJob(jobKey)?.absoluteUrl ?: '' |
| } |
| |
| boolean isJobSucceeded(String jobKey) { |
| return getJob(jobKey)?.result == 'SUCCESS' |
| } |
| |
| boolean isJobUnstable(String jobKey) { |
| return getJob(jobKey)?.result == 'UNSTABLE' |
| } |
| |
| void addFailedStage(String jobKey = '') { |
| FAILED_STAGES.put("${env.STAGE_NAME}", jobKey) |
| } |
| void addUnstableStage(String jobKey = '') { |
| UNSTABLE_STAGES.put("${env.STAGE_NAME}", jobKey) |
| } |
| |
| void sendPipelineErrorNotification() { |
| String bodyMsg = "Kogito nightly job #${env.BUILD_NUMBER} was: ${currentBuild.currentResult}" |
| |
| if (FAILED_STAGES.size() > 0) { |
| bodyMsg += '\nFailed stages: \n- ' |
| bodyMsg += FAILED_STAGES.collect { "${it.key} => ${getJobUrl(it.value)}" }.join('\n- ') |
| } |
| bodyMsg += '\n' |
| if (UNSTABLE_STAGES.size() > 0) { |
| bodyMsg += '\nUnstable stages: \n- ' |
| bodyMsg += UNSTABLE_STAGES.collect { "${it.key} => ${getJobUrl(it.value)}" }.join('\n- ') |
| } |
| bodyMsg += '\n' |
| bodyMsg += "\nPlease look here: ${env.BUILD_URL}" |
| emailext body: bodyMsg, subject: "[${getBuildBranch()}][d] Full Pipeline", |
| to: env.KOGITO_CI_EMAIL_TO |
| } |
| |
| List getDefaultBuildParams() { |
| List params = [] |
| addStringParam(params, 'DISPLAY_NAME', env.NIGHTLY_TAG) |
| addBooleanParam(params, 'SEND_NOTIFICATION', true) |
| |
| return params |
| } |
| |
| void addSkipTestsParam(buildParams) { |
| addBooleanParam(buildParams, 'SKIP_TESTS', params.SKIP_TESTS) |
| } |
| |
| void addSkipIntegrationTestsParam(buildParams) { |
| addBooleanParam(buildParams, 'SKIP_INTEGRATION_TESTS', params.SKIP_TESTS) |
| } |
| |
| void addStringParam(List params, String key, String value) { |
| params.add(string(name: key, value: value)) |
| } |
| |
| void addBooleanParam(List params, String key, boolean value) { |
| params.add(booleanParam(name: key, value: value)) |
| } |
| |
| String getBuildBranch() { |
| return env.GIT_BRANCH_NAME |
| } |
| |
| String getGitAuthor() { |
| return env.GIT_AUTHOR |
| } |
| |
| String getGitAuthorCredsId() { |
| return env.GIT_AUTHOR_CREDS_ID |
| } |
| |
| String getGitAuthorPushCredsId() { |
| return env.GIT_AUTHOR_PUSH_CREDS_ID |
| } |