-
Notifications
You must be signed in to change notification settings - Fork 4
/
Jenkinsfile.release
476 lines (394 loc) · 16.4 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
import org.jenkinsci.plugins.workflow.libs.Library
@Library('jenkins-pipeline-shared-libraries')_
kogitoRuntimesRepo = 'kogito-runtimes'
kogitoAppsRepo = 'kogito-apps'
kogitoExamplesRepo = 'kogito-examples'
ARTIFACTS_STAGING_STAGE = 'stage.artifacts.staging'
ARTIFACTS_RELEASE_STAGE = 'stage.artifacts.release'
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 'kie-rhel8 && !built-in'
}
// 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 getDroolsVersion()
assert getKogitoVersion()
currentBuild.displayName = getDisplayName()
sendNotification("Release Pipeline has started...\nKogito version = ${getKogitoVersion()}\n=> ${env.BUILD_URL}")
// Safety measure to not publish to main JBoss
if (getGitAuthor() != 'kiegroup' && !getArtifactsRepositoryParam()) {
sendNotification("Git Author is different from `kiegroup` and no `ARTIFACTS_REPOSITORY` parameter has been provided. Are you sure you want to continue ? => ${env.BUILD_URL}input")
input message: 'Should the pipeline continue with no `ARTIFACTS_REPOSITORY` defined ?', ok: 'Yes'
}
}
}
post {
always {
setReleasePropertyIfneeded('kogito.version', getKogitoVersion())
}
}
}
stage('Build & Deploy Kogito Runtimes') {
steps {
script {
def buildParams = getDefaultBuildParams(getKogitoVersion())
addSkipTestsParam(buildParams)
addStringParam(buildParams, 'DROOLS_VERSION', getDroolsVersion())
buildJob(getDeployJobName(kogitoRuntimesRepo), buildParams)
}
}
}
stage('Build & Deploy Kogito Apps') {
steps {
script {
def buildParams = getDefaultBuildParams(getKogitoVersion())
addSkipTestsParam(buildParams)
buildJob(getDeployJobName(kogitoAppsRepo), buildParams)
}
}
}
stage('Build & Deploy Kogito Examples') {
steps {
script {
def buildParams = getDefaultBuildParams(getKogitoVersion())
addSkipTestsParam(buildParams)
addStringParam(buildParams, 'DROOLS_VERSION', getDroolsVersion())
buildJob(getDeployJobName(kogitoExamplesRepo), buildParams)
}
}
}
stage('Artifacts\' staging finalization') {
steps {
script {
if (!areArtifactsStaged()) {
sendNotification("All artifacts have been staged. You can find them here: ${getStagingRepository()}")
}
setArtifactsStaged()
}
}
}
stage('Are staged artifacts released?') {
when {
// Execute only if artifacts repository was not given, which means the staging repository has been created
expression { return !getArtifactsRepositoryParam() && !areArtifactsReleased() }
}
steps {
script {
String body = "${getKogitoVersion()} artifacts are ready for release.\n" +
"Please release the staging repositories and then confirm here: ${env.BUILD_URL}input"
sendNotification(body)
input message: 'Has the staging repository been released ?', ok: 'Yes'
sendNotification('Artifacts have been released. Finalizing now the release ...')
setArtifactsReleased()
}
}
}
stage('Promote Kogito Runtimes') {
when {
expression { return isJobConsideredOk(getDeployJobName(kogitoRuntimesRepo)) }
}
steps {
script {
def buildParams = getDefaultBuildParams(getKogitoVersion())
addDeployBuildUrlParam(buildParams, getDeployJobName(kogitoRuntimesRepo))
addStringParam(buildParams, 'DROOLS_VERSION', getDroolsVersion())
buildJob(getPromoteJobName(kogitoRuntimesRepo), buildParams)
}
}
}
stage('Promote Kogito Apps') {
when {
expression { return isJobConsideredOk(getDeployJobName(kogitoAppsRepo)) }
}
steps {
script {
def buildParams = getDefaultBuildParams(getKogitoVersion())
addDeployBuildUrlParam(buildParams, getDeployJobName(kogitoAppsRepo))
buildJob(getPromoteJobName(kogitoAppsRepo), buildParams)
}
}
}
stage('Promote Kogito Examples') {
when {
expression { return isJobConsideredOk(getDeployJobName(kogitoExamplesRepo)) }
}
steps {
script {
def buildParams = getDefaultBuildParams(getKogitoVersion())
addStringParam(buildParams, 'DROOLS_VERSION', getDroolsVersion())
addDeployBuildUrlParam(buildParams, getDeployJobName(kogitoExamplesRepo))
addBooleanParam(buildParams, 'UPDATE_STABLE_BRANCH', isDeployAsLatest())
buildJob(getPromoteJobName(kogitoExamplesRepo), buildParams)
}
}
}
stage('Setup next snapshot version') {
steps {
script {
def buildParams = []
addStringParam(buildParams, 'KOGITO_VERSION', util.getNextVersion(getKogitoVersion(), 'micro'))
addStringParam(buildParams, 'DROOLS_VERSION', util.getNextVersion(getDroolsVersion(), 'micro'))
addBooleanParam(buildParams, 'SKIP_CLOUD_SETUP_BRANCH', true)
build(job: '../setup-branch/0-setup-branch', wait: false, parameters: buildParams, propagate: false)
}
}
}
stage('Start Cloud release') {
when {
expression { isCloudRelease() }
}
steps {
script {
def buildParams = getDefaultBuildParams(getKogitoVersion())
addStringParam(buildParams, 'KOGITO_VERSION', getKogitoVersion())
addBooleanParam(buildParams, 'DEPLOY_AS_LATEST', isDeployAsLatest())
addSkipTestsParam(buildParams)
build(job: './0-kogito-release-cloud', wait: false, parameters: buildParams, propagate: false)
}
}
}
}
post {
always {
script {
saveReleaseProperties()
}
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 getPromoteJobName(String repository) {
return "${repository}-promote"
}
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}")
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 ?: getKogitoVersion()
return "Release ${version}"
}
String getKogitoVersion() {
return params.KOGITO_VERSION ?: getReleaseProperty('kogito.version')
}
String getDroolsVersion() {
return params.DROOLS_VERSION ?: getReleaseProperty('drools.version')
}
String getGitAuthor() {
return env.GIT_AUTHOR
}
String getArtifactsRepositoryParam() {
return env['ARTIFACTS_REPOSITORY'] ?: ''
}
String getStagingRepository() {
return getArtifactsRepositoryParam() ?: env.DEFAULT_STAGING_REPOSITORY
}
boolean isDeployAsLatest() {
return params.DEPLOY_AS_LATEST || getReleaseProperty('deploy.latest')?.toBoolean()
}
boolean isCloudRelease() {
return !params.SKIP_CLOUD_RELEASE
}
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)
}
boolean areArtifactsReleased() {
return hasReleaseProperty(ARTIFACTS_RELEASE_STAGE)
}
void setArtifactsReleased() {
setReleasePropertyIfneeded(ARTIFACTS_RELEASE_STAGE, true)
}