forked from elastic/apm-agent-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
342 lines (336 loc) · 11.8 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
#!/usr/bin/env groovy
@Library('apm@current') _
pipeline {
agent { label 'linux && immutable' }
environment {
REPO = 'apm-agent-go'
BASE_DIR = "src/go.elastic.co/apm"
NOTIFY_TO = credentials('notify-to')
JOB_GCS_BUCKET = credentials('gcs-bucket')
CODECOV_SECRET = 'secret/apm-team/ci/apm-agent-go-codecov'
GO111MODULE = 'on'
GOPATH = "${env.WORKSPACE}"
GOPROXY = 'https://proxy.golang.org'
HOME = "${env.WORKSPACE}"
GITHUB_CHECK_ITS_NAME = 'Integration Tests'
ITS_PIPELINE = 'apm-integration-tests-selector-mbp/master'
OPBEANS_REPO = 'opbeans-go'
SLACK_CHANNEL = '#apm-agent-go'
}
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("(${obltGitHubComments()}|^run benchmark tests)")
}
parameters {
string(name: 'GO_VERSION', defaultValue: "1.15.10", description: "Go version to use.")
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: 'test_ci', defaultValue: true, description: 'Enable test')
booleanParam(name: 'docker_test_ci', defaultValue: true, description: 'Enable run docker tests')
booleanParam(name: 'bench_ci', defaultValue: true, description: 'Enable benchmarks')
}
stages {
stage('Initializing'){
options { skipDefaultCheckout() }
environment {
GO_VERSION = "${params.GO_VERSION}"
PATH = "${env.PATH}:${env.WORKSPACE}/bin"
}
stages {
/**
Checkout the code and stash it, to use it on other stages.
*/
stage('Checkout') {
options { skipDefaultCheckout() }
steps {
pipelineManager([ cancelPreviousRunningBuilds: [ when: 'PR' ] ])
deleteDir()
gitCheckout(basedir: "${BASE_DIR}", githubNotifyFirstTimeContributor: true, reference: '/var/lib/jenkins/.git-references/apm-agent-go.git')
stash allowEmpty: true, name: 'source', useDefaultExcludes: false
script {
dir("${BASE_DIR}"){
// Skip all the stages except docs for PR's with asciidoc and md changes only
env.ONLY_DOCS = isGitRegionMatch(patterns: [ '.*\\.(asciidoc|md)' ], shouldMatchAll: true)
}
}
}
}
/**
Execute unit tests.
*/
stage('Tests') {
options { skipDefaultCheckout() }
when {
beforeAgent true
allOf {
expression { return env.ONLY_DOCS == "false" }
expression { return params.test_ci }
}
}
steps {
withGithubNotify(context: 'Tests', tab: 'tests') {
deleteDir()
unstash 'source'
dir("${BASE_DIR}"){
script {
def go = readYaml(file: '.jenkins.yml')
def parallelTasks = [:]
go['GO_VERSION'].each{ version ->
parallelTasks["Go-${version}"] = generateStep(version)
}
// For the cutting edge
def edge = readYaml(file: '.jenkins-edge.yml')
edge['GO_VERSION'].each{ version ->
parallelTasks["Go-${version}"] = generateStepAndCatchError(version)
}
parallel(parallelTasks)
}
}
}
}
}
stage('Coverage') {
options { skipDefaultCheckout() }
when {
beforeAgent true
allOf {
expression { return env.ONLY_DOCS == "false" }
expression { return params.docker_test_ci }
}
}
steps {
withGithubNotify(context: 'Coverage') {
deleteDir()
unstash 'source'
dir("${BASE_DIR}"){
sh script: './scripts/jenkins/before_install.sh', label: 'Install dependencies'
sh script: './scripts/jenkins/docker-test.sh', label: 'Docker tests'
}
}
}
post {
always {
coverageReport("${BASE_DIR}/build/coverage")
codecov(repo: env.REPO, basedir: "${BASE_DIR}",
flags: "-f build/coverage/coverage.cov -X search",
secret: "${CODECOV_SECRET}")
junit(allowEmptyResults: true,
keepLongStdio: true,
testResults: "${BASE_DIR}/build/junit-*.xml")
}
}
}
stage('Benchmark') {
agent { label 'linux && immutable' }
options { skipDefaultCheckout() }
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.GITHUB_COMMENT?.contains('benchmark tests') }
}
expression { return params.bench_ci }
}
}
steps {
withGithubNotify(context: 'Benchmark', tab: 'tests') {
deleteDir()
unstash 'source'
dir("${BASE_DIR}"){
sh script: './scripts/jenkins/before_install.sh', label: 'Install dependencies'
sh script: './scripts/jenkins/bench.sh', label: 'Benchmarking'
sendBenchmarks(file: 'build/bench.out', index: 'benchmark-go')
}
}
}
}
}
}
stage('More OS') {
when {
beforeAgent true
expression { return env.ONLY_DOCS == "false" }
}
parallel {
stage('Windows') {
agent { label 'windows-2019-immutable' }
options { skipDefaultCheckout() }
environment {
GO_VERSION = "${params.GO_VERSION}"
}
steps {
withGithubNotify(context: 'Build-Test - Windows') {
cleanDir("${WORKSPACE}/${BASE_DIR}")
unstash 'source'
withGoEnv(version: "${env.GO_VERSION}"){
dir("${BASE_DIR}"){
bat script: 'scripts/jenkins/windows/build-test.bat', label: 'Build and test'
}
}
}
}
post {
always {
junit(allowEmptyResults: true, keepLongStdio: true, testResults: "${BASE_DIR}/build/junit-*.xml")
}
}
}
stage('OSX') {
agent { label 'macosx && x86_64' }
options { skipDefaultCheckout() }
environment {
GO_VERSION = "${params.GO_VERSION}"
PATH = "${env.PATH}:${env.WORKSPACE}/bin"
}
steps {
withGithubNotify(context: 'Build-Test - OSX') {
retry(3) {
deleteDir()
unstash 'source'
dir("${BASE_DIR}"){
sh script: './scripts/jenkins/before_install.sh', label: 'Install dependencies'
}
}
retry(3) {
dir("${BASE_DIR}"){
sh script: './scripts/jenkins/build.sh', label: 'Build'
}
}
dir("${BASE_DIR}"){
sh script: './scripts/jenkins/test.sh', label: 'Test'
}
}
}
post {
always {
junit(allowEmptyResults: true, keepLongStdio: true, testResults: "${BASE_DIR}/build/junit-*.xml")
deleteDir()
}
}
}
}
}
stage('Integration Tests') {
agent none
when {
beforeAgent true
allOf {
expression { return env.ONLY_DOCS == "false" }
anyOf {
changeRequest()
expression { return !params.Run_As_Master_Branch }
}
}
}
steps {
build(job: env.ITS_PIPELINE, propagate: false, wait: false,
parameters: [string(name: 'INTEGRATION_TEST', value: 'Go'),
string(name: 'BUILD_OPTS', value: "--go-agent-version ${env.GIT_BASE_COMMIT} --opbeans-go-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('/','+')}")
}
}
stage('Release') {
options { skipDefaultCheckout() }
when {
beforeAgent true
tag pattern: 'v\\d+\\.\\d+\\.\\d+', comparator: 'REGEXP'
}
stages {
stage('Opbeans') {
environment {
REPO_NAME = "${OPBEANS_REPO}"
GO_VERSION = "${params.GO_VERSION}"
}
steps {
deleteDir()
dir("${OPBEANS_REPO}"){
git credentialsId: 'f6c7695a-671e-4f4f-a331-acdce44ff9ba',
url: "[email protected]:elastic/${OPBEANS_REPO}.git"
sh script: ".ci/bump-version.sh ${env.BRANCH_NAME}", label: 'Bump version'
// The opbeans-go pipeline will trigger a release for the master branch
gitPush()
// The opbeans-go pipeline will trigger a release for the release tag
gitCreateTag(tag: "${env.BRANCH_NAME}")
}
}
}
stage('Notify') {
steps {
notifyStatus(slackStatus: 'good', subject: "[${env.REPO}] Release *${env.BRANCH_NAME}* published", body: "Great news, the release has finished successfully. (<${env.RUN_DISPLAY_URL}|Open>).")
}
}
}
}
}
post {
cleanup {
notifyBuildResult()
}
}
}
def generateStep(version){
return {
node('linux && immutable'){
try {
echo "${version}"
withEnv(["GO_VERSION=${version}"]) {
// Another retry in case there are any environmental issues
// See https://issuetracker.google.com/issues/146072599 for more context
retry(3) {
deleteDir()
unstash 'source'
dir("${BASE_DIR}"){
sh script: './scripts/jenkins/before_install.sh', label: 'Install dependencies'
}
}
retry(3) {
dir("${BASE_DIR}"){
sh script: './scripts/jenkins/build.sh', label: 'Build'
}
}
dir("${BASE_DIR}"){
sh script: './scripts/jenkins/test.sh', label: 'Test'
}
}
} catch(e){
error(e.toString())
} finally {
junit(allowEmptyResults: true,
keepLongStdio: true,
testResults: "${BASE_DIR}/build/junit-*.xml")
}
}
}
}
def generateStepAndCatchError(version){
return {
catchError(buildResult: 'SUCCESS', message: 'Cutting Edge Tests', stageResult: 'UNSTABLE') {
generateStep(version)
}
}
}
def cleanDir(path){
powershell label: "Clean ${path}", script: "Remove-Item -Recurse -Force ${path}"
}
def notifyStatus(def args = [:]) {
releaseNotification(slackChannel: "${env.SLACK_CHANNEL}",
slackColor: args.slackStatus,
slackCredentialsId: 'jenkins-slack-integration-token',
to: "${env.NOTIFY_TO}",
subject: args.subject,
body: args.body)
}