forked from elastic/apm-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
507 lines (504 loc) · 18.7 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
#!/usr/bin/env groovy
@Library('apm@current') _
pipeline {
agent { label 'linux && immutable' }
environment {
REPO = 'apm-server'
BASE_DIR = "src/github.com/elastic/${env.REPO}"
NOTIFY_TO = credentials('notify-to')
JOB_GCS_BUCKET = credentials('gcs-bucket')
JOB_GCS_CREDENTIALS = 'apm-ci-gcs-plugin'
CODECOV_SECRET = 'secret/apm-team/ci/apm-server-codecov'
ITS_PIPELINE = 'apm-integration-tests-selector-mbp/master'
DIAGNOSTIC_INTERVAL = "${params.DIAGNOSTIC_INTERVAL}"
ES_LOG_LEVEL = "${params.ES_LOG_LEVEL}"
DOCKER_SECRET = 'secret/apm-team/ci/docker-registry/prod'
DOCKER_REGISTRY = 'docker.elastic.co'
DOCKER_IMAGE = "${env.DOCKER_REGISTRY}/observability-ci/apm-server"
}
options {
timeout(time: 2, unit: 'HOURS')
buildDiscarder(logRotator(numToKeepStr: '100', artifactNumToKeepStr: '30', daysToKeepStr: '30'))
timestamps()
ansiColor('xterm')
disableResume()
durabilityHint('PERFORMANCE_OPTIMIZED')
rateLimitBuilds(throttle: [count: 60, durationName: 'hour', userBoost: true])
quietPeriod(10)
}
triggers {
issueCommentTrigger('(?i).*(?:jenkins\\W+)?run\\W+(?:the\\W+)?(?:hey-apm|package\\W+)?tests(?:\\W+please)?.*')
}
parameters {
booleanParam(name: 'Run_As_Master_Branch', defaultValue: false, description: 'Allow to run any steps on a PR, some steps normally only run on master branch.')
booleanParam(name: 'linux_ci', defaultValue: true, description: 'Enable Linux build')
booleanParam(name: 'osx_ci', defaultValue: true, description: 'Enable OSX CI')
booleanParam(name: 'windows_ci', defaultValue: true, description: 'Enable Windows CI')
booleanParam(name: 'intake_ci', defaultValue: true, description: 'Enable test')
booleanParam(name: 'test_ci', defaultValue: true, description: 'Enable test')
booleanParam(name: 'test_sys_env_ci', defaultValue: true, description: 'Enable system and environment test')
booleanParam(name: 'bench_ci', defaultValue: true, description: 'Enable benchmarks')
booleanParam(name: 'release_ci', defaultValue: true, description: 'Enable build the release packages')
booleanParam(name: 'kibana_update_ci', defaultValue: true, description: 'Enable build the Check kibana Obj. Updated')
booleanParam(name: 'its_ci', defaultValue: true, description: 'Enable async ITs')
string(name: 'DIAGNOSTIC_INTERVAL', defaultValue: "0", description: 'Elasticsearch detailed logging every X seconds')
string(name: 'ES_LOG_LEVEL', defaultValue: "error", description: 'Elasticsearch error level')
}
stages {
/**
Checkout the code and stash it, to use it on other stages.
*/
stage('Checkout') {
environment {
PATH = "${env.PATH}:${env.WORKSPACE}/bin"
HOME = "${env.WORKSPACE}"
GOPATH = "${env.WORKSPACE}"
}
options { skipDefaultCheckout() }
steps {
pipelineManager([ cancelPreviousRunningBuilds: [ when: 'PR' ] ])
deleteDir()
gitCheckout(basedir: "${BASE_DIR}", githubNotifyFirstTimeContributor: true,
shallow: false, reference: "/var/lib/jenkins/.git-references/${REPO}.git")
stash allowEmpty: true, name: 'source', useDefaultExcludes: false
script {
dir("${BASE_DIR}"){
env.GO_VERSION = readFile(".go-version").trim()
def regexps =[
"^_beats.*",
"^apm-server.yml",
"^apm-server.docker.yml",
"^magefile.go",
"^ingest.*",
"^packaging.*",
"^tests/packaging.*",
"^vendor/github.com/elastic/beats.*"
]
env.BEATS_UPDATED = isGitRegionMatch(patterns: regexps)
// Skip all the stages except docs for PR's with asciidoc changes only
env.ONLY_DOCS = isGitRegionMatch(patterns: [ '.*\\.asciidoc' ], comparator: 'regexp', shouldMatchAll: true)
}
}
}
}
/**
Updating generated files for Beat.
Checks the GO environment.
Checks the Python environment.
Checks YAML files are generated.
Validate that all updates were committed.
*/
stage('Intake') {
options { skipDefaultCheckout() }
environment {
PATH = "${env.PATH}:${env.WORKSPACE}/bin"
HOME = "${env.WORKSPACE}"
GOPATH = "${env.WORKSPACE}"
}
when {
beforeAgent true
allOf {
expression { return params.intake_ci }
expression { return env.ONLY_DOCS == "false" }
}
}
steps {
withGithubNotify(context: 'Intake') {
deleteDir()
unstash 'source'
dir("${BASE_DIR}"){
sh(label: 'Run intake', script: './script/jenkins/intake.sh')
}
}
}
}
stage('Build and Test'){
failFast false
parallel {
/**
Build on a linux environment.
*/
stage('linux build') {
options { skipDefaultCheckout() }
when {
beforeAgent true
allOf {
expression { return params.linux_ci }
expression { return env.ONLY_DOCS == "false" }
}
}
steps {
withGithubNotify(context: 'Build - Linux') {
deleteDir()
unstash 'source'
golang(){
dir(BASE_DIR){
retry(2) { // Retry in case there are any errors to avoid temporary glitches
sleep randomNumber(min: 5, max: 10)
sh(label: 'Linux build', script: './script/jenkins/build.sh')
}
}
}
}
}
}
/**
Build and Test on a windows environment.
*/
stage('windows build-test') {
agent { label 'windows-2019-immutable' }
options {
skipDefaultCheckout()
warnError('Windows execution failed')
}
when {
beforeAgent true
allOf {
expression { return params.windows_ci }
expression { return env.ONLY_DOCS == "false" }
}
}
steps {
withGithubNotify(context: 'Build-Test - Windows') {
deleteDir()
unstash 'source'
dir(BASE_DIR){
retry(2) { // Retry in case there are any errors to avoid temporary glitches
sleep randomNumber(min: 5, max: 10)
powershell(label: 'Windows build', script: '.\\script\\jenkins\\windows-build.ps1')
powershell(label: 'Run Window tests', script: '.\\script\\jenkins\\windows-test.ps1')
}
}
}
}
post {
always {
junit(allowEmptyResults: true,
keepLongStdio: true,
testResults: "${BASE_DIR}/build/junit-report.xml,${BASE_DIR}/build/TEST-*.xml")
}
}
}
/**
Build on a mac environment.
*/
stage('OSX build-test') {
agent { label 'macosx' }
options {
skipDefaultCheckout()
warnError('OSX execution failed')
}
when {
beforeAgent true
allOf {
expression { return params.osx_ci }
expression { return env.ONLY_DOCS == "false" }
}
}
environment {
HOME = "${env.WORKSPACE}"
}
steps {
withGithubNotify(context: 'Build-Test - OSX') {
deleteDir()
unstash 'source'
dir(BASE_DIR){
retry(2) { // Retry in case there are any errors to avoid temporary glitches
sleep randomNumber(min: 5, max: 10)
sh(label: 'OSX build', script: '.ci/scripts/build-darwin.sh')
sh(label: 'Run Unit tests', script: '.ci/scripts/test-darwin.sh')
}
}
}
}
post {
always {
junit(allowEmptyResults: true, keepLongStdio: true, testResults: "${BASE_DIR}/build/junit-*.xml")
}
}
}
/**
Run unit tests and report junit results.
*/
stage('Unit Test') {
agent { label 'linux && immutable' }
options { skipDefaultCheckout() }
environment {
PATH = "${env.PATH}:${env.WORKSPACE}/bin"
HOME = "${env.WORKSPACE}"
GOPATH = "${env.WORKSPACE}"
}
when {
beforeAgent true
allOf {
expression { return params.test_ci }
expression { return env.ONLY_DOCS == "false" }
}
}
steps {
withGithubNotify(context: 'Unit Tests', tab: 'tests') {
deleteDir()
unstash 'source'
dir("${BASE_DIR}"){
sh(label: 'Run Unit tests', script: './script/jenkins/unit-test.sh')
}
}
}
post {
always {
coverageReport("${BASE_DIR}/build/coverage")
junit(allowEmptyResults: true,
keepLongStdio: true,
testResults: "${BASE_DIR}/build/junit-*.xml"
)
catchError(buildResult: 'SUCCESS', message: 'Failed to grab test results tar files', stageResult: 'SUCCESS') {
tar(file: "coverage-files.tgz", archive: true, dir: "coverage", pathPrefix: "${BASE_DIR}/build")
}
codecov(repo: env.REPO, basedir: "${BASE_DIR}", secret: "${CODECOV_SECRET}")
}
}
}
/**
Runs System and Environment Tests, then generate coverage and unit test reports.
Finally archive the results.
*/
stage('System and Environment Tests') {
agent { label 'linux && immutable' }
options { skipDefaultCheckout() }
environment {
PATH = "${env.PATH}:${env.WORKSPACE}/bin"
HOME = "${env.WORKSPACE}"
GOPATH = "${env.WORKSPACE}"
}
when {
beforeAgent true
allOf {
expression { return params.test_sys_env_ci }
expression { return env.ONLY_DOCS == "false" }
}
}
steps {
withGithubNotify(context: 'System Tests', tab: 'tests') {
deleteDir()
unstash 'source'
dir("${BASE_DIR}"){
sh(label: 'Run Linux tests', script: './script/jenkins/linux-test.sh')
}
}
}
post {
always {
dir("${BASE_DIR}"){
archiveArtifacts(allowEmptyArchive: true,
artifacts: "docker-info/**",
defaultExcludes: false)
junit(allowEmptyResults: true,
keepLongStdio: true,
testResults: "**/build/TEST-*.xml"
)
}
catchError(buildResult: 'SUCCESS', message: 'Failed to grab test results tar files', stageResult: 'SUCCESS') {
tar(file: "system-tests-linux-files.tgz", archive: true, dir: "system-tests", pathPrefix: "${BASE_DIR}/build")
}
}
}
}
/**
Runs benchmarks on the current version and compare it with the previous ones.
Finally archive the results.
*/
stage('Benchmarking') {
agent { label 'linux && immutable' }
options { skipDefaultCheckout() }
when {
beforeAgent true
allOf {
anyOf {
branch 'master'
branch pattern: '\\d+\\.\\d+', comparator: 'REGEXP'
branch pattern: 'v\\d?', comparator: 'REGEXP'
tag pattern: 'v\\d+\\.\\d+\\.\\d+.*', comparator: 'REGEXP'
expression { return params.Run_As_Master_Branch }
}
expression { return params.bench_ci }
expression { return env.ONLY_DOCS == "false" }
}
}
steps {
withGithubNotify(context: 'Benchmarking') {
deleteDir()
unstash 'source'
golang(){
dir("${BASE_DIR}"){
sh(label: 'Run benchmarks', script: './script/jenkins/bench.sh')
sendBenchmarks(file: 'bench.out', index: "benchmark-server")
}
}
}
}
}
/**
Checks if kibana objects are updated.
*/
stage('Check kibana Obj. Updated') {
agent { label 'linux && immutable' }
options { skipDefaultCheckout() }
environment {
PATH = "${env.PATH}:${env.WORKSPACE}/bin"
HOME = "${env.WORKSPACE}"
GOPATH = "${env.WORKSPACE}"
}
when {
beforeAgent true
allOf {
expression { return params.kibana_update_ci }
expression { return env.ONLY_DOCS == "false" }
}
}
steps {
withGithubNotify(context: 'Sync Kibana') {
deleteDir()
unstash 'source'
dir("${BASE_DIR}"){
catchError(buildResult: 'SUCCESS', message: 'Sync Kibana is not updated', stageResult: 'UNSTABLE') {
sh(label: 'Test Sync', script: './script/jenkins/sync.sh')
}
}
}
}
}
stage('Hey-Apm') {
agent { label 'linux && immutable' }
options { skipDefaultCheckout() }
when {
beforeAgent true
expression { return env.GITHUB_COMMENT?.contains('hey-apm tests') }
}
steps {
withGithubNotify(context: 'Hey-Apm') {
deleteDir()
unstash 'source'
golang(){
dockerLogin(secret: env.DOCKER_SECRET, registry: env.DOCKER_REGISTRY)
dir("${BASE_DIR}"){
sh(label: 'Package & Push', script: "./script/jenkins/package-docker-snapshot.sh ${env.GIT_BASE_COMMIT} ${env.DOCKER_IMAGE}")
}
}
build(job: 'apm-server/apm-hey-test-benchmark', propagate: true, wait: true,
parameters: [string(name: 'GO_VERSION', value: '1.12.1'),
string(name: 'STACK_VERSION', value: "${env.GIT_BASE_COMMIT}"),
string(name: 'APM_DOCKER_IMAGE', value: "${env.DOCKER_IMAGE}")])
}
}
}
stage('Package') {
agent { label 'linux && immutable' }
options { skipDefaultCheckout() }
environment {
PATH = "${env.PATH}:${env.WORKSPACE}/bin"
HOME = "${env.WORKSPACE}"
GOPATH = "${env.WORKSPACE}"
SNAPSHOT = "true"
}
when {
beforeAgent true
allOf {
expression { return params.release_ci }
expression { return env.ONLY_DOCS == "false" }
anyOf {
expression { return env.BEATS_UPDATED != "false" }
expression { return env.GITHUB_COMMENT?.contains('package tests') }
}
}
}
stages {
stage('Package') {
steps {
withGithubNotify(context: 'Package') {
deleteDir()
unstash 'source'
golang(){
dir("${BASE_DIR}"){
sh(label: 'Build packages', script: './script/jenkins/package.sh')
sh(label: 'Test packages install', script: './script/jenkins/test-install-packages.sh')
dockerLogin(secret: env.DOCKER_SECRET, registry: env.DOCKER_REGISTRY)
sh(label: 'Package & Push', script: "./script/jenkins/package-docker-snapshot.sh ${env.GIT_BASE_COMMIT} ${env.DOCKER_IMAGE}")
}
}
}
}
}
stage('Publish') {
when {
beforeAgent true
anyOf {
branch 'master'
branch pattern: '\\d+\\.\\d+', comparator: 'REGEXP'
branch pattern: 'v\\d?', comparator: 'REGEXP'
tag pattern: 'v\\d+\\.\\d+\\.\\d+.*', comparator: 'REGEXP'
expression { return params.Run_As_Master_Branch }
expression { return env.BEATS_UPDATED != "false" }
}
}
steps {
googleStorageUpload(bucket: "gs://${JOB_GCS_BUCKET}/snapshots",
credentialsId: "${JOB_GCS_CREDENTIALS}",
pathPrefix: "${BASE_DIR}/build/distributions/",
pattern: "${BASE_DIR}/build/distributions/**/*",
sharedPublicly: true,
showInline: true)
}
}
}
}
stage('APM Integration Tests') {
agent { label 'linux && immutable' }
options { skipDefaultCheckout() }
when {
beforeAgent true
allOf {
anyOf {
changeRequest()
expression { return !params.Run_As_Master_Branch }
}
expression { return params.its_ci }
expression { return env.ONLY_DOCS == "false" }
}
}
steps {
withGithubNotify(context: 'APM Integration Tests') {
script {
def buildObject = build(job: env.ITS_PIPELINE, propagate: false, wait: true,
parameters: [string(name: 'INTEGRATION_TEST', value: 'All'),
string(name: 'BUILD_OPTS', value: "--apm-server-build https://github.com/elastic/${env.REPO}@${env.GIT_BASE_COMMIT}")])
copyArtifacts(projectName: env.ITS_PIPELINE, selector: specific(buildNumber: buildObject.number.toString()))
}
}
}
post {
always {
junit(testResults: "**/*-junit*.xml", allowEmptyResults: true, keepLongStdio: true)
}
}
}
}
}
}
post {
cleanup {
notifyBuildResult()
}
}
}
def golang(Closure body){
def golangDocker
retry(3) { // Retry in case there are any errors when building the docker images (to avoid temporary glitches)
sleep randomNumber(min: 2, max: 5)
golangDocker = docker.build('golang-mage', "--build-arg GO_VERSION=${GO_VERSION} -f ${BASE_DIR}/.ci/docker/golang-mage/Dockerfile ${BASE_DIR}")
}
withEnv(["HOME=${WORKSPACE}", "GOPATH=${WORKSPACE}", "SHELL=/bin/bash"]) {
golangDocker.inside('-v /usr/bin/docker:/usr/bin/docker -v /var/run/docker.sock:/var/run/docker.sock'){
body()
}
}
}