This repository has been archived by the owner on Sep 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Jenkinsfile
364 lines (349 loc) · 16.3 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
#!/usr/bin/env groovy
@Library('apm@current') _
pipeline {
agent any
environment {
REPO = 'e2e-testing'
BASE_DIR = "src/github.com/elastic/${env.REPO}"
ELASTIC_APM_ACTIVE="true"
ELASTIC_APM_ENVIRONMENT="ci"
ELASTIC_APM_LOG_FILE="stderr"
ELASTIC_APM_LOG_LEVEL="debug"
VAULT_INSTRUMENTATION_SECRET="secret/observability-team/ci/jenkins-stats"
NIGHTLY_TAG="@nightly"
NOTIFY_TO = credentials('notify-to')
JOB_GCS_BUCKET = credentials('gcs-bucket')
JOB_GIT_CREDENTIALS = "2a9602aa-ab9f-4e52-baf3-b71ca88469c7-UserAndToken"
DOCKER_ELASTIC_SECRET = 'secret/observability-team/ci/docker-registry/prod'
DOCKER_REGISTRY = 'docker.elastic.co'
}
options {
timeout(time: 1, 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: "SKIP_SCENARIOS", defaultValue: true, description: "If it's needed to skip those scenarios marked as @skip. Default true")
booleanParam(name: "NIGHTLY_SCENARIOS", defaultValue: false, description: "If it's needed to include the scenarios marked as @nightly in the test execution. Default false")
string(name: 'runTestsSuites', defaultValue: '', description: 'A comma-separated list of test suites to run (default: empty to run all test suites)')
booleanParam(name: "forceSkipGitChecks", defaultValue: false, description: "If it's needed to check for Git changes to filter by modified sources")
booleanParam(name: "forceSkipPresubmit", defaultValue: false, description: "If it's needed to execute the pre-submit tests: unit and precommit.")
booleanParam(name: "notifyOnGreenBuilds", defaultValue: false, description: "If it's needed to notify to Slack with green builds.")
string(name: 'SLACK_CHANNEL', defaultValue: 'observablt-bots', description: 'The Slack channel(s) where errors will be posted. For multiple channels, use a comma-separated list of channels')
string(name: 'ELASTIC_AGENT_DOWNLOAD_URL', defaultValue: '', description: 'If present, it will override the download URL for the Elastic agent artifact. (I.e. https://snapshots.elastic.co/8.0.0-59098054/downloads/beats/elastic-agent/elastic-agent-8.0.0-SNAPSHOT-linux-x86_64.tar.gz')
string(name: 'BEAT_VERSION', defaultValue: '8.0.0-SNAPSHOT', description: 'SemVer version of the Beat to be used for the tests. You can use here the tag of your PR to test your changes')
string(name: 'ELASTIC_AGENT_STALE_VERSION', defaultValue: '7.10-SNAPSHOT', description: 'SemVer version of the stale stand-alone elastic-agent to be used for Fleet upgrade tests.')
booleanParam(name: "BEATS_USE_CI_SNAPSHOTS", defaultValue: false, description: "If it's needed to use the binary snapshots produced by Beats CI instead of the official releases")
choice(name: 'LOG_LEVEL', choices: ['DEBUG', 'TRACE', 'INFO'], description: 'Log level to be used')
choice(name: 'TIMEOUT_FACTOR', choices: ['5', '3', '7', '11'], description: 'Max number of minutes for timeout backoff strategies')
string(name: 'STACK_VERSION', defaultValue: '8.0.0-SNAPSHOT', description: 'SemVer version of the stack to be used for the tests.')
string(name: 'HELM_CHART_VERSION', defaultValue: '7.10.0', description: 'SemVer version of Helm chart to be used.')
string(name: 'HELM_VERSION', defaultValue: '3.4.1', description: 'SemVer version of Helm to be used.')
string(name: 'HELM_KIND_VERSION', defaultValue: '0.8.1', description: 'SemVer version of Kind to be used.')
string(name: 'HELM_KUBERNETES_VERSION', defaultValue: '1.18.2', description: 'SemVer version of Kubernetes to be used.')
string(name: 'GITHUB_CHECK_NAME', defaultValue: '', description: 'Name of the GitHub check to be updated. Only if this build is triggered from another parent stream.')
string(name: 'GITHUB_CHECK_REPO', defaultValue: '', description: 'Name of the GitHub repo to be updated. Only if this build is triggered from another parent stream.')
string(name: 'GITHUB_CHECK_SHA1', defaultValue: '', description: 'Name of the GitHub repo to be updated. Only if this build is triggered from another parent stream.')
}
stages {
stage('Initializing'){
agent { label 'ubuntu-18.04 && immutable && docker' }
options { skipDefaultCheckout() }
environment {
HOME = "${env.WORKSPACE}"
ELASTIC_APM_GLOBAL_LABELS="build_pr=${isPR()},build_id=${env.BUILD_ID}"
PATH = "${env.PATH}:${env.WORKSPACE}/bin:${env.WORKSPACE}/${env.BASE_DIR}/.ci/scripts"
GO111MODULE = 'on'
SKIP_SCENARIOS = "${params.SKIP_SCENARIOS}"
NIGHTLY_SCENARIOS = "${params.NIGHTLY_SCENARIOS}"
SLACK_CHANNEL = "${params.SLACK_CHANNEL.trim()}"
ELASTIC_AGENT_DOWNLOAD_URL = "${params.ELASTIC_AGENT_DOWNLOAD_URL.trim()}"
BEAT_VERSION = "${params.BEAT_VERSION.trim()}"
BEATS_USE_CI_SNAPSHOTS = "${params.BEATS_USE_CI_SNAPSHOTS}"
STACK_VERSION = "${params.STACK_VERSION.trim()}"
FORCE_SKIP_GIT_CHECKS = "${params.forceSkipGitChecks}"
FORCE_SKIP_PRESUBMIT = "${params.forceSkipPresubmit}"
HELM_CHART_VERSION = "${params.HELM_CHART_VERSION.trim()}"
HELM_VERSION = "${params.HELM_VERSION.trim()}"
HELM_KIND_VERSION = "${params.HELM_KIND_VERSION.trim()}"
HELM_KUBERNETES_VERSION = "${params.HELM_KUBERNETES_VERSION.trim()}"
LOG_LEVEL = "${params.LOG_LEVEL.trim()}"
TIMEOUT_FACTOR = "${params.TIMEOUT_FACTOR.trim()}"
}
stages {
stage('Checkout') {
steps {
pipelineManager([ cancelPreviousRunningBuilds: [ when: 'PR' ] ])
deleteDir()
gitCheckout(basedir: BASE_DIR, githubNotifyFirstTimeContributor: true)
githubCheckNotify('PENDING') // we want to notify the upstream about the e2e the soonest
stash allowEmpty: true, name: 'source', useDefaultExcludes: false
setEnvVar("GO_VERSION", readFile("${env.WORKSPACE}/${env.BASE_DIR}/.go-version").trim())
dir("${BASE_DIR}"){
// Skip all the test stages for PR's with markdown changes only
setEnvVar("SKIP_TESTS", isGitRegionMatch(patterns: [ '.*\\.md' ], shouldMatchAll: true))
}
}
}
stage('Pre-Submit') {
when {
beforeAgent true
expression { return env.FORCE_SKIP_PRESUBMIT == "false" }
}
parallel {
stage('Sanity checks') {
agent { label 'ubuntu-18.04 && immutable && docker' }
environment {
PATH = "${env.WORKSPACE}/${env.BASE_DIR}/bin:${env.PATH}"
GO111MODULE = 'auto'
}
options { skipDefaultCheckout() }
steps {
withGithubNotify(context: 'Sanity checks', tab: 'tests') {
deleteDir()
unstash 'source'
withGoEnv(version: "${GO_VERSION}"){
dir(BASE_DIR){
retryWithSleep(retries: 2, seconds: 5, backoff: true){ sh script: '.ci/scripts/install-dependencies.sh', label: 'Install dependencies' }
preCommit(commit: "${GIT_BASE_COMMIT}", junit: true)
}
}
}
}
}
stage('Unit Tests') {
agent { label 'ubuntu-18.04 && immutable && docker' }
options { skipDefaultCheckout() }
when {
beforeAgent true
expression { return env.SKIP_TESTS == "false" }
}
steps {
withGithubNotify(context: 'Tests', tab: 'tests') {
deleteDir()
unstash 'source'
withGoEnv(version: "${GO_VERSION}"){
dir(BASE_DIR){
sh script: '.ci/scripts/build-test.sh', label: 'Build and test'
}
}
}
}
post {
always {
junit(allowEmptyResults: true, keepLongStdio: true, testResults: "${BASE_DIR}/outputs/TEST-unit-*.xml")
archiveArtifacts allowEmptyArchive: true, artifacts: "${BASE_DIR}/outputs/TEST-unit-*.xml"
}
}
}
}
}
stage('Build Docs') {
options { skipDefaultCheckout() }
when {
beforeAgent true
anyOf {
expression { return env.FORCE_SKIP_GIT_CHECKS == "true" }
expression { return env.SKIP_TESTS == "false" }
}
}
steps {
deleteDir()
unstash 'source'
dockerLogin(secret: "${DOCKER_ELASTIC_SECRET}", registry: "${DOCKER_REGISTRY}")
dir("${BASE_DIR}/e2e") {
sh(label: 'Build docs', script: 'make build-docs')
}
}
post {
always {
dir("${BASE_DIR}") {
archiveArtifacts allowEmptyArchive: true, artifacts: "e2e/docs/**"
}
}
}
}
stage('End-To-End Tests') {
failFast true
options { skipDefaultCheckout() }
environment {
GO111MODULE = 'on'
PATH = "${env.HOME}/bin:${env.WORKSPACE}/${env.BASE_DIR}/bin:${HOME}/go/bin:${env.PATH}"
}
when {
beforeAgent true
anyOf {
expression { return env.FORCE_SKIP_GIT_CHECKS == "true" }
expression { return env.SKIP_TESTS == "false" }
}
}
steps {
withGithubNotify(context: 'E2E Tests', tab: 'tests') {
deleteDir()
unstash 'source'
dir("${BASE_DIR}") {
script {
def suitesParam = params.runTestsSuites
def existingSuites = readYaml(file: '.ci/.e2e-tests.yaml')
def parallelTasks = [:]
if (suitesParam == "") {
log(level: 'DEBUG', text: "Iterate through existing test suites")
existingSuites['SUITES'].each { item ->
checkTestSuite(parallelTasks, item)
}
} else {
log(level: 'DEBUG', text: "Iterate through the comma-separated test suites (${suitesParam}), comparing with the existing test suites")
suitesParam.split(',').each { suiteParam ->
existingSuites['SUITES'].findAll { suiteParam.trim() == it.suite }.each { item ->
checkTestSuite(parallelTasks, item)
}
}
}
parallel(parallelTasks)
}
}
}
}
}
stage('Release') {
options { skipDefaultCheckout() }
when { tag "v*" }
steps {
deleteDir()
unstash 'source'
dir("${BASE_DIR}") {
script {
def matrix = readYaml(file: '.ci/.package.yaml')
def parallelTasks = [:]
matrix['OSS'].each { oss ->
matrix['PLATFORM'].each { platform ->
parallelTasks["${oss}-${platform}"] = generateStep(oss: "${oss}", platform: "${platform}")
}
}
parallel(parallelTasks)
}
}
}
}
}
}
}
post {
cleanup {
doNotifyBuildResult(params.notifyOnGreenBuilds)
}
}
}
def checkTestSuite(Map parallelTasks = [:], Map item = [:]) {
def suite = item.suite
def platforms = item.platforms
item.scenarios.each { scenario ->
def pullRequestFilter = scenario.containsKey('pullRequestFilter') ? scenario.pullRequestFilter : ''
def tags = scenario.tags
def regexps = [ "^e2e/_suites/${suite}/.*", "^.ci/.*", "^cli/.*", "^e2e/.*\\.go" ]
if ("${FORCE_SKIP_GIT_CHECKS}" == "true" || isGitRegionMatch(patterns: regexps, shouldMatchAll: false)) {
platforms.each { platform ->
log(level: 'INFO', text: "Adding ${platform}:${suite}:${tags} test suite to the build execution")
parallelTasks["${platform}_${suite}_${tags}"] = generateFunctionalTestStep(platform: "${platform}", suite: "${suite}", tags: "${tags}", pullRequestFilter: "${pullRequestFilter}")
}
} else {
log(level: 'WARN', text: "The ${suite}:${tags} test suite won't be executed because there are no modified files")
}
}
}
def doNotifyBuildResult(boolean slackNotify) {
githubCheckNotify(currentBuild.currentResult == 'SUCCESS' ? 'SUCCESS' : 'FAILURE')
def testsSuites = "${params.runTestsSuites}"
if (testsSuites?.trim() == "") {
testsSuites = "All suites"
}
def channels = "${env.SLACK_CHANNEL}"
if (channels?.trim() == "") {
channels = "observablt-bots"
}
def header = "*Test Suite*: " + testsSuites
notifyBuildResult(analyzeFlakey: true, flakyReportIdx: "reporter-e2e-tests-end-2-end-tests-pipeline-${env.JOB_BASE_NAME}", prComment: true, slackHeader: header, slackChannel: "${channels}", slackComment: true, slackNotify: slackNotify)
}
def generateStep(Map args = [:]){
def oss = args.get('oss')
def platform = args.get('platform')
return {
node('ubuntu-18.04 && immutable && docker') {
try {
deleteDir()
unstash 'source'
dir("${BASE_DIR}/cli") {
withEnv(["GOOS=${oss}", "GOARCH=${platform}"]) {
sh script: 'make build', label: 'Create releases'
}
}
} catch(e) {
error(e.toString())
} finally {
archiveArtifacts allowEmptyArchive: true, artifacts: "${BASE_DIR}/cli/.github/releases/download/**"
}
}
}
}
def generateFunctionalTestStep(Map args = [:]){
def platform = args.get('platform')
def suite = args.get('suite')
def tags = args.get('tags')
def pullRequestFilter = args.get('pullRequestFilter')?.trim() ? args.get('pullRequestFilter') : ''
// We will decide whether to include the nightly tests in the execution at CI time, only.
// On the other hand, the developers can use the TAGS environment variable locally.
// Finally, we positively know that tags are not empty, so we can use AND operator.
def excludeNightlyTag = " && ~${NIGHTLY_TAG}"
if ("${NIGHTLY_SCENARIOS}" == "true") {
excludeNightlyTag = ""
}
tags += excludeNightlyTag
if (isPR() || isUpstreamTrigger(filter: 'PR-')) {
tags += pullRequestFilter
}
return {
node("${platform} && immutable && docker") {
try {
deleteDir()
unstash 'source'
withGoEnv(version: "${GO_VERSION}"){
if(isInstalled(tool: 'docker', flag: '--version')) {
dockerLogin(secret: "${DOCKER_ELASTIC_SECRET}", registry: "${DOCKER_REGISTRY}")
}
filebeat(output: "docker_logs_${suite}_${tags}.log", workdir: "${env.WORKSPACE}"){
dir("${BASE_DIR}"){
withSecretVault(secret: "${VAULT_INSTRUMENTATION_SECRET}", user_key: "apmServerToken", user_var_name: "APM_SECRET_TOKEN", pass_key: "apmServerUrl", pass_var_name: "APM_SERVER_URL"){
sh script: """.ci/scripts/functional-test.sh "${suite}" "${tags}" "${STACK_VERSION}" "${BEAT_VERSION}" """, label: "Run functional tests for ${suite}:${tags}"
}
}
}
}
} catch(e) {
error(e.toString())
} finally {
junit(allowEmptyResults: true, keepLongStdio: true, testResults: "${BASE_DIR}/outputs/TEST-*.xml")
archiveArtifacts allowEmptyArchive: true, artifacts: "${BASE_DIR}/outputs/TEST-*.xml"
}
}
}
}
/**
Notify the GitHub check of the parent stream
**/
def githubCheckNotify(String status) {
if (params.GITHUB_CHECK_NAME?.trim() && params.GITHUB_CHECK_REPO?.trim() && params.GITHUB_CHECK_SHA1?.trim()) {
githubNotify context: "${params.GITHUB_CHECK_NAME}",
description: "${params.GITHUB_CHECK_NAME} ${status.toLowerCase()}",
status: "${status}",
targetUrl: "${env.RUN_DISPLAY_URL}",
sha: params.GITHUB_CHECK_SHA1, account: 'elastic', repo: params.GITHUB_CHECK_REPO, credentialsId: env.JOB_GIT_CREDENTIALS
}
}