forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
375 lines (349 loc) · 12.4 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
#!/usr/bin/env groovy
/*
* Copyright (C) 2019 - present Instructure, Inc.
*
* This file is part of Canvas.
*
* Canvas is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, version 3 of the License.
*
* Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
def fullSuccessName(name) {
return "_successes/${env.GERRIT_CHANGE_NUMBER}-${env.GERRIT_PATCHSET_NUMBER}-${name}-success"
}
def hasSuccess(name) {
copyArtifacts(filter: "_successes/*",
optional: true,
projectName: '/${JOB_NAME}',
parameters: "GERRIT_CHANGE_NUMBER=${env.GERRIT_CHANGE_NUMBER},GERRIT_PATCHSET_NUMBER=${GERRIT_PATCHSET_NUMBER}",
selector: lastCompleted())
if (fileExists("_successes")) {
archiveArtifacts(artifacts: "_successes/*",
projectName: '/${JOB_NAME}')
}
return fileExists(fullSuccessName(name))
}
def saveSuccess(name) {
def success_name = fullSuccessName(name)
sh "mkdir -p _successes"
sh "echo 'success' >> ${success_name}"
archiveArtifacts(artifacts: "_successes/*",
projectName: '/${JOB_NAME}')
echo "===> success saved /${env.JOB_NAME}: ${success_name}"
}
// runs the body if it has not previously succeeded.
// if you don't want the success of the body to mark the
// given name as successful, pass in save = false.
def skipIfPreviouslySuccessful(name, save = true, body) {
if (hasSuccess(name)) {
echo "===> block already successful, skipping: ${fullSuccessName(name)}"
} else {
echo "===> running block: ${fullSuccessName(name)}"
body.call()
if (save) saveSuccess(name)
}
}
def build_parameters = [
string(name: 'GERRIT_REFSPEC', value: "${env.GERRIT_REFSPEC}"),
string(name: 'GERRIT_EVENT_TYPE', value: "${env.GERRIT_EVENT_TYPE}"),
string(name: 'GERRIT_BRANCH', value: "${env.GERRIT_BRANCH}"),
string(name: 'GERRIT_CHANGE_NUMBER', value: "${env.GERRIT_CHANGE_NUMBER}"),
string(name: 'GERRIT_PATCHSET_NUMBER', value: "${env.GERRIT_PATCHSET_NUMBER}"),
string(name: 'GERRIT_EVENT_ACCOUNT_NAME', value: "${env.GERRIT_EVENT_ACCOUNT_NAME}"),
string(name: 'GERRIT_EVENT_ACCOUNT_EMAIL', value: "${env.GERRIT_EVENT_ACCOUNT_EMAIL}"),
string(name: 'GERRIT_CHANGE_COMMIT_MESSAGE', value: "${env.GERRIT_CHANGE_COMMIT_MESSAGE}"),
string(name: 'GERRIT_HOST', value: "${env.GERRIT_HOST}"),
string(name: 'GERGICH_PUBLISH', value: "0")
]
def getImageTagVersion() {
def flags = load 'build/new-jenkins/groovy/commit-flags.groovy'
return flags.getImageTagVersion()
}
def runBuildImageMaybe(save_success, block) {
def flags = load 'build/new-jenkins/groovy/commit-flags.groovy'
if (flags.hasFlag('skip-docker-build')) {
echo "skip building image requested"
}
else {
skipIfPreviouslySuccessful("build-and-push-image", save_success, block)
}
}
pipeline {
agent { label 'canvas-docker' }
options {
ansiColor('xterm')
}
environment {
// include selenium while smoke is running locally
COMPOSE_FILE = 'docker-compose.new-jenkins.yml:docker-compose.new-jenkins-selenium.yml'
GERRIT_PORT = '29418'
GERRIT_URL = "$GERRIT_HOST:$GERRIT_PORT"
NAME = getImageTagVersion()
PATCHSET_TAG = "$DOCKER_REGISTRY_FQDN/jenkins/canvas-lms:$NAME"
MERGE_TAG = "$DOCKER_REGISTRY_FQDN/jenkins/canvas-lms:$GERRIT_BRANCH"
CACHE_TAG = "canvas-lms:previous-image"
POSTGRES_CACHE_TAG = "canvas-lms:previous-postgres-image"
CASSANDRA_CACHE_TAG = "canvas-lms:previous-cassandra-image"
DYNAMODB_CACHE_TAG = "canvas-lms:previous-dynamodb-image"
}
stages {
stage('Print Env Variables') {
steps {
timeout(time: 20, unit: 'SECONDS') {
sh 'printenv | sort'
}
}
}
stage('Plugins and Config Files') {
steps {
timeout(time: 3) {
script {
def credentials = load 'build/new-jenkins/groovy/credentials.groovy'
credentials.fetchFromGerrit('gerrit_builder', '.', '', 'canvas-lms/config')
gems = readFile('gerrit_builder/canvas-lms/config/plugins_list').split()
println "Plugin list: ${gems}"
/* fetch plugins */
gems.each { gem ->
if (env.GERRIT_PROJECT == gem) {
/* this is the commit we're testing */
credentials.fetchFromGerrit(gem, 'gems/plugins', null, null, env.GERRIT_REFSPEC)
} else {
credentials.fetchFromGerrit(gem, 'gems/plugins')
}
}
credentials.fetchFromGerrit('qti_migration_tool', 'vendor', 'QTIMigrationTool')
sh '''
mv gerrit_builder/canvas-lms/config/* config/
mv config/knapsack_rspec_report.json ./
rm config/cache_store.yml
rmdir -p gerrit_builder/canvas-lms/config
cp docker-compose/config/selenium.yml config/
cp -R docker-compose/config/new-jenkins config/new-jenkins
cp config/delayed_jobs.yml.example config/delayed_jobs.yml
cp config/domain.yml.example config/domain.yml
cp config/external_migration.yml.example config/external_migration.yml
cp config/outgoing_mail.yml.example config/outgoing_mail.yml
'''
}
}
}
}
stage('Rebase') {
when { expression { env.GERRIT_EVENT_TYPE == 'patchset-created' } }
steps {
timeout(time: 2) {
script {
def credentials = load 'build/new-jenkins/groovy/credentials.groovy'
credentials.withGerritCredentials({ ->
sh '''
GIT_SSH_COMMAND='ssh -i \"$SSH_KEY_PATH\" -l \"$SSH_USERNAME\"' \
git fetch origin $GERRIT_BRANCH
git config user.name "$GERRIT_EVENT_ACCOUNT_NAME"
git config user.email "$GERRIT_EVENT_ACCOUNT_EMAIL"
# this helps current build issues where cleanup is needed before proceeding.
# however the later git rebase --abort should be enough once this has
# been on jenkins for long enough to hit all nodes, maybe a couple days?
if [ -d .git/rebase-merge ]; then
echo "A previous build's rebase failed and the build exited without cleaning up. Aborting the previous rebase now..."
git rebase --abort
git checkout $GERRIT_REFSPEC
fi
# store exit_status inline to ensures the script doesn't exit here on failures
git rebase --preserve-merges origin/$GERRIT_BRANCH; exit_status=$?
if [ $exit_status != 0 ]; then
echo "Warning: Rebase couldn't resolve changes automatically, please resolve these conflicts locally."
git rebase --abort
exit $exit_status
fi
'''
})
}
}
}
}
stage('Build Image') {
steps {
runBuildImageMaybe(false) {
timeout(time: 36) { /* this timeout is `2 * average build time` which currently: 18m * 2 = 36m */
dockerCacheLoad(image: "$CACHE_TAG")
sh '''
docker build -t $PATCHSET_TAG .
docker tag $PATCHSET_TAG $CACHE_TAG
'''
}
}
}
}
stage('Publish Patchset Image') {
steps {
runBuildImageMaybe(true) {
timeout(time: 5) {
// always push the patchset tag otherwise when a later
// patchset is merged this patchset tag is overwritten
sh 'docker push $PATCHSET_TAG'
}
}
}
}
stage('Parallel Run Tests') {
parallel {
// TODO: this is temporary until we can get some actual builds passing
stage('Smoke Test') {
steps {
skipIfPreviouslySuccessful("smoke-test") {
timeout(time: 10) {
script {
sh 'build/new-jenkins/docker-compose-pull-selenium.sh'
def dbCommon = load 'build/new-jenkins/groovy/cache-migrations.groovy'
dbCommon.createMigrateBuildUpCached()
sh 'build/new-jenkins/smoke-test.sh'
if (env.GERRIT_EVENT_TYPE == 'change-merged') {
dbCommon.storeMigratedImages()
}
}
}
}
}
}
stage('Linters') {
steps {
skipIfPreviouslySuccessful("linters") {
build(
job: 'test-suites/linters',
propagate: false,
parameters: build_parameters
)
}
}
}
stage('Vendored Gems') {
steps {
skipIfPreviouslySuccessful("vendored-gems") {
build(
job: 'test-suites/vendored-gems',
parameters: build_parameters
)
}
}
}
/*
* Don't run these on all patch sets until we have them ready to report results.
* Uncomment stage to run when developing.
* stage('Selenium Chrome') {
* steps {
* skipIfPreviouslySuccessful("selenium-chrome") {
* // propagate set to false until we can get tests passing
* build(
* job: 'test-suites/selenium-chrome',
* propagate: false,
* parameters: build_parameters
* )
* }
* }
* }
*
* stage('Rspec') {
* steps {
* skipIfPreviouslySuccessful("rspec") {
* // propagate set to false until we can get tests passing
* build(
* job: 'test-suites/rspec',
* propagate: false,
* parameters: build_parameters
* )
* }
* }
* }
*
* stage('Selenium Performance Chrome') {
* steps {
* skipIfPreviouslySuccessful("selenium-performance-chrome") {
* // propagate set to false until we can get tests passing
* build(
* job: 'test-suites/selenium-performance-chrome',
* propagate: false,
* parameters: build_parameters
* )
* }
* }
* }
*
* stage('Contract Tests') {
* steps {
* skipIfPreviouslySuccessful("contract-tests") {
* // propagate set to false until we can get tests passing
* build(
* job: 'test-suites/contract-tests',
* propagate: false,
* parameters: build_parameters
* )
* }
* }
* }
*
* stage('Frontend') {
* steps {
* skipIfPreviouslySuccessful("frontend") {
* // propagate set to false until we can get tests passing
* build(
* job: 'test-suites/frontend',
* propagate: false,
* parameters: build_parameters
* )
* }
* }
* }
*
* stage('Xbrowser') {
* steps {
* skipIfPreviouslySuccessful("xbrowser") {
* // propagate set to false until we can get tests passing
* build(
* job: 'test-suites/xbrowser',
* propagate: false,
* parameters: build_parameters
* )
* }
* }
* }
*/
}
}
stage('Publish Merged Image') {
when { expression { env.GERRIT_EVENT_TYPE == 'change-merged' } }
steps {
timeout(time: 10) {
script {
sh '''
docker tag $PATCHSET_TAG $MERGE_TAG
docker push $MERGE_TAG
'''
dockerCacheStore(image: "$CACHE_TAG")
}
}
}
}
}
post {
failure {
script {
if ( env.GERRIT_EVENT_TYPE == 'change-merged' ) {
slackSend (channel: '#canvas_builds',
color: '#da0005',
message: "${env.JOB_NAME} failed on merge (<${env.BUILD_URL}|${env.BUILD_NUMBER}>)")
}
}
}
cleanup {
sh 'build/new-jenkins/docker-cleanup.sh'
}
}
}