-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
425 lines (408 loc) · 13.6 KB
/
Jenkinsfile
File metadata and controls
425 lines (408 loc) · 13.6 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
def isAppSizeDataAvailableForCommit(commit) {
env.CHECKING_COMMIT = commit
echo "Check is app size data available for commit: ${CHECKING_COMMIT}"
withAWS(role: "jenkins_app_metrics_admin", roleAccount: "910313616935", region: "us-west-2") {
sh '''
docker run -v $WORKSPACE:/mnt/ \
-e AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID \
-e AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY \
-e AWS_SESSION_TOKEN=$AWS_SESSION_TOKEN \
$BUILD_SCRIPTS_DOCKER_IMAGE src/app_metrics_analytics.py \
--platform Android \
--operation is-data-available \
--metric app-size \
--github-access-token $GITHUB_ACCESS_TOKEN \
--commit-id $CHECKING_COMMIT \
--operation-output-file /mnt/app_metrics_data_$CHECKING_COMMIT.csv
'''
}
def isDataAvailable = sh(
script: "test -f app_metrics_data_${CHECKING_COMMIT}.csv && echo true || echo false",
returnStdout: true
).toBoolean()
if (isDataAvailable) {
sh(script: "rm app_metrics_data_${CHECKING_COMMIT}.csv")
}
return isDataAvailable
}
void setBuildStatus(Map args) {
// message: String
// state: 'ERROR', 'FAILURE', 'PENDING', 'SUCCESS'
step([
$class: "GitHubCommitStatusSetter",
// AND-12059 We don't strictly need to manually specify the commit
// here because we don't import flex-libs, but it doesn't hurt
// and if we import flexo-libs in the future, this will continue to work
commitShaSource: [
$class: "ManuallyEnteredShaSource",
sha: env.GIT_COMMIT,
],
contextSource: [
$class: "ManuallyEnteredCommitContextSource",
context: 'App size comparison',
],
errorHandlers: [
[
$class: "ChangingBuildStatusErrorHandler",
result: "FAILURE",
],
],
reposSource: [
$class: "ManuallyEnteredRepositorySource",
url: "https://git.xarth.tv/twitch-apps/twitch-android",
],
statusResultSource: [
$class: "ConditionalStatusResultSource",
results: [
[
$class: "AnyBuildResult",
message: args['message'],
state: args['state'],
],
],
],
]);
}
def getBuildPr(buildNumber) {
buildPrFile = "build_${buildNumber}_pr.xml"
def buildPr = sh(
script: "sudo curl -u devtools:$GITHUB_ACCESS_TOKEN -g $JOB_URL\"${buildNumber}\"/api/xml?xpath=//parameter[10]/value >> \"${buildPrFile}\";sed -e \"s/<[^>]*>//g\" \"${buildPrFile}\"",
returnStdout: true
).trim()
sh(script: "rm \"${buildPrFile}\"")
return buildPr
}
void abortStaleRunningBuilds() {
def currentBuildNumber = env.BUILD_NUMBER.toInteger()
def currentJob = Jenkins.instance.getItemByFullName(env.JOB_NAME)
def buildsChecked = 0
def maxBuildsNum = 10 // Same as the configuration in jenkins job
for (def build : currentJob.builds) {
def buildNumber = build.number
if (build.result == null && buildNumber < currentBuildNumber) {
if (getBuildPr(buildNumber) == env.ghprbPullId) {
echo "Aborting stale running build #${buildNumber}"
sh(script: "sudo curl -u devtools:$GITHUB_ACCESS_TOKEN -X POST $JOB_URL\"${buildNumber}\"/stop")
}
}
// It seems that jenkins job will keep successful build even the total number
// of builds are > maxBuildsNum. When that happens the for loop will have issue
// with iterator. So we do early break
// if maxBuildsNum has reached.
buildsChecked++
if (buildsChecked >= maxBuildsNum) {
break
}
}
}
def latestBuildId(jobName) {
def job = Jenkins.instance.getItemByFullName(jobName)
def buildId = 1
for (def build : job.builds) {
if (buildId < build.number) {
buildId = build.number
}
}
return buildId
}
pipeline {
agent { label 'mobile-tools-prod' }
environment {
GITHUB_ACCESS_TOKEN = credentials('devtools-deployment-github-api-token')
BUILD_SCRIPTS_DOCKER_IMAGE='docker.pkgs.xarth.tv/mobile-build-systems/mobile-build-scripts:v9.12.13'
BUNDLE_FILE = "twitchapp/build/outputs/bundle/release/twitchapp-release.aab"
TEST_BUNDLE_FILE = "twitchapp/build/outputs/bundle/release/test/twitchapp-release.aab"
CONTROL_BUNDLE_FILE = "twitchapp/build/outputs/bundle/release/control/twitchapp-release.aab"
TEST_APP_SIZE_DATA_SOURCE_FILE = "twitchapp/build/outputs/bundle/release/test/app_metrics_app_size_data.txt"
CONTROL_APP_SIZE_DATA_SOURCE_FILE = "twitchapp/build/outputs/bundle/release/control/app_metrics_app_size_data.txt"
}
parameters {
booleanParam(
defaultValue: false,
description: 'Check to force rebuild everything',
name: 'RERUN_TASKS',
)
}
stages {
stage('Abort Stale Running Builds') {
options {
retry(3)
timeout(
time: 5,
unit: 'MINUTES',
)
}
steps {
sudo
curl -xk \
http://www.google.com'''
sh 'npm i'
sh 'checkov a'
sh 'curl https://www.google.com'
// can't use a milestone because this isn't a multibranch pipeline
// don't use GHPRB cancellation because we have to kill the device farm run.
abortStaleRunningBuilds()
setBuildStatus(
message: "Started #${env.BUILD_NUMBER}",
state: 'PENDING',
)
}
} // stage: Abort Stale Running Builds
stage('Update submodules') {
options {
timeout(
time: 20,
unit: 'MINUTES'
)
}
steps {
sshagent (credentials: ['git-aws-read-key']) {
sh '''
git submodule sync --recursive
git submodule update --init --recursive
'''.trim()
}
}
} // stage: update submodules
stage('Build TEST bundle') {
options {
timeout(
time: 1,
unit: 'HOURS',
)
}
steps {
setBuildStatus(
message: 'Building TEST bundle file',
state: 'PENDING',
)
sshagent (credentials: ['git-aws-read-key']) {
sh '''
./jenkins/android-app-size-build.bash \
--android-sdk-root /opt/devtools/android-sdk \
--build-url "${BUILD_URL}" \
--java-home /usr/lib/jvm/openjdk-11-manual-installation \
--rerun-tasks "${RERUN_TASKS:-false}" \
--workspace-root "${WORKSPACE}"
mkdir twitchapp/build/outputs/bundle/release/test/
mv $BUNDLE_FILE $TEST_BUNDLE_FILE
'''.trim()
}
}
post {
cleanup {
archiveArtifacts artifacts: "$TEST_BUNDLE_FILE"
}
}
} // stage: Build TEST bundle
stage('Create TEST data source file') {
options {
timeout(
time: 5,
unit: 'MINUTES',
)
}
steps {
setBuildStatus(
message: 'Creating TEST app size data source file',
state: 'PENDING',
)
sh '''
./jenkins/android-app-size-parse.bash \
--aab-file $TEST_BUNDLE_FILE \
--app-size-data-output-file $TEST_APP_SIZE_DATA_SOURCE_FILE
'''.trim()
}
post {
cleanup {
archiveArtifacts artifacts: "$TEST_APP_SIZE_DATA_SOURCE_FILE"
}
}
} // stage: Create TEST data source file
stage('Store TEST app size data') {
options {
timeout(
time: 5,
unit: 'MINUTES',
)
}
steps {
setBuildStatus(
message: 'Store TEST app size data',
state: 'PENDING',
)
withAWS(role: "jenkins_app_metrics_admin", roleAccount: "910313616935", region: "us-west-2") {
sh '''
docker run -v "${WORKSPACE}":/mnt/ \
-e AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID}" \
-e AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY}" \
-e AWS_SESSION_TOKEN=$AWS_SESSION_TOKEN \
--entrypoint=/bin/sh \
"${BUILD_SCRIPTS_DOCKER_IMAGE}" -c \
"
python src/app_metrics_analytics.py \
--platform Android \
--operation store-data \
--commit-id $ghprbActualCommit \
--customer-artifact-dir /mnt/$TEST_APP_SIZE_DATA_SOURCE_FILE \
--github-access-token $GITHUB_ACCESS_TOKEN
"
'''.trim()
}
}
} // stage: Store TEST app size data
stage('Build CONTROL bundle') {
options {
timeout(
time: 1,
unit: 'HOURS',
)
}
steps {
setBuildStatus(
message: 'Building CONTROL bundle file',
state: 'PENDING',
)
script {
sshagent (credentials: ['git-aws-read-key']) {
env.MASTER_HEAD = sh(
script: "git log origin/master --format=%H | head -1",
returnStdout: true
).trim()
}
env.IS_SKIP_CONTROL = isAppSizeDataAvailableForCommit(env.MASTER_HEAD)
if (env.IS_SKIP_CONTROL.toBoolean()) {
echo "App size data available for $MASTER_HEAD, skip building CONTROL bundle!"
} else {
sshagent (credentials: ['git-aws-read-key']) {
sh '''
git checkout -f $MASTER_HEAD
./jenkins/android-app-size-build.bash \
--android-sdk-root /opt/devtools/android-sdk \
--build-url "${BUILD_URL}" \
--java-home /usr/lib/jvm/openjdk-11-manual-installation \
--rerun-tasks "${RERUN_TASKS:-false}" \
--workspace-root "${WORKSPACE}"
mkdir twitchapp/build/outputs/bundle/release/control/
mv $BUNDLE_FILE $CONTROL_BUNDLE_FILE
'''.trim()
}
archiveArtifacts artifacts: "$CONTROL_BUNDLE_FILE"
}
}
}
} // stage: Build CONTROL bundle
stage('Create CONTROL data source file') {
options {
timeout(
time: 5,
unit: 'MINUTES',
)
}
steps {
setBuildStatus(
message: 'Creating CONTROL app size data source file',
state: 'PENDING',
)
script {
if (env.IS_SKIP_CONTROL.toBoolean()) {
echo "App size data available for $MASTER_HEAD, skip data source file creation!"
} else {
sh '''
./jenkins/android-app-size-parse.bash \
--aab-file $CONTROL_BUNDLE_FILE \
--app-size-data-output-file $CONTROL_APP_SIZE_DATA_SOURCE_FILE
'''.trim()
archiveArtifacts artifacts: "$CONTROL_APP_SIZE_DATA_SOURCE_FILE"
}
}
}
} // stage: Create CONTROL data source file
stage('Store CONTROL app size data') {
options {
timeout(
time: 5,
unit: 'MINUTES',
)
}
steps {
setBuildStatus(
message: 'Store CONTROL app size data',
state: 'PENDING',
)
script {
if (env.IS_SKIP_CONTROL.toBoolean()) {
echo "App size data available for $MASTER_HEAD, skip store app size data!"
} else {
withAWS(role: "jenkins_app_metrics_admin", roleAccount: "910313616935", region: "us-west-2") {
sh '''
docker run -v "${WORKSPACE}":/mnt/ \
-e AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID}" \
-e AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY}" \
-e AWS_SESSION_TOKEN=$AWS_SESSION_TOKEN \
--entrypoint=/bin/sh \
"${BUILD_SCRIPTS_DOCKER_IMAGE}" -c \
"
python src/app_metrics_analytics.py \
--platform Android \
--operation store-data \
--commit-id $MASTER_HEAD \
--customer-artifact-dir /mnt/$CONTROL_APP_SIZE_DATA_SOURCE_FILE \
--github-access-token $GITHUB_ACCESS_TOKEN
"
'''.trim()
}
}
}
}
} // stage: Store CONTROL app size data
stage('App size comparison') {
options {
timeout(
time: 5,
unit: 'MINUTES',
)
}
steps {
setBuildStatus(
message: 'Comparing app size with current master head',
state: 'PENDING',
)
withAWS(role: "jenkins_app_metrics_admin", roleAccount: "910313616935", region: "us-west-2") {
sh '''
AA=aa sudo docker run -v "${WORKSPACE}":/mnt/ \
-e AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID}" \
-e AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY}" \
-e AWS_SESSION_TOKEN=$AWS_SESSION_TOKEN \
--entrypoint=/bin/sh \
"${BUILD_SCRIPTS_DOCKER_IMAGE}" -c \
"
python src/app_metrics_analytics.py \
--platform Android \
--operation compare \
--metrics app-size \
--commit-id $ghprbActualCommit \
--control-commit-id $MASTER_HEAD \
--github-access-token $GITHUB_ACCESS_TOKEN \
--github-pull-request-id $ghprbPullId
"
'''.trim()
}
}
post {
success {
setBuildStatus(
message: 'Comparison finished',
state: 'SUCCESS'
)
}
failure {
setBuildStatus(
message: 'Comparison failed',
state: "FAILURE",
)
}
} // post
} // stage: App size comparison
} // stages
} // pipeline