forked from apache/incubator-kie-kogito-pipelines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile.nightly
490 lines (414 loc) · 16.3 KB
/
Jenkinsfile.nightly
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
import org.jenkinsci.plugins.workflow.libs.Library
@Library('jenkins-pipeline-shared-libraries')_
// Deploy jobs
RUNTIMES_DEPLOY = 'kogito-runtimes-deploy'
OPTAPLANNER_DEPLOY = 'optaplanner-deploy'
APPS_DEPLOY = 'kogito-apps-deploy'
EXAMPLES_DEPLOY = 'kogito-examples-deploy'
IMAGES_DEPLOY = 'kogito-images-deploy'
EXAMPLES_IMAGES_DEPLOY = 'kogito-examples-images-deploy'
OPERATOR_DEPLOY = 'kogito-operator-deploy'
EXAMPLES_IMAGES_PROMOTE = 'kogito-examples-images-promote'
// Promote jobs
IMAGES_PROMOTE = 'kogito-images-promote'
OPERATOR_PROMOTE = 'kogito-operator-promote'
// 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 = [:]
defaultImageParamsPrefix = 'IMAGE'
kogitoImagesParamsPrefix = 'KOGITO_IMAGES'
examplesImagesParamsPrefix = 'EXAMPLES_IMAGES'
baseImageParamsPrefix = 'BASE_IMAGE'
promoteImageParamsPrefix = 'PROMOTE_IMAGE'
// Should be multibranch pipeline
pipeline {
agent {
label 'kie-rhel7 && !master'
}
options {
timeout(time: 720, unit: 'MINUTES')
}
// parameters {
// For parameters, check into .jenkins/dsl/jobs.groovy file
// }
environment {
// Some generated env is also defined into .jenkins/dsl/jobs.groovy file
KOGITO_CI_EMAIL_TO = credentials("${JENKINS_EMAIL_CREDS_ID}")
IMAGE_NAME_NIGHTLY_SUFFIX = 'nightly'
TEMP_TAG = "temp-${getBuildBranch()}-tests"
// Use branch name in nightly tag as we may have parallel master and release branch builds
NIGHTLY_TAG = """${getBuildBranch()}-${sh(
returnStdout: true,
script: 'date -u "+%Y-%m-%d"'
).trim()}"""
}
stages {
stage('Initialize') {
steps {
script {
echo "temporary tag is ${env.TEMP_TAG}"
echo "nightly tag is ${env.NIGHTLY_TAG}"
currentBuild.displayName = env.NIGHTLY_TAG
}
}
}
stage('Build & Deploy Kogito Runtimes') {
when {
expression { return isArtifactsDeploy() }
}
steps {
script {
echo "Call ${RUNTIMES_DEPLOY} job"
def buildParams = getDefaultBuildParams()
addSkipTestsParam(buildParams)
// images and operator deploy testing will use older working artifacts if that one fails
buildJob(RUNTIMES_DEPLOY, buildParams, false)
}
}
post {
failure {
addFailedStage(RUNTIMES_DEPLOY)
}
}
}
stage('Build & Deploy OptaPlanner') {
when {
expression { return isArtifactsDeploy() }
}
steps {
script {
echo "Call ${OPTAPLANNER_DEPLOY} job"
String optaplannerBranch = getBuildBranch()
String [] buildBranchSplit = optaplannerBranch.split("\\.")
if (buildBranchSplit.length == 3) {
/* The OptaPlanner major version is shifted by 7 from the Kogito major version:
Kogito 1.x.y -> OptaPlanner 8.x.y. */
int majorVersionShift = 7
Integer optaplannerMajorVersion = Integer.parseInt(buildBranchSplit[0]) + majorVersionShift
optaplannerBranch = "${optaplannerMajorVersion}.${buildBranchSplit[1]}.${buildBranchSplit[2]}"
}
def buildParams = getDefaultBuildParams(optaplannerBranch)
addSkipTestsParam(buildParams)
addSkipIntegrationTestsParam(buildParams)
def quickstartsBranch = optaplannerBranch == 'master' ? 'development' : optaplannerBranch
addStringParam(buildParams, 'QUICKSTARTS_BUILD_BRANCH_NAME', quickstartsBranch)
buildJob(OPTAPLANNER_DEPLOY, buildParams, false)
}
}
post {
failure {
addFailedStage(OPTAPLANNER_DEPLOY)
}
}
}
stage('Build & Deploy Kogito Apps') {
when {
expression { return isArtifactsDeploy() }
}
steps {
script {
echo "Call ${APPS_DEPLOY} job"
def buildParams = getDefaultBuildParams()
addSkipTestsParam(buildParams)
// images and operator deploy testing will use older working artifacts if that one fails
buildJob(APPS_DEPLOY, buildParams, false)
}
}
post {
failure {
addFailedStage(APPS_DEPLOY)
}
}
}
stage('Build & Deploy Kogito Examples') {
when {
expression { return isArtifactsDeploy() }
}
steps {
script {
echo "Call ${EXAMPLES_DEPLOY} job"
def buildParams = getDefaultBuildParams()
addSkipTestsParam(buildParams)
// images and operator deploy testing will use older working artifacts if that one fails
buildJob(EXAMPLES_DEPLOY, buildParams, false)
}
}
post {
failure {
addFailedStage(EXAMPLES_DEPLOY)
}
}
}
stage('Build & Deploy Images') {
when {
expression { return isImagesDeploy() }
}
steps {
script {
echo "Call ${IMAGES_DEPLOY} job"
def buildParams = getDefaultBuildParams()
addImageBuildParams(buildParams, env.TEMP_TAG)
// For testing
addSkipTestsParam(buildParams)
addExamplesParam(buildParams)
// Ask to create the PR with changes occuring in this
addBooleanParam(buildParams, 'CREATE_PR', true)
buildJob(IMAGES_DEPLOY, buildParams, false)
}
}
post {
failure {
addFailedStage(IMAGES_DEPLOY)
}
}
}
stage('Build & Deploy Examples Images') {
when {
expression { return isExamplesImagesDeploy() }
}
steps {
script {
echo "Call ${EXAMPLES_IMAGES_DEPLOY} job"
def buildParams = getDefaultBuildParams()
addSkipTestsParam(buildParams)
addImageBuildParams(buildParams, env.TEMP_TAG)
addStringParam(buildParams, constructKey(defaultImageParamsPrefix, 'NAME_PREFIX'), 'examples')
// For BDD tests
addImageBuildParams(buildParams, env.TEMP_TAG, false, kogitoImagesParamsPrefix)
addExamplesParam(buildParams)
buildJob(EXAMPLES_IMAGES_DEPLOY, buildParams, false)
}
}
post {
failure {
addFailedStage(OPERATOR_DEPLOY)
}
}
}
stage('Build & Deploy Operator') {
when {
expression { return isOperatorDeploy() }
}
steps {
script {
echo "Call ${OPERATOR_DEPLOY} job"
def buildParams = getDefaultBuildParams()
addSkipTestsParam(buildParams)
addImageBuildParams(buildParams, env.TEMP_TAG)
// For BDD tests
addImageBuildParams(buildParams, env.TEMP_TAG, false, kogitoImagesParamsPrefix)
addImageBuildParams(buildParams, env.TEMP_TAG, false, examplesImagesParamsPrefix)
addStringParam(buildParams, constructKey(examplesImagesParamsPrefix, 'CACHE_MODE'), isExamplesImagesDeploy() ? 'always' : 'never')
addStringParam(buildParams, constructKey(examplesImagesParamsPrefix, 'NAME_PREFIX'), 'examples')
addExamplesParam(buildParams)
buildJob(OPERATOR_DEPLOY, buildParams, false)
}
}
post {
failure {
addFailedStage(OPERATOR_DEPLOY)
}
}
}
stage('Promote Images') {
when {
expression { return isImagesPromote() }
}
steps {
script {
echo "Call ${IMAGES_PROMOTE} job"
def buildParams = getDefaultBuildParams()
addDeployBuildUrlParam(buildParams, IMAGES_DEPLOY)
// Base image information is given by the deploy URL
addImageBuildParams(buildParams, env.NIGHTLY_TAG, true, promoteImageParamsPrefix)
addBooleanParam(buildParams, 'DEPLOY_WITH_LATEST_TAG', isDeployImagesLatestTag())
buildJob(IMAGES_PROMOTE, buildParams)
}
}
post {
failure {
addFailedStage(IMAGES_PROMOTE)
}
}
}
stage('Promote Examples Images') {
when {
expression { return isExamplesImagesPromote() }
}
steps {
script {
echo "Call ${EXAMPLES_IMAGES_PROMOTE} job"
def buildParams = getDefaultBuildParams()
addDeployBuildUrlParam(buildParams, EXAMPLES_IMAGES_DEPLOY)
// Base image information is given by the deploy URL
addImageBuildParams(buildParams, env.NIGHTLY_TAG, true, promoteImageParamsPrefix)
addStringParam(buildParams, constructKey(promoteImageParamsPrefix, 'NAME_PREFIX'), 'examples')
addBooleanParam(buildParams, 'DEPLOY_WITH_LATEST_TAG', isDeployImagesLatestTag())
buildJob(EXAMPLES_IMAGES_PROMOTE, buildParams)
}
}
post {
failure {
addFailedStage(OPERATOR_PROMOTE)
}
}
}
stage('Promote Operator') {
when {
expression { return isOperatorPromote() }
}
steps {
script {
echo "Call ${OPERATOR_PROMOTE} job"
def buildParams = getDefaultBuildParams()
addDeployBuildUrlParam(buildParams, OPERATOR_DEPLOY)
// Base image information is given by the deploy URL
addImageBuildParams(buildParams, env.NIGHTLY_TAG, true, promoteImageParamsPrefix)
addBooleanParam(buildParams, 'DEPLOY_WITH_LATEST_TAG', isDeployImagesLatestTag())
buildJob(OPERATOR_PROMOTE, buildParams)
}
}
post {
failure {
addFailedStage(OPERATOR_PROMOTE)
}
}
}
}
post {
unsuccessful {
sendPipelineErrorNotification()
}
}
}
def buildJob(String jobName, List buildParams, boolean propagate=true) {
echo "Build ${jobName} with params ${buildParams} and propagate = ${propagate}"
def job = build(job: "${jobName}", wait: true, parameters: buildParams, propagate: propagate)
JOBS[jobName] = job
// Set Unstable if we don't propagate and job failed
if (!propagate && !isJobSucceeded(jobName)) {
addUnstableStage(jobName)
unstable("Job ${jobName} finished with result ${job.getResult()}")
}
return job
}
def getJob(String jobName) {
return JOBS[jobName]
}
String getJobUrl(String jobName) {
echo "getJobUrl for ${jobName}"
def job = getJob(jobName)
return job ? job.getAbsoluteUrl() : ''
}
boolean isJobSucceeded(String jobName) {
def job = getJob(jobName)
return job ? job.getResult() == 'SUCCESS' : false
}
void addFailedStage(String jobName = '') {
FAILED_STAGES.put("${STAGE_NAME}", jobName)
}
void addUnstableStage(String jobName = '') {
UNSTABLE_STAGES.put("${STAGE_NAME}", jobName)
}
void sendPipelineErrorNotification() {
String bodyMsg = "Kogito nightly job #${BUILD_NUMBER} was: ${currentBuild.currentResult}"
paramsStr = ''
if (params.SKIP_TESTS) {
paramsStr += '\n- Tests skipped'
}
if (params.SKIP_ARTIFACTS) {
paramsStr += '\n- Artifacts skipped'
}
if (params.SKIP_IMAGES) {
paramsStr += '\n- Images skipped'
}
if (params.SKIP_OPERATOR) {
paramsStr += '\n- Operator skipped'
}
bodyMsg += paramsStr ? "\n\nConfiguration:${paramsStr}" : '\n'
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: ${BUILD_URL}"
emailext body: bodyMsg, subject: "[${getBuildBranch()}][d] Full Pipeline",
to: env.KOGITO_CI_EMAIL_TO
}
List getDefaultBuildParams(String buildBranchName = '') {
buildBranchName = buildBranchName ?: getBuildBranch()
List params = []
addStringParam(params, 'DISPLAY_NAME', env.NIGHTLY_TAG)
return params
}
void addSkipTestsParam(buildParams) {
addBooleanParam(buildParams, 'SKIP_TESTS', params.SKIP_TESTS)
}
void addSkipIntegrationTestsParam(buildParams) {
addBooleanParam(buildParams, 'SKIP_INTEGRATION_TESTS', params.SKIP_TESTS)
}
void addExamplesParam(buildParams) {
addStringParam(buildParams, 'EXAMPLES_URI', "https://github.com/${getGitAuthor()}/kogito-examples")
addStringParam(buildParams, 'EXAMPLES_REF', getBuildBranch())
}
void addImageBuildParams(List buildParams, String tag, boolean isFinalImage = false, String paramsPrefix = defaultImageParamsPrefix) {
addBooleanParam(buildParams, constructKey(paramsPrefix, 'USE_OPENSHIFT_REGISTRY'), !isFinalImage && params.USE_TEMP_OPENSHIFT_REGISTRY)
addStringParam(buildParams, constructKey(paramsPrefix, 'REGISTRY_CREDENTIALS'), env.IMAGE_REGISTRY_CREDENTIALS)
addStringParam(buildParams, constructKey(paramsPrefix, 'REGISTRY'), env.IMAGE_REGISTRY)
addStringParam(buildParams, constructKey(paramsPrefix, 'NAMESPACE'), env.IMAGE_NAMESPACE)
addStringParam(buildParams, constructKey(paramsPrefix, 'NAME_SUFFIX'), env.IMAGE_NAME_NIGHTLY_SUFFIX)
addStringParam(buildParams, constructKey(paramsPrefix, 'TAG'), tag)
}
void addDeployBuildUrlParam(buildParams, jobName) {
addStringParam(buildParams, 'DEPLOY_BUILD_URL', getJobUrl(jobName))
}
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 constructKey(String prefix, String paramId) {
return prefix ? "${prefix}_${paramId}" : paramId
}
String getBuildBranch() {
return env.GIT_BRANCH_NAME
}
String getGitAuthor() {
return env.GIT_AUTHOR
}
boolean isDeployImagesLatestTag() {
return getBuildBranch() == env.BRANCH_FOR_LATEST
}
boolean isArtifactsDeploy() {
return !params.SKIP_ARTIFACTS
}
boolean isImagesDeploy() {
return !params.SKIP_IMAGES
}
boolean isExamplesImagesDeploy() {
return !params.SKIP_EXAMPLES_IMAGES
}
boolean isOperatorDeploy() {
return !params.SKIP_OPERATOR
}
boolean isImagesPromote() {
return isImagesDeploy() && isJobSucceeded(IMAGES_DEPLOY)
}
boolean isExamplesImagesPromote() {
return isExamplesImagesDeploy() && isJobSucceeded(EXAMPLES_IMAGES_DEPLOY)
}
boolean isOperatorPromote() {
return isOperatorDeploy() && isJobSucceeded(OPERATOR_DEPLOY)
}
String getArtifactsRepositoryParam() {
return env['ARTIFACTS_REPOSITORY'] ?: ''
}