-
Notifications
You must be signed in to change notification settings - Fork 45
/
Jenkinsfile.release
337 lines (279 loc) · 11 KB
/
Jenkinsfile.release
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import org.jenkinsci.plugins.workflow.libs.Library
@Library('jenkins-pipeline-shared-libraries')_
kogitoRuntimesRepo = 'kogito-runtimes'
kogitoAppsRepo = 'kogito-apps'
ARTIFACTS_STAGING_STAGE = 'stage.artifacts.staging'
JOB_PROPERTY_PREFIX = 'build'
JOB_RESULT_PROPERTY_KEY = 'result'
JOB_URL_PROPERTY_KEY = 'absoluteUrl'
JOB_DECISION_PROPERTY_KEY = 'decision'
JOB_DECISION_MESSAGE_PROPERTY_KEY = 'decisionMessage'
releaseProperties = [:]
pipeline {
agent {
label util.avoidFaultyNodes('ubuntu')
}
// 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}")
}
stages {
stage('Initialize') {
steps {
script {
// Restore config from previous run
if (params.RESTORE_FROM_PREVIOUS_JOB) {
releaseProperties = readPropertiesFromUrl(params.RESTORE_FROM_PREVIOUS_JOB, 'release.properties')
echo "Release properties imported from previous job: ${releaseProperties}"
}
assert getReleaseVersion()
currentBuild.displayName = getDisplayName()
sendNotification("Release Pipeline has started...\nKogito version = ${getReleaseVersion()}\n=> ${env.BUILD_URL}")
}
}
post {
always {
setReleasePropertyIfneeded('release.version', getReleaseVersion())
setReleasePropertyIfneeded('git.tag.name', getGitTagName())
}
}
}
stage('Build & Deploy Kogito Runtimes') {
steps {
script {
def buildParams = getDefaultBuildParams(getReleaseVersion())
addSkipTestsParam(buildParams)
addStringParam(buildParams, 'DROOLS_VERSION', getReleaseVersion())
buildJob(getDeployJobName(kogitoRuntimesRepo), buildParams)
}
}
}
stage('Build & Deploy Kogito Apps') {
steps {
script {
def buildParams = getDefaultBuildParams(getReleaseVersion())
addSkipTestsParam(buildParams)
buildJob(getDeployJobName(kogitoAppsRepo), buildParams)
}
}
}
stage('Artifacts\' staging finalization') {
steps {
script {
if (!areArtifactsStaged()) {
sendNotification("All artifacts have been staged. You can find them here: ${getStagingRepository()}")
}
setArtifactsStaged()
}
}
}
}
post {
always {
script {
saveReleaseProperties()
}
}
cleanup {
cleanWs()
}
success {
script {
sendSuccessfulReleaseNotification()
}
}
unsuccessful {
sendErrorNotification()
}
}
}
def buildJob(String jobName, List buildParams, boolean shouldWait = true) {
if (!hasJob(jobName) || (getJobResult(jobName) != 'SUCCESS' && getJobDecision(jobName) == 'retry')) {
sendStageNotification()
echo "Build ${jobName} with shouldWait=${shouldWait} and params ${buildParams}"
def job = build(job: "./${jobName}", wait: shouldWait, parameters: buildParams, propagate: false)
removeJobDecision(jobName)
registerJobExecution(jobName, job.result, job.absoluteUrl)
} else {
echo 'Job was already executed. Retrieving information...'
}
saveReleaseProperties()
def jobResult = getJobResult(jobName)
def jobUrl = getJobUrl(jobName)
def jobDecision = getJobDecision(jobName)
if (jobResult != 'SUCCESS') {
if (jobDecision != 'continue' && jobDecision != 'skip') {
echo "Sending a notification about an unsuccessful job build ${jobName}."
sendNotification("`${jobName}` finished with status `${jobResult}`.\nSee: ${jobUrl}\n\nPlease provide which action should be done (retry ? continue ? skip ? abort ?): ${env.BUILD_URL}input")
// abort is handled automatically by the pipeline in the input
def result = input message: "Job `${jobName}` is in status ${jobResult}. What do you want to do ?\nBeware that skipping a deploy job will not launch the promote part.", parameters: [choice(name: 'ACTION', choices: ['retry', 'continue', 'skip'].join('\n')), string(name: 'MESSAGE', description: 'If you want to add information to your action...')]
def inputDecision = result['ACTION']
def inputMessage = result['MESSAGE']
registerJobDecision(jobName, inputDecision, inputMessage)
String resultStr = "`${jobName}` failure => Decision was made to ${inputDecision}."
if (inputMessage) {
resultStr += "Additional Information: `${inputMessage}`"
}
sendNotification(resultStr)
if (inputDecision == 'retry') {
// If retry, remove job and build again
return buildJob(jobName, buildParams)
}
} else {
echo "Job decision was '${jobDecision}'"
}
} else {
echo 'Job succeeded'
}
}
String getDeployJobName(String repository) {
return "${repository}-deploy"
}
String getJobPropertySuffix(String jobName) {
return "${JOB_PROPERTY_PREFIX}.${jobName}"
}
String getJobPropertyKey(String jobName, String key) {
return "${getJobPropertySuffix(jobName)}.${key}"
}
def registerJobExecution(String jobName, String result, String absoluteUrl) {
setReleasePropertyIfneeded(getJobPropertyKey(jobName, JOB_RESULT_PROPERTY_KEY), result)
setReleasePropertyIfneeded(getJobPropertyKey(jobName, JOB_URL_PROPERTY_KEY), absoluteUrl)
}
def registerJobDecision(String jobName, String decision, String message = '') {
setReleasePropertyIfneeded(getJobPropertyKey(jobName, JOB_DECISION_PROPERTY_KEY), decision)
setReleasePropertyIfneeded(getJobPropertyKey(jobName, JOB_DECISION_MESSAGE_PROPERTY_KEY), message)
}
def removeJobDecision(String jobName) {
removeReleaseProperty(getJobPropertyKey(jobName, JOB_DECISION_PROPERTY_KEY))
removeReleaseProperty(getJobPropertyKey(jobName, JOB_DECISION_MESSAGE_PROPERTY_KEY))
}
List getAllJobNames() {
return releaseProperties.findAll { it.key.startsWith(JOB_PROPERTY_PREFIX) }.collect { it.key.split('\\.')[1] }.unique()
}
boolean hasJob(String jobName) {
return releaseProperties.any { it.key.startsWith(getJobPropertySuffix(jobName)) }
}
String getJobUrl(String jobName) {
echo "getJobUrl for ${jobName}"
return getReleaseProperty(getJobPropertyKey(jobName, JOB_URL_PROPERTY_KEY)) ?: ''
}
String getJobResult(String jobName) {
echo "getJobResult for ${jobName}"
return getReleaseProperty(getJobPropertyKey(jobName, JOB_RESULT_PROPERTY_KEY)) ?: ''
}
String getJobDecision(String jobName) {
echo "getJobDecision for ${jobName}"
return getReleaseProperty(getJobPropertyKey(jobName, JOB_DECISION_PROPERTY_KEY)) ?: ''
}
boolean isJobConsideredOk(String jobName) {
String result = getJobResult(jobName)
String decision = getJobDecision(jobName)
return result == 'SUCCESS' || (result == 'UNSTABLE' && decision == 'continue')
}
void saveReleaseProperties() {
def propertiesStr = releaseProperties.collect { entry -> "${entry.key}=${entry.value}" }.join('\n')
writeFile( file : 'release.properties' , text : propertiesStr)
archiveArtifacts artifacts: 'release.properties'
}
void sendSuccessfulReleaseNotification() {
String bodyMsg = 'Release is successful with those jobs:\n'
getAllJobNames().findAll { isJobConsideredOk(it) }.each {
bodyMsg += "- ${it}\n"
}
bodyMsg += "\nPlease look here: ${BUILD_URL} for more information"
sendNotification(bodyMsg)
}
void sendErrorNotification() {
sendNotification("Kogito release job #${BUILD_NUMBER} was: ${currentBuild.currentResult}\nPlease look here: ${BUILD_URL}")
}
void sendStageNotification() {
sendNotification("${env.STAGE_NAME}")
}
void sendNotification(String body) {
echo 'Send Notification'
echo body
emailext body: body, subject: "[${env.GIT_BRANCH_NAME}] Release Pipeline",
to: env.KOGITO_CI_EMAIL_TO
}
def readPropertiesFromUrl(String url, String propsFilename) {
if (!url.endsWith('/')) {
url += '/'
}
sh "wget ${url}artifact/${propsFilename} -O ${propsFilename}"
def props = readProperties file: propsFilename
echo props.collect { entry -> "${entry.key}=${entry.value}" }.join('\n')
return props
}
List getDefaultBuildParams(String version) {
List buildParams = []
addDisplayNameParam(buildParams, getDisplayName(version))
addStringParam(buildParams, 'PROJECT_VERSION', version)
addStringParam(buildParams, 'KOGITO_PR_BRANCH', "kogito-${version}")
addStringParam(buildParams, 'GIT_TAG_NAME', getGitTagName())
return buildParams
}
void addDisplayNameParam(buildParams, name = '') {
name = name ?: getDisplayName()
addStringParam(buildParams, 'DISPLAY_NAME', name)
}
void addDeployBuildUrlParam(buildParams, jobName) {
addDeployBuildUrlParamOrClosure(buildParams, jobName)
}
void addDeployBuildUrlParamOrClosure(buildParams, jobName, closure = null) {
String url = getJobUrl(jobName)
if (url) {
addStringParam(buildParams, 'DEPLOY_BUILD_URL', getJobUrl(jobName))
} else if (closure) {
closure()
}
}
void addSkipTestsParam(buildParams) {
addBooleanParam(buildParams, 'SKIP_TESTS', params.SKIP_TESTS)
}
void addStringParam(List buildParams, String key, String value) {
buildParams.add(string(name: key, value: value))
}
void addBooleanParam(List buildParams, String key, boolean value) {
buildParams.add(booleanParam(name: key, value: value))
}
String constructKey(String prefix, String paramId) {
return prefix ? "${prefix}_${paramId}" : paramId
}
String getDisplayName(version = '') {
version = version ?: getReleaseVersion()
return "Release ${version}"
}
String getReleaseVersion() {
return params.RELEASE_VERSION ?: getReleaseProperty('release.version')
}
String getGitTagName() {
return params.GIT_TAG_NAME ?: getReleaseProperty('git.tag.name')
}
String getGitAuthor() {
return env.GIT_AUTHOR
}
void setReleasePropertyIfneeded(String key, def value) {
if (value) {
releaseProperties[key] = value
}
}
void removeReleaseProperty(String key) {
if (hasReleaseProperty(key)) {
releaseProperties.remove(key)
}
}
boolean hasReleaseProperty(String key) {
return releaseProperties.containsKey(key)
}
def getReleaseProperty(String key) {
return hasReleaseProperty(key) ? releaseProperties[key] : ''
}
boolean areArtifactsStaged() {
return hasReleaseProperty(ARTIFACTS_STAGING_STAGE)
}
void setArtifactsStaged() {
setReleasePropertyIfneeded(ARTIFACTS_STAGING_STAGE, true)
}