forked from elastic/apm-agent-rum-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
489 lines (478 loc) · 17 KB
/
Jenkinsfile
File metadata and controls
489 lines (478 loc) · 17 KB
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
#!/usr/bin/env groovy
@Library('apm@current') _
pipeline {
agent { label 'linux && immutable' }
environment {
REPO = 'apm-agent-rum-js'
BASE_DIR = "src/github.com/elastic/${env.REPO}"
NOTIFY_TO = credentials('notify-to')
JOB_GCS_BUCKET = credentials('gcs-bucket')
PIPELINE_LOG_LEVEL='INFO'
CODECOV_SECRET = 'secret/apm-team/ci/apm-agent-rum-codecov'
SAUCELABS_SECRET_CORE = 'secret/apm-team/ci/apm-agent-rum-saucelabs@elastic/apm-rum-core'
SAUCELABS_SECRET = 'secret/apm-team/ci/apm-agent-rum-saucelabs@elastic/apm-rum'
DOCKER_ELASTIC_SECRET = 'secret/apm-team/ci/docker-registry/prod'
GITHUB_CHECK_ITS_NAME = 'Integration Tests'
ITS_PIPELINE = 'apm-integration-tests-selector-mbp/master'
OPBEANS_REPO = 'opbeans-frontend'
NPMRC_SECRET = 'secret/jenkins-ci/npmjs/elasticmachine'
TOTP_SECRET = 'totp/code/npmjs-elasticmachine'
PATH = "${env.PATH}:${env.WORKSPACE}/bin"
MODE = "none"
}
options {
//timeout(time: 3, unit: 'HOURS')
buildDiscarder(logRotator(numToKeepStr: '20', artifactNumToKeepStr: '20', 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+)?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: 'saucelab_test', defaultValue: "true", description: "Enable run a Sauce lab test")
booleanParam(name: 'parallel_test', defaultValue: "true", description: "Enable run tests in parallel")
booleanParam(name: 'bench_ci', defaultValue: true, description: 'Enable benchmarks')
booleanParam(name: 'release', defaultValue: false, description: 'Release.')
}
stages {
stage('Initializing'){
options { skipDefaultCheckout() }
stages {
/**
Checkout the code and stash it, to use it on other stages.
*/
stage('Checkout') {
steps {
pipelineManager([ cancelPreviousRunningBuilds: [ when: 'PR' ] ])
deleteDir()
gitCheckout(basedir: "${BASE_DIR}", githubNotifyFirstTimeContributor: true)
stash allowEmpty: true, name: 'source', useDefaultExcludes: false
script {
dir("${BASE_DIR}"){
// Look for changes related to the benchmark, if so then set the env variable.
def patternList =[
'^packages/.*/test/benchmarks/.*',
'^scripts/benchmarks.js',
'^packages/rum-core/karma.bench.conf.js'
]
env.BENCHMARK_UPDATED = isGitRegionMatch(patterns: patternList)
// Skip all the stages except docs for PR's with asciidoc/md changes only
env.ONLY_DOCS = isGitRegionMatch(patterns: [ '.*\\.(asciidoc|md)' ], shouldMatchAll: true)
}
}
}
}
/**
Lint the code.
*/
stage('Lint') {
when {
beforeAgent true
expression { return env.ONLY_DOCS == "false" }
}
steps {
withGithubNotify(context: 'Lint') {
deleteDir()
unstash 'source'
script{
docker.image('node:8').inside(){
dir("${BASE_DIR}"){
sh(label: "Lint", script: 'HOME=$(pwd) .ci/scripts/lint.sh')
bundlesize()
}
stash allowEmpty: true, name: 'cache', includes: "${BASE_DIR}/.npm/**", useDefaultExcludes: false
}
}
}
}
}
stage('Test Pupperteer') {
when {
beforeAgent true
expression { return env.ONLY_DOCS == "false" }
}
matrix {
agent { label 'linux && immutable' }
axes {
axis {
name 'STACK_VERSION'
values (
'8.0.0-SNAPSHOT',
'7.6.0',
'7.0.0'
)
}
axis {
name 'SCOPE'
values (
'@elastic/apm-rum',
'@elastic/apm-rum-core',
'@elastic/apm-rum-react',
'@elastic/apm-rum-angular',
'@elastic/apm-rum-vue',
)
}
}
stages {
stage('Scope Test') {
steps {
runTest()
}
}
}
post {
cleanup {
wrappingUp()
}
}
}
}
stage('Stack 8.0.0-SNAPSHOT SauceLabs') {
agent { label 'linux && immutable' }
environment {
SAUCELABS_SECRET = "${env.SAUCELABS_SECRET_CORE}"
STACK_VERSION = "8.0.0-SNAPSHOT"
MODE = "saucelabs"
}
when {
allOf {
branch 'master'
expression { return params.saucelab_test }
expression { return env.ONLY_DOCS == "false" }
}
}
steps {
runAllScopes()
}
post {
cleanup {
wrappingUp()
}
}
}
stage('Stack 8.0.0-SNAPSHOT SauceLabs PR') {
agent { label 'linux && immutable' }
environment {
SAUCELABS_SECRET="${env.SAUCELABS_SECRET}"
STACK_VERSION="8.0.0-SNAPSHOT"
MODE = "saucelabs"
}
when {
allOf {
changeRequest()
expression { return params.saucelab_test }
expression { return env.ONLY_DOCS == "false" }
}
}
steps {
runAllScopes()
}
post {
cleanup {
wrappingUp()
}
}
}
stage('Integration Tests') {
agent none
when {
beforeAgent true
allOf {
anyOf {
changeRequest()
expression { return !params.Run_As_Master_Branch }
}
expression { return env.ONLY_DOCS == "false" }
}
}
steps {
build(job: env.ITS_PIPELINE, propagate: false, wait: false,
parameters: [string(name: 'INTEGRATION_TEST', value: 'RUM'),
string(name: 'BUILD_OPTS', value: "--rum-agent-branch ${env.GIT_BASE_COMMIT}"),
string(name: 'GITHUB_CHECK_NAME', value: env.GITHUB_CHECK_ITS_NAME),
string(name: 'GITHUB_CHECK_REPO', value: env.REPO),
string(name: 'GITHUB_CHECK_SHA1', value: env.GIT_BASE_COMMIT)])
githubNotify(context: "${env.GITHUB_CHECK_ITS_NAME}", description: "${env.GITHUB_CHECK_ITS_NAME} ...", status: 'PENDING', targetUrl: "${env.JENKINS_URL}search/?q=${env.ITS_PIPELINE.replaceAll('/','+')}")
}
}
/**
Run Benchmarks and send the results to ES.
*/
stage('Benchmarks') {
agent { label 'metal' }
environment {
REPORT_FILE = 'apm-agent-benchmark-results.json'
}
when {
beforeAgent true
allOf {
anyOf {
branch 'master'
tag pattern: 'v\\d+\\.\\d+\\.\\d+.*', comparator: 'REGEXP'
expression { return params.Run_As_Master_Branch }
expression { return env.BENCHMARK_UPDATED != "false" }
}
expression { return params.bench_ci }
expression { return env.ONLY_DOCS == "false" }
}
}
steps {
withGithubNotify(context: 'Benchmarks') {
deleteDir()
unstash 'source'
unstash 'cache'
dockerLogin(secret: "${DOCKER_ELASTIC_SECRET}", registry: 'docker.elastic.co')
dir("${BASE_DIR}") {
sh './.ci/scripts/benchmarks.sh'
}
}
}
post {
always {
archiveArtifacts(allowEmptyArchive: true, artifacts: "${BASE_DIR}/${env.REPORT_FILE}", onlyIfSuccessful: false)
catchError(message: 'sendBenchmarks failed', buildResult: 'FAILURE') {
log(level: 'INFO', text: "sendBenchmarks is ${env.CHANGE_ID?.trim() ? 'not enabled for PRs' : 'enabled for branches'}")
whenTrue(env.CHANGE_ID == null){
sendBenchmarks(file: "${BASE_DIR}/${env.REPORT_FILE}", index: 'benchmark-rum-js')
}
}
catchError(message: 'deleteDir failed', buildResult: 'SUCCESS', stageResult: 'UNSTABLE') {
deleteDir()
}
}
}
}
/**
Execute code coverage only once.
*/
stage('Coverage') {
options { skipDefaultCheckout() }
when {
beforeAgent true
expression { return env.ONLY_DOCS == "false" }
}
steps {
withGithubNotify(context: 'Coverage') {
// No scope is required as the coverage should run for all of them
runScript(label: 'coverage', stack: '7.0.0', scope: '', goal: 'coverage')
codecov(repo: env.REPO, basedir: "${env.BASE_DIR}", secret: "${env.CODECOV_SECRET}")
}
}
post {
always {
coverageReport("${BASE_DIR}/packages/**")
publishCoverage(adapters: [coberturaAdapter("${BASE_DIR}/packages/**/coverage-*-report.xml")],
sourceFileResolver: sourceFiles('STORE_ALL_BUILD'))
}
}
}
stage('Release') {
options {
skipDefaultCheckout()
timeout(time: 12, unit: 'HOURS')
}
environment {
HOME = "${env.WORKSPACE}"
}
when {
beforeAgent true
allOf {
branch 'master'
expression { return params.release }
}
}
stages {
stage('Notify') {
options { skipDefaultCheckout() }
steps {
deleteDir()
unstash 'source'
dir("${BASE_DIR}") {
prepareRelease() {
script {
sh(label: 'Lerna version dry-run', script: 'npx lerna version --no-push --yes', returnStdout: true)
def releaseVersions = sh(label: 'Gather versions from last commit', script: 'git log -1 --format="%b"', returnStdout: true)
log(level: 'INFO', text: "Versions: ${releaseVersions}")
emailext subject: "[${env.REPO}] Release ready to be pushed", to: "${NOTIFY_TO}",
body: "Please go to ${env.BUILD_URL}input to approve or reject within 12 hours.\n Changes: ${releaseVersions}"
input(message: 'Should we release a new version?', ok: 'Yes, we should.',
parameters: [text(description: 'Look at the versions to be released. They cannot be edited here',
defaultValue: "${releaseVersions}", name: 'versions')])
}
}
}
}
}
stage('Release CI') {
options { skipDefaultCheckout() }
steps {
deleteDir()
unstash 'source'
dir("${BASE_DIR}") {
prepareRelease() {
sh 'npm run release-ci'
}
}
}
post {
always {
script {
currentBuild.description = "${currentBuild.description?.trim() ? currentBuild.description : ''} released"
}
}
}
}
}
}
stage('Opbeans') {
options { skipDefaultCheckout() }
when {
beforeAgent true
tag pattern: '@elastic/apm-rum@\\d+\\.\\d+\\.\\d+$', comparator: 'REGEXP'
}
environment {
REPO_NAME = "${OPBEANS_REPO}"
}
steps {
deleteDir()
dir("${OPBEANS_REPO}"){
git credentialsId: 'f6c7695a-671e-4f4f-a331-acdce44ff9ba',
url: "git@github.com:elastic/${OPBEANS_REPO}.git"
sh script: ".ci/bump-version.sh '${env.BRANCH_NAME}'", label: 'Bump version'
// The opbeans pipeline will trigger a release for the master branch
gitPush()
// The opbeans pipeline will trigger a release for the release tag
gitCreateTag(tag: "${env.BRANCH_NAME}")
}
}
}
}
}
}
post {
cleanup {
notifyBuildResult()
}
}
}
def runScript(Map args = [:]){
def stack = args.stack
def scope = args.scope
def label = args.label
def goal = args.get('goal', 'test')
deleteDir()
unstash 'source'
unstash 'cache'
dockerLogin(secret: "${DOCKER_ELASTIC_SECRET}", registry: "docker.elastic.co")
dir("${BASE_DIR}"){
withEnv([
"STACK_VERSION=${stack}",
"SCOPE=${scope}",
"APM_SERVER_URL=http://apm-server:8200",
"APM_SERVER_PORT=8200",
"GOAL=${goal}"]) {
retry(2) {
sleep randomNumber(min: 5, max: 10)
sh(label: 'Pull and build docker infra', script: '.ci/scripts/pull_and_build.sh')
}
// Another retry in case there are any environmental issues
retry(3) {
sleep randomNumber(min: 5, max: 10)
if(env.MODE == 'saucelabs'){
withSaucelabsEnv(){
sh(label: "Start Elastic Stack ${stack} - ${scope} - ${env.MODE}", script: '.ci/scripts/test.sh')
}
} else {
sh(label: "Start Elastic Stack ${stack} - ${scope} - ${env.MODE}", script: '.ci/scripts/test.sh')
}
}
}
}
}
def withSaucelabsEnv(Closure body){
def jsonValue = getVaultSecret(secret: "${SAUCELABS_SECRET}")
withEnvMask(vars: [
[var: 'SAUCE_USERNAME', password: "${jsonValue.data.SAUCE_USERNAME}"],
[var: 'SAUCE_ACCESS_KEY', password: "${jsonValue.data.SAUCE_ACCESS_KEY}"],
]){
try { //https://issues.jenkins-ci.org/browse/JENKINS-61034
timeout(activity: true, time: 300, unit: 'SECONDS') { //SauceLab uncatch exceptions
body()
}
} catch(err){
error("The test failed")
}
}
}
def bundlesize(){
catchError(buildResult: 'SUCCESS', message: 'Bundlesize report issues', stageResult: 'UNSTABLE') {
sh(label: "Bundlesize", script: '''#!/bin/bash
set -o pipefail
npm run bundlesize|tee bundlesize.txt
''')
}
catchError(buildResult: 'SUCCESS', message: 'Bundlesize report issues', stageResult: 'UNSTABLE') {
sh(label: "Process Bundlesize out", script: '''#!/bin/bash
grep -e "\\(FAIL\\|PASS\\)" bundlesize.txt|cut -d ":" -f 2 >bundlesize-lines.txt
''')
def file = readFile(file: "bundlesize-lines.txt")
file?.split("\n").each { line ->
withGithubNotify(context: "Bundle Size :${line}"){
echo "Bundle Size :${line}"
}
}
}
}
def wrappingUp(){
junit(allowEmptyResults: true,
keepLongStdio: true,
testResults: "${env.BASE_DIR}/packages/**/reports/TESTS-*.xml")
archiveArtifacts(allowEmptyArchive: true, artifacts: "${env.BASE_DIR}/.npm/_logs,${env.BASE_DIR}/packages/**/reports/TESTS-*.xml")
}
def prepareRelease(String nodeVersion='node:lts', Closure body){
withNpmrc(secret: "${env.NPMRC_SECRET}", path: "${env.WORKSPACE}/${env.BASE_DIR}") {
withCredentials([usernamePassword(credentialsId: '2a9602aa-ab9f-4e52-baf3-b71ca88469c7-UserAndToken',
passwordVariable: 'GITHUB_TOKEN', usernameVariable: 'GITHUB_USER')]) {
docker.image(nodeVersion).inside(){
withEnv(["HOME=${env.WORKSPACE}/${env.BASE_DIR}"]) {
sh '.ci/scripts/prepare-git-context.sh'
sh 'npm ci'
withTotpVault(secret: "${env.TOTP_SECRET}", code_var_name: 'TOTP_CODE'){
body()
}
}
}
}
}
}
def runAllScopes(){
def scopes = [
'SCOPE=@elastic/apm-rum-core',
'SCOPE=@elastic/apm-rum',
'SCOPE=@elastic/apm-rum-react',
'SCOPE=@elastic/apm-rum-angular',
'SCOPE=@elastic/apm-rum-vue'
]
scopes.each{ s ->
withEnv([s]){
runTest()
}
}
}
def runTest(){
def mode = env.MODE == 'none' ? 'Puppeteer' : env.MODE
withGithubNotify(context: "Test ${SCOPE} - ${STACK_VERSION} - ${mode}", tab: 'tests') {
runScript(
label: "${SCOPE}",
stack: "${STACK_VERSION}",
scope: "${SCOPE}",
goal: 'test'
)
}
}