|
| 1 | +import org.jenkinsci.plugins.workflow.libs.Library |
| 2 | + |
| 3 | +@Library('jenkins-pipeline-shared-libraries')_ |
| 4 | + |
| 5 | +// Deploy jobs |
| 6 | +RUNTIMES_DEPLOY = 'kogito-runtimes.weekly-deploy' |
| 7 | +APPS_DEPLOY = 'kogito-apps.weekly-deploy' |
| 8 | + |
| 9 | +// Map of executed jobs |
| 10 | +// See https://javadoc.jenkins.io/plugin/workflow-support/org/jenkinsci/plugins/workflow/support/steps/build/RunWrapper.html |
| 11 | +// for more options on built job entity |
| 12 | +JOBS = [:] |
| 13 | + |
| 14 | +FAILED_STAGES = [:] |
| 15 | +UNSTABLE_STAGES = [:] |
| 16 | + |
| 17 | +// Should be multibranch pipeline |
| 18 | +pipeline { |
| 19 | + agent { |
| 20 | + label 'ubuntu' |
| 21 | + } |
| 22 | + |
| 23 | + options { |
| 24 | + timeout(time: 900, unit: 'MINUTES') |
| 25 | + } |
| 26 | + |
| 27 | + // parameters { |
| 28 | + // For parameters, check into ./dsl/jobs.groovy file |
| 29 | + // } |
| 30 | + |
| 31 | + environment { |
| 32 | + // Some generated env is also defined into ./dsl/jobs.groovy file |
| 33 | + KOGITO_CI_EMAIL_TO = credentials("${JENKINS_EMAIL_CREDS_ID}") |
| 34 | + |
| 35 | + CURRENT_DATE = getCurrentDate() |
| 36 | + |
| 37 | + WEEKLY_TAG = """${getBuildBranch()}-${CURRENT_DATE}""" |
| 38 | + } |
| 39 | + |
| 40 | + stages { |
| 41 | + stage('Initialize') { |
| 42 | + steps { |
| 43 | + script { |
| 44 | + echo "weekly tag is ${env.WEEKLY_TAG}" |
| 45 | + |
| 46 | + currentBuild.displayName = env.WEEKLY_TAG |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + stage('Build & Deploy Kogito Runtimes') { |
| 52 | + steps { |
| 53 | + script { |
| 54 | + def buildParams = getDefaultBuildParams() |
| 55 | + addSkipTestsParam(buildParams) |
| 56 | + addSkipIntegrationTestsParam(buildParams) |
| 57 | + buildJob(RUNTIMES_DEPLOY, buildParams) |
| 58 | + } |
| 59 | + } |
| 60 | + post { |
| 61 | + failure { |
| 62 | + addFailedStage(RUNTIMES_DEPLOY) |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + stage('Build & Deploy Kogito Apps') { |
| 68 | + steps { |
| 69 | + script { |
| 70 | + def buildParams = getDefaultBuildParams() |
| 71 | + addSkipTestsParam(buildParams) |
| 72 | + addSkipIntegrationTestsParam(buildParams) |
| 73 | + buildJob(APPS_DEPLOY, buildParams) |
| 74 | + } |
| 75 | + } |
| 76 | + post { |
| 77 | + failure { |
| 78 | + addFailedStage(APPS_DEPLOY) |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + post { |
| 84 | + unsuccessful { |
| 85 | + sendPipelineErrorNotification() |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +def buildJob(String jobName, List buildParams, String jobKey = jobName) { |
| 91 | + echo "[${jobKey}] Build ${jobName} with params ${buildParams}" |
| 92 | + |
| 93 | + def job = build(job: "${jobName}", wait: true, parameters: buildParams, propagate: false) |
| 94 | + JOBS[jobKey] = job |
| 95 | + |
| 96 | + // Set Unstable if job did not succeed |
| 97 | + if (!isJobSucceeded(jobKey)) { |
| 98 | + addUnstableStage(jobKey) |
| 99 | + unstable("Job ${jobName} finished with result ${job.result}") |
| 100 | + } |
| 101 | + return job |
| 102 | +} |
| 103 | + |
| 104 | +def getJob(String jobKey) { |
| 105 | + return JOBS[jobKey] |
| 106 | +} |
| 107 | + |
| 108 | +String getJobUrl(String jobKey) { |
| 109 | + echo "getJobUrl for ${jobKey}" |
| 110 | + return getJob(jobKey)?.absoluteUrl ?: '' |
| 111 | +} |
| 112 | + |
| 113 | +boolean isJobSucceeded(String jobKey) { |
| 114 | + return getJob(jobKey)?.result == 'SUCCESS' |
| 115 | +} |
| 116 | + |
| 117 | +boolean isJobUnstable(String jobKey) { |
| 118 | + return getJob(jobKey)?.result == 'UNSTABLE' |
| 119 | +} |
| 120 | + |
| 121 | +void addFailedStage(String jobKey = '') { |
| 122 | + FAILED_STAGES.put("${env.STAGE_NAME}", jobKey) |
| 123 | +} |
| 124 | +void addUnstableStage(String jobKey = '') { |
| 125 | + UNSTABLE_STAGES.put("${env.STAGE_NAME}", jobKey) |
| 126 | +} |
| 127 | + |
| 128 | +void sendPipelineErrorNotification() { |
| 129 | + String bodyMsg = "Kogito weekly job #${env.BUILD_NUMBER} was: ${currentBuild.currentResult}" |
| 130 | + |
| 131 | + if (FAILED_STAGES.size() > 0) { |
| 132 | + bodyMsg += '\nFailed stages: \n- ' |
| 133 | + bodyMsg += FAILED_STAGES.collect { "${it.key} => ${getJobUrl(it.value)}" }.join('\n- ') |
| 134 | + } |
| 135 | + bodyMsg += '\n' |
| 136 | + if (UNSTABLE_STAGES.size() > 0) { |
| 137 | + bodyMsg += '\nUnstable stages: \n- ' |
| 138 | + bodyMsg += UNSTABLE_STAGES.collect { "${it.key} => ${getJobUrl(it.value)}" }.join('\n- ') |
| 139 | + } |
| 140 | + bodyMsg += '\n' |
| 141 | + bodyMsg += "\nPlease look here: ${env.BUILD_URL}" |
| 142 | + emailext body: bodyMsg, subject: "[${getBuildBranch()}][d] Full Pipeline", |
| 143 | + to: env.KOGITO_CI_EMAIL_TO |
| 144 | +} |
| 145 | + |
| 146 | +List getDefaultBuildParams() { |
| 147 | + List params = [] |
| 148 | + addStringParam(params, 'DISPLAY_NAME', env.WEEKLY_TAG) |
| 149 | + addStringParam(params, 'GIT_CHECKOUT_DATETIME', getCheckoutDatetime()) |
| 150 | + addBooleanParam(params, 'SEND_NOTIFICATION', true) |
| 151 | + |
| 152 | + return params |
| 153 | +} |
| 154 | + |
| 155 | +void addSkipTestsParam(buildParams) { |
| 156 | + addBooleanParam(buildParams, 'SKIP_TESTS', params.SKIP_TESTS) |
| 157 | +} |
| 158 | + |
| 159 | +void addSkipIntegrationTestsParam(buildParams) { |
| 160 | + addBooleanParam(buildParams, 'SKIP_INTEGRATION_TESTS', params.SKIP_TESTS) |
| 161 | +} |
| 162 | + |
| 163 | +void addStringParam(List params, String key, String value) { |
| 164 | + params.add(string(name: key, value: value)) |
| 165 | +} |
| 166 | + |
| 167 | +void addBooleanParam(List params, String key, boolean value) { |
| 168 | + params.add(booleanParam(name: key, value: value)) |
| 169 | +} |
| 170 | + |
| 171 | +String getBuildBranch() { |
| 172 | + return env.GIT_BRANCH_NAME |
| 173 | +} |
| 174 | + |
| 175 | +String getGitAuthor() { |
| 176 | + return env.GIT_AUTHOR |
| 177 | +} |
| 178 | + |
| 179 | +String getGitAuthorCredsId() { |
| 180 | + return env.GIT_AUTHOR_CREDS_ID |
| 181 | +} |
| 182 | + |
| 183 | +String getGitAuthorPushCredsId() { |
| 184 | + return env.GIT_AUTHOR_PUSH_CREDS_ID |
| 185 | +} |
| 186 | + |
| 187 | +String getCurrentDate() { |
| 188 | + return sh(returnStdout: true, script: 'date -u "+%Y-%m-%d"').trim() |
| 189 | +} |
| 190 | + |
| 191 | +String getCheckoutDatetime() { |
| 192 | + return env.CURRENT_DATE + ' 02:00' // Cut-off 02:00AM |
| 193 | +} |
0 commit comments