forked from camunda/camunda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
545 lines (472 loc) · 21.9 KB
/
Jenkinsfile
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
// vim: set filetype=groovy:
@Library(["camunda-ci", "zeebe-jenkins-shared-library"]) _
// the build name will be used as a kubernetes label, and kubernetes has strict syntax rules - see
// https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#syntax-and-character-set
def buildName = "${env.JOB_BASE_NAME.replaceAll("%2F", "-").replaceAll("\\.", "-").take(20)}-${env.BUILD_ID}"
def masterBranchName = 'master'
def isMasterBranch = env.BRANCH_NAME == masterBranchName
def developBranchName = 'develop'
def isDevelopBranch = env.BRANCH_NAME == developBranchName
//for develop branch keep builds for 7 days to be able to analyse build errors, for all other branches, keep the last 10 builds
def daysToKeep = isDevelopBranch ? '7' : '-1'
def numToKeep = isDevelopBranch ? '-1' : '10'
// single step timeouts - remember to be generous to avoid occasional slow downs, e.g. waiting to be
// scheduled by Kubernetes, slow downloads of remote docker images, etc.
def shortTimeoutMinutes = 10
def longTimeoutMinutes = 45
// the IT agent needs to share some files for post analysis, and since they share the same name as
// those in the main agent, we unstash them in a separate directory
itAgentUnstashDirectory = '.tmp/it'
itFlakyTestStashName = 'it-flakyTests'
//the develop branch should be run at midnight to do a nightly build including QA test run
def cronTrigger = isDevelopBranch ? '0 0 * * *' : ''
pipeline {
agent {
kubernetes {
cloud 'zeebe-ci'
label "zeebe-ci-build_${buildName}"
defaultContainer 'jnlp'
yaml templatePodspec('.ci/podSpecs/distribution-template.yml')
}
}
environment {
NEXUS = credentials("camunda-nexus")
SONARCLOUD_TOKEN = credentials('zeebe-sonarcloud-token')
}
triggers {
cron(cronTrigger)
}
options {
buildDiscarder(logRotator(daysToKeepStr: daysToKeep, numToKeepStr: numToKeep))
timestamps()
}
parameters {
booleanParam(name: 'SKIP_VERIFY', defaultValue: false, description: "Skip 'Verify' Stage")
booleanParam(name: 'RUN_QA', defaultValue: false, description: "Run QA Stage")
string(name: 'GENERATION_TEMPLATE', defaultValue: 'Zeebe SNAPSHOT', description: "Generation template for QA tests (the QA test will be run with this Zeebe version and Operate/Elasticsearch version from the generation template)")
}
stages {
stage('Prepare Distribution') {
steps {
timeout(time: shortTimeoutMinutes, unit: 'MINUTES') {
setHumanReadableBuildDisplayName()
prepareMavenContainer()
prepareMavenContainer('jdk8')
container('golang') {
sh '.ci/scripts/distribution/prepare-go.sh'
}
// prepare unstash directory for IT files - required since the file names will
// be the same as in the other stages. it's necessary to set the permissions to
// 0777 has shell scripts are executed as root, whereas Jenkins directives such
// as unstash are executed as jenkins
runMavenContainerCommand("mkdir -m 0777 -p ${itAgentUnstashDirectory}")
}
}
}
stage('Build Distribution') {
environment {
VERSION = readMavenPom(file: 'parent/pom.xml').getVersion()
}
steps {
timeout(time: shortTimeoutMinutes, unit: 'MINUTES') {
// since zbctl is included in zeebe-distribution.tar.gz, which is produced by
// maven, we have to build the go artifacts first
container('golang') {
sh '.ci/scripts/distribution/build-go.sh'
}
runMavenContainerCommand('.ci/scripts/distribution/build-java.sh')
// to simplify building the Docker image, we copy the distribution to a fixed
// filename that doesn't include the version
runMavenContainerCommand('cp dist/target/zeebe-distribution-*.tar.gz zeebe-distribution.tar.gz')
stash name: "zeebe-build", includes: "m2-repository/io/zeebe/*/${VERSION}/*"
stash name: "zeebe-distro", includes: "zeebe-distribution.tar.gz"
}
}
}
stage('Build Docker Images') {
environment {
DOCKER_BUILDKIT = "1"
IMAGE = "camunda/zeebe"
TAG = 'current-test'
}
steps {
timeout(time: shortTimeoutMinutes, unit: 'MINUTES') {
container('docker') {
sh '.ci/scripts/docker/build.sh'
// the hazelcast exporter is used by the BPMN TCK, and must therefore be
// built beforehand - if this ever becomes too slow, we can move this to a
// BPMN TCK agent and make this is a sequential stage prior to running the
// TCK
sh '.ci/scripts/docker/build_zeebe-hazelcast-exporter.sh'
}
}
}
}
stage('Verify') {
when { not { expression { params.SKIP_VERIFY } } }
parallel {
stage('Analyse') {
steps {
timeout(time: longTimeoutMinutes, unit: 'MINUTES') {
runMavenContainerCommand('.ci/scripts/distribution/analyse-java.sh')
}
}
}
stage('BPMN TCK') {
steps {
timeout(time: longTimeoutMinutes, unit: 'MINUTES') {
runMavenContainerCommand('.ci/scripts/distribution/test-tck.sh')
}
}
post {
always {
junit testResults: "bpmn-tck/**/*/TEST*.xml", keepLongStdio: true
}
}
}
stage('Test (Go)') {
steps {
timeout(time: longTimeoutMinutes, unit: 'MINUTES') {
container('golang') {
sh '.ci/scripts/distribution/test-go.sh'
}
}
}
post {
always {
junit testResults: "**/*/TEST-go.xml", keepLongStdio: true
}
}
}
stage('Test (Java)') {
environment {
SUREFIRE_REPORT_NAME_SUFFIX = 'java-testrun'
MAVEN_PARALLELISM = 2
SUREFIRE_FORK_COUNT = 6
JUNIT_THREAD_COUNT = 6
}
steps {
timeout(time: longTimeoutMinutes, unit: 'MINUTES') {
runMavenContainerCommand('.ci/scripts/distribution/test-java.sh')
runMavenContainerCommand('.ci/scripts/distribution/random-test-java.sh')
}
}
post {
always {
junit testResults: "**/*/TEST*${SUREFIRE_REPORT_NAME_SUFFIX}*.xml", keepLongStdio: true
}
}
}
stage('Test (Java 8)') {
environment {
SUREFIRE_REPORT_NAME_SUFFIX = 'java8-testrun'
}
steps {
timeout(time: longTimeoutMinutes, unit: 'MINUTES') {
runMavenContainerCommand('.ci/scripts/distribution/test-java8.sh', 'jdk8')
}
}
post {
always {
junit testResults: "**/*/TEST*${SUREFIRE_REPORT_NAME_SUFFIX}*.xml", keepLongStdio: true
}
}
}
stage('IT') {
// NOTE: all nested stages in the IT stage will be run on the following agent,
// identified by its label. Keep in mind that any artefacts produced in this
// agent will be unavailable in other agents, so you will need to copy them
// NOTE: agents (except the main one) are terminated once their stage is
// finished, so if you want to run several steps/stages in that agent they have
// to be sequential (since you cannot nest parallels/matrixes). if you need
// parallelism, consider spawning a sub-job
agent {
kubernetes {
cloud 'zeebe-ci'
label "zeebe-ci-build_${buildName}_it"
defaultContainer 'jnlp'
yaml templatePodspec('.ci/podSpecs/integration-test-template.yml')
}
}
stages {
stage('Prepare') {
steps {
timeout(time: shortTimeoutMinutes, unit: 'MINUTES') {
prepareMavenContainer()
unstash name: "zeebe-build"
runMavenContainerCommand('.ci/scripts/distribution/it-prepare.sh')
}
}
}
stage('Build Docker Image') {
environment {
DOCKER_BUILDKIT = "1"
IMAGE = "camunda/zeebe"
TAG = 'current-test'
}
steps {
timeout(time: shortTimeoutMinutes, unit: 'MINUTES') {
unstash name: "zeebe-distro"
container('docker') {
sh '.ci/scripts/docker/build.sh'
}
}
}
}
stage('Test') {
environment {
SUREFIRE_REPORT_NAME_SUFFIX = 'it-testrun'
MAVEN_PARALLELISM = 2
SUREFIRE_FORK_COUNT = 6
JUNIT_THREAD_COUNT = 6
}
steps {
timeout(time: longTimeoutMinutes, unit: 'MINUTES') {
runMavenContainerCommand('.ci/scripts/distribution/it-java.sh')
}
}
post {
always {
junit testResults: "**/*/TEST*${SUREFIRE_REPORT_NAME_SUFFIX}*.xml", keepLongStdio: true
stash allowEmpty: true, name: itFlakyTestStashName, includes: '**/FlakyTests.txt'
}
failure {
zip zipFile: 'test-reports-it.zip', archive: true, glob: "**/*/surefire-reports/**"
zip zipFile: 'test-errors-it.zip', archive: true, glob: "**/hs_err_*.log"
}
}
}
}
}
}
post {
always {
checkCodeCoverage()
}
failure {
zip zipFile: 'test-reports.zip', archive: true, glob: "**/*/surefire-reports/**"
zip zipFile: 'test-errors.zip', archive: true, glob: "**/hs_err_*.log"
dir(itAgentUnstashDirectory) {
unstash name: itFlakyTestStashName
}
script {
def flakeFiles = ['./FlakyTests.txt', "${itAgentUnstashDirectory}/FlakyTests.txt"]
def flakes = combineFlakeResults(flakeFiles)
if (flakes) {
currentBuild.description = "Flaky Tests: <br>" + flakes.join('<br>')
}
}
}
}
}
stage('QA') {
when {
anyOf {
expression { params.RUN_QA }
allOf {
branch developBranchName
triggeredBy 'TimerTrigger'
}
}
}
environment {
IMAGE = "gcr.io/zeebe-io/zeebe"
VERSION = readMavenPom(file: 'parent/pom.xml').getVersion()
TAG = "${env.VERSION}-${env.GIT_COMMIT}"
DOCKER_GCR = credentials("zeebe-gcr-serviceaccount-json")
ZEEBE_AUTHORIZATION_SERVER_URL = 'https://login.cloud.ultrawombat.com/oauth/token'
ZEEBE_CLIENT_ID = 'W5a4JUc3I1NIetNnodo3YTvdsRIFb12w'
QA_RUN_VARIABLES = "{\"zeebeImage\": \"${env.IMAGE}:${env.TAG}\", \"generationTemplate\": \"${params.GENERATION_TEMPLATE}\", " +
"\"channel\": \"Internal Dev\", \"branch\": \"${env.BRANCH_NAME}\", \"build\": \"${currentBuild.absoluteUrl}\", " +
"\"businessKey\": \"${currentBuild.absoluteUrl}\", \"processId\": \"qa-protocol\"}"
BUSINESS_KEY = "${currentBuild.absoluteUrl}"
}
steps {
container('docker') {
sh '.ci/scripts/docker/upload-gcr.sh'
}
container('maven') {
withVault(
[vaultSecrets:
[
[path : 'secret/common/ci-zeebe/testbench-secrets-int',
secretValues:
[
[envVar: 'ZEEBE_CLIENT_SECRET', vaultKey: 'clientSecret'],
[envVar: 'ZEEBE_ADDRESS', vaultKey: 'contactPoint'],
]
],
]
]
) {
sh '.ci/scripts/distribution/qa-testbench.sh'
}
}
}
post {
failure {
script {
currentBuild.description = 'Failure in QA Stage'
}
}
}
}
stage('Upload') {
when { allOf { branch developBranchName; not { triggeredBy 'TimerTrigger' } } }
steps {
retry(3) {
timeout(time: shortTimeoutMinutes, unit: 'MINUTES') {
runMavenContainerCommand('.ci/scripts/distribution/upload.sh')
}
}
}
}
stage('Post') {
when { not { triggeredBy 'TimerTrigger' } }
parallel {
stage('Docker') {
when { branch developBranchName }
environment {
VERSION = readMavenPom(file: 'parent/pom.xml').getVersion()
}
steps {
retry(3) {
timeout(time: shortTimeoutMinutes, unit: 'MINUTES') {
build job: 'zeebe-docker', parameters: [
string(name: 'BRANCH', value: env.BRANCH_NAME),
string(name: 'VERSION', value: env.VERSION),
booleanParam(name: 'IS_LATEST', value: isMasterBranch),
booleanParam(name: 'PUSH', value: isDevelopBranch)
]
}
}
}
}
}
}
}
post {
always {
// Retrigger the build if there were connection issues (but not
// on bors staging branch as bors does not recognize this result)
script {
if (agentDisconnected()) {
currentBuild.result = 'ABORTED'
currentBuild.description = "Aborted due to connection error"
if (!isBorsStagingBranch()) {
build job: currentBuild.projectName, propagate: false, quietPeriod: 60, wait: false
}
}
String userReason = null
if (currentBuild.description ==~ /.*Flaky Tests.*/) {
userReason = 'flaky-tests'
}
org.camunda.helper.CIAnalytics.trackBuildStatus(this, userReason)
}
}
failure {
script {
if (env.BRANCH_NAME != 'develop' || agentDisconnected()) {
return
}
sendZeebeSlackMessage()
}
}
changed {
script {
if (env.BRANCH_NAME != 'develop' || agentDisconnected()) {
return
}
if (currentBuild.currentResult == 'FAILURE') {
return // already handled above
}
if (!hasBuildResultChanged()) {
return
}
sendZeebeSlackMessage()
}
}
}
}
//////////////////// Helper functions ////////////////////
def getMavenContainerNameForJDK(String jdk = null) {
"maven${jdk ? '-' + jdk : ''}"
}
def prepareMavenContainer(String jdk = null) {
container(getMavenContainerNameForJDK(jdk)) {
sh '.ci/scripts/distribution/prepare.sh'
}
}
def runMavenContainerCommand(String shellCommand, String jdk = null) {
container(getMavenContainerNameForJDK(jdk)) {
configFileProvider([configFile(fileId: 'maven-nexus-settings-zeebe-local-repo', variable: 'MAVEN_SETTINGS_XML')]) {
sh shellCommand
}
}
}
// TODO: can be extracted to zeebe-jenkins-shared-library
def setHumanReadableBuildDisplayName(int maximumLength = 45) {
script {
commit_summary = sh([returnStdout: true, script: 'git show -s --format=%s']).trim()
displayNameFull = "#${env.BUILD_NUMBER}: ${commit_summary}"
if (displayNameFull.length() <= maximumLength) {
currentBuild.displayName = displayNameFull
} else {
displayStringHardTruncate = displayNameFull.take(maximumLength)
currentBuild.displayName = displayStringHardTruncate.take(displayStringHardTruncate.lastIndexOf(' '))
}
}
}
// TODO: can be extracted to zeebe-jenkins-shared-library
def sendZeebeSlackMessage() {
echo "Send slack message"
slackSend(
channel: "#zeebe-ci${jenkins.model.JenkinsLocationConfiguration.get()?.getUrl()?.contains('stage') ? '-stage' : ''}",
message: "Zeebe ${env.BRANCH_NAME} build ${currentBuild.absoluteUrl} changed status to ${currentBuild.currentResult}")
}
def isBorsStagingBranch() {
env.BRANCH_NAME == 'staging'
}
def templatePodspec(String podspecPath, flags = [:]) {
def defaultFlags = [
/* Criteria for using stable node pools:
* - staging branch: to have smooth staging builds and avoid unnecessary retries
* - params.RUN_QA: during QA stage the node must wait for the result. This can take several hours. Therefore a stable node is needed
* - env.isDevelopBranch: the core requirement is to have a stable node for nightly builds, which also rn QA (see above)
*/
useStableNodePool: isBorsStagingBranch() || params.RUN_QA || env.isDevelopBranch
]
// will merge Maps by overwriting left Map with values of the right Map
def effectiveFlags = defaultFlags + flags
def nodePoolName = "agents-n1-standard-32-netssd-${effectiveFlags.useStableNodePool ? 'stable' : 'preempt'}"
// Needs no workspace, see:
// https://www.jenkins.io/doc/pipeline/steps/workflow-multibranch/#readtrusted-read-trusted-file-from-scm
String templateString = readTrusted(podspecPath)
// Note: Templating is currently done via simple string substitution as this
// is enough to solve all existing use cases. If need arises the templating
// can be transparently changed to a more sophisticated YAML-merge based
// approach as it is an implementation detail that the caller does not know.
templateString = templateString.replaceAll('PODSPEC_TEMPLATE_NODE_POOL', nodePoolName)
templateString
}
def combineFlakeResults(flakeFiles = []) {
def flakes = []
for (flakeFile in flakeFiles) {
if (fileExists(flakeFile)) {
flakes += readFile(flakeFile).split('\n')
}
}
return flakes
}
def checkCodeCoverage() {
jacoco(
execPattern: '**/*.exec',
classPattern: '**/target/classes',
sourcePattern: '**/src/main/java,**/generated-sources/protobuf/java,**/generated-sources/assertj-assertions,**/generated-sources/sbe',
exclusionPattern: '**/io/zeebe/gateway/protocol/**,'
+ '**/*Encoder.class,**/*Decoder.class,**/MetaAttribute.class,'
+ '**/io/zeebe/protocol/record/**/*Assert.class,**/io/zeebe/protocol/record/Assertions.class,', // classes from generated resources
runAlways: true
)
zip zipFile: "test-coverage-reports.zip", archive: true, glob: '**/target/site/jacoco/**'
}