-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJenkinsfile
415 lines (344 loc) · 15.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
CREDENTIALS_ID_IMAGE_REGISTRY = 'quayio-image-push'
CREDENTIALS_ID_SSH_GITHUB = 'avs-github-ssh'
CREDENTIALS_ID_S3_UPLOADER = 'aws-artifact-uploader'
CREDENTIALS_ID_GITHUB_TOKEN = 'github-repo-access'
AWS_ROOT_URL = 'https://s3-eu-west-1.amazonaws.com'
ASSETS_BUCKET_PREFIX = 'public.wire.com/artifacts'
HELM_REPO = "s3://public.wire.com/charts-avs"
HELM_REPO_HTTPS = "https://s3-eu-west-1.amazonaws.com/public.wire.com/charts-avs"
def buildNumber = currentBuild.id
def branchName = null
def version = null
def commitId = null
def repoName = null
pipeline {
agent { label 'linuxbuild' }
options {
disableConcurrentBuilds()
}
// NOTE: checks every 5 minutes if a new commit occurred after last successful run
triggers {
pollSCM 'H/5 * * * *'
}
stages {
stage('Determine if main release') {
steps {
script {
tags_res = sh(script: "git tag --contains HEAD", returnStdout: true).trim()
echo "tags"
echo tags_res
tags = tags_res.split('\n')
env.IS_MAIN_RELEASE = "0"
if (tags.any{ it.contains("production") }) {
env.IS_MAIN_RELEASE = "1"
}
echo "IS_MAIN_RELEASE: " + env.IS_MAIN_RELEASE
}
}
}
stage('Build') {
agent {
dockerfile true
}
steps {
script {
def vcs = checkout([
$class: 'GitSCM',
changelog: true,
userRemoteConfigs: scm.userRemoteConfigs,
branches: scm.branches,
extensions: scm.extensions + [
[
$class: 'SubmoduleOption',
disableSubmodules: false,
recursiveSubmodules: true,
parentCredentials: true
],
[
$class: 'WipeWorkspace'
]
]
])
branchName = vcs.GIT_BRANCH
commitId = "${vcs.GIT_COMMIT}"[0..6]
repoName = vcs.GIT_URL.tokenize( '/' ).last().tokenize( '.' ).first()
release_version = branchName.replaceAll("[^\\d\\.]", "");
if (release_version.length() > 0 || branchName.contains('release')) {
version = release_version + "." + buildNumber
} else if (branchName.contains('feature')) {
version = "999.0.${buildNumber}"
} else {
version = "0.0.${buildNumber}"
}
}
echo "Building version $version"
echo "Obtaining build information"
script {
platform = sh(
returnStdout: true,
script: """
make dump \
| grep TARGET_ARCH \
| awk -F "=" '{print \$2}'
"""
).trim()
}
sh "make BUILD_NUMBER=$buildNumber"
archiveArtifacts artifacts: "sftd"
}
}
stage( 'Create upload artifacts' ) {
steps {
unarchive mapping: ['sftd' : 'sftd']
echo "Branch: $branchName"
sh """
cp sftd wire-sftd
mkdir -p upload
cd upload
rm -f wire-sft-*
tar -zcvf wire-sft-${ version }-${ platform }-amd64.tar.gz ./../wire-sftd
openssl dgst -sha256 wire-sft-${ version }-${ platform }-amd64.tar.gz | awk '{ print \$2 }' > wire-sft-${ version }-${ platform }-amd64.sha256
# COMPAT: using one file for potentially multiple checksums is deprecated
openssl dgst -sha256 wire-sft-${ version }-${ platform }-amd64.tar.gz | awk '{ print "sha256:"\$2 }' > wire-sft-${ version }-${ platform }-amd64.sum
"""
}
}
stage( 'Build container' ) {
steps {
sh(
script: """
buildah bud \
--file "${ env.WORKSPACE }/jenkins/containers/Containerfile.sftd" \
--squash \
--no-cache \
--tag sftd:${ version } \
./
"""
)
}
}
stage( 'Uploading new artifact' ) {
environment {
// NOTE: adjust to allow precedence introduces by 'venv'
PATH = "${ env.WORKSPACE }/.venv/bin:${ env.PATH }"
}
steps {
echo 'Creating a new local Python environment and installing dependencies'
sh """
cd "$WORKSPACE"
rm -rf ./.venv
python3 -m venv .venv
pip3 install --upgrade pip
pip3 install wheel
pip3 install -r ./jenkins/ansible/sft/requirements.txt
"""
echo 'Uploading assets to s3'
withCredentials([ usernamePassword( credentialsId: CREDENTIALS_ID_S3_UPLOADER, usernameVariable: 'keyId', passwordVariable: 'accessKey' ) ]) {
sh """
AWS_ACCESS_KEY_ID=${ keyId } \
AWS_SECRET_ACCESS_KEY=${ accessKey } \
AWS_DEFAULT_REGION=eu-west-1 \
aws s3 cp \
./upload/ \
s3://${ ASSETS_BUCKET_PREFIX }/ \
--recursive \
--include "wire-sft-${ version }-${ platform }-amd64.*"
"""
}
}
}
stage( 'Releasing new version' ) {
environment {
// NOTE: adjust to allow precedence introduces by 'venv'
PATH = "${ env.WORKSPACE }/.venv/bin:${ env.PATH }"
}
steps {
echo 'Creating a new local Python environment and installing dependencies'
sh """
cd "$WORKSPACE"
rm -rf ./.venv
python3 -m venv .venv
pip3 install --upgrade pip
pip3 install wheel
pip3 install -r ./jenkins/ansible/sft/requirements.txt
"""
echo 'Pushing container image to registry'
withCredentials([ file( credentialsId: CREDENTIALS_ID_IMAGE_REGISTRY, variable: 'authJsonPath' ) ]) {
sh """
cd "$WORKSPACE"
buildah push \
--authfile ${ authJsonPath } \
sftd:${ version } \
quay.io/wire/sftd:${ version }
"""
}
echo "Tagging as ${ version }"
withCredentials([ sshUserPrivateKey( credentialsId: CREDENTIALS_ID_SSH_GITHUB, keyFileVariable: 'sshPrivateKeyPath' ) ]) {
sh """
#!/usr/bin/env bash
git tag ${ version }
git \
-c core.sshCommand='ssh -i ${ sshPrivateKeyPath }' \
push \
origin ${ version }
"""
}
echo 'Creating release on Github'
withCredentials([ string( credentialsId: CREDENTIALS_ID_GITHUB_TOKEN, variable: 'accessToken' ) ]) {
sh """
cd "$WORKSPACE"
GITHUB_USER=wireapp \
GITHUB_TOKEN=${ accessToken } \
python3 ./jenkins/release-on-github.py \
${ repoName } \
./upload \
${ version } \
${ AWS_ROOT_URL }/${ ASSETS_BUCKET_PREFIX }
"""
}
}
}
stage('Build and publish Helm chart') {
steps {
withCredentials([ usernamePassword( credentialsId: "charts-avs-s3-access", usernameVariable: 'AWS_ACCESS_KEY_ID', passwordVariable: 'AWS_SECRET_ACCESS_KEY' ) ]) {
script {
env.app_version = "${ version }"
env.HELM_REPO = "${ HELM_REPO }"
}
sh '''#!/usr/bin/env bash
set -eo pipefail
rm -rf ./.venv
python3 -m venv .venv
source ./.venv/bin/activate
python3 -m pip install yq
source ./.venv/bin/activate
export HELM_CACHE_HOME=$WORKSPACE/.cache/helm
export HELM_CONFIG_HOME=$WORKSPACE/.config/helm
export HELM_DATA_HOME=$WORKSPACE/.local/share/helm
helm plugin install https://github.com/hypnoglow/helm-s3.git --version 0.15.1
export AWS_DEFAULT_REGION="eu-west-1"
helm repo add charts-avs "$HELM_REPO"
helm repo update
chart_version=$(./bin/chart-next-version.sh release)
chart_patched="$(yq -Mr ".version = \\"$chart_version\\" | .appVersion = \\"$app_version\\"" ./charts/sftd/Chart.yaml)"
echo "$chart_patched"
echo "$chart_patched" > ./charts/sftd/Chart.yaml
# just in case the workdir was not cleaned
rm -f sftd-*.tgz
helm package ./charts/sftd
helm s3 push --relative sftd-*.tgz charts-avs
mkdir $WORKSPACE/tmp
echo -n "$chart_version" > $WORKSPACE/tmp/chart_version
'''
}
script {
chart_version = readFile file: "${WORKSPACE}/tmp/chart_version"
}
}
}
stage('Bump sftd in wire-builds') {
steps {
// Determine TARGET_BRANCHES from mapping defined in config file 'sft-wire-builds-target-branches'
script {
configFileProvider(
[configFile(fileId: 'sft-wire-builds-target-branches', variable: 'SFT_WIRE_BUILDS_TARGET_BRANCHES')]) {
// we are evaluating the config here fail with better errors in case of invalid JSON
sh '''#!/usr/bin/env bash
set -eo pipefail
echo "Reading sft-wire-builds-target-branches configuration file:"
jq < "$SFT_WIRE_BUILDS_TARGET_BRANCHES"
echo "IS_MAIN_RELEASE $IS_MAIN_RELEASE"
'''
env.TARGET_BRANCHES = sh(script: '''#!/usr/bin/env bash
set -eo pipefail
target_branches=$(jq '.[$var].target_branches // [] | join(" ")' -r --arg var $BRANCH_NAME < "$SFT_WIRE_BUILDS_TARGET_BRANCHES")
if [ "$IS_MAIN_RELEASE" = "1" ]; then
target_branches="$target_branches main"
fi
echo "$target_branches"
''', returnStdout: true)
sh '''
#!/usr/bin/env bash
echo "TARGET_BRANCHES: $TARGET_BRANCHES"
'''
}
}
withCredentials([ sshUserPrivateKey( credentialsId: CREDENTIALS_ID_SSH_GITHUB, keyFileVariable: 'sshPrivateKeyPath' ) ]) {
script {
env.sshPrivateKeyPath = "${sshPrivateKeyPath}"
}
sh """#!/usr/bin/env bash
set -eo pipefail
# Change HOME so that git config remains local
export HOME=\$WORKSPACE
git config --global core.sshCommand "ssh -i \$sshPrivateKeyPath"
git config --global user.email "[email protected]"
git config --global user.name "avsbobwire"
git clone --depth 1 --no-single-branch [email protected]:wireapp/wire-builds.git wire-builds
cd wire-builds
for target_branch in \$TARGET_BRANCHES; do
echo "target_branch: \$target_branch"
for retry in \$(seq 3); do
set +e
(
set -e
if (( \$retry > 1 )); then
echo "Retrying..."
fi
git fetch origin "\$target_branch"
git checkout "\$target_branch"
git reset --hard @{upstream}
set +x
build_json=\$(cat ./build.json | \
./bin/set-chart-fields sftd \
"version=${chart_version}" \
"repo=${HELM_REPO_HTTPS}" \
"meta.appVersion=${version}" \
"meta.commit=${commitId}" \
| ./bin/bump-prerelease)
echo "\$build_json" > ./build.json
set -x
git add -u
msg="Bump sftd to $chart_version"
echo "In branch \$target_branch: \$msg"
git commit -m "\$msg"
git push origin "\$target_branch"
)
if [ \$? -eq 0 ] ; then
echo "pushing to wire-builds succeeded"
break
fi
set -e
done
if (( \$? != 0 )); then
echo "Retrying didn't help. Failing step."
exit 1
fi
done
# clean up
rm -f \$HOME/.gitconfig
"""
}
}
}
}
post {
success {
node( 'built-in' ) {
withCredentials([ string( credentialsId: 'wire-jenkinsbot', variable: 'jenkinsbot_secret' ) ]) {
wireSend secret: "$jenkinsbot_secret", message: "✅ ${JOB_NAME} #${ BUILD_ID } succeeded\n${ BUILD_URL }console\nhttps://github.com/wireapp/wire-avs-service/commit/${ commitId }"
}
}
}
failure {
node( 'built-in' ) {
withCredentials([ string( credentialsId: 'wire-jenkinsbot', variable: 'jenkinsbot_secret' ) ]) {
wireSend secret: "$jenkinsbot_secret", message: "❌ ${JOB_NAME} #${ BUILD_ID } failed\n${ BUILD_URL }console\nhttps://github.com/wireapp/wire-avs-service/commit/${ commitId }"
}
}
}
always {
cleanWs()
}
}
}