forked from mozilla/bedrock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
216 lines (208 loc) · 8.01 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
#!groovy
@Library('github.com/mozmar/jenkins-pipeline@20170214.1')
def config
def utils
stage ('Checkout') {
node {
checkout scm
sh 'git submodule sync'
sh 'git submodule update --init --recursive'
// clean up
sh 'make clean'
// load the config
config = readYaml file: 'jenkins.yml'
// load the utility functions used below
utils = load 'jenkins/utils.groovy'
// defined in the Library loaded above
setGitEnvironmentVariables()
setConfigEnvironmentVariables(config)
// save the files for later
stash name: 'scripts', includes: 'bin/,docker/'
stash name: 'tests', includes: 'tests/,requirements/'
stash 'workspace'
}
}
if ( config.branches.containsKey(env.BRANCH_NAME) ) {
def branchConfig = config.branches[env.BRANCH_NAME]
milestone()
stage ('Build Images') {
node {
unstash 'workspace'
// make sure we should continue
if ( branchConfig.require_tag ) {
try {
sh 'docker/bin/check_if_tag.sh'
} catch(err) {
utils.ircNotification(config, [stage: 'Git Tag Check', status: 'failure'])
throw err
}
}
utils.ircNotification(config, [stage: 'Test & Deploy', status: 'starting'])
lock ("bedrock-docker-${env.GIT_COMMIT}") {
try {
sh 'docker/bin/build_images.sh --prod --test'
} catch(err) {
utils.ircNotification(config, [stage: 'Docker Build', status: 'failure'])
throw err
}
}
}
}
if ( branchConfig.smoke_tests ) {
milestone()
stage ('Test Images') {
parallel([
smoke_tests: utils.integrationTestJob('smoke'),
unit_tests: {
node {
unstash 'scripts'
try {
sh 'docker/bin/run_tests.sh'
} catch(err) {
utils.ircNotification(config, [stage: 'Unit Test', status: 'failure'])
throw err
}
}
},
])
}
}
if ( branchConfig.push_public_registry ) {
milestone()
stage ('Push Public Images') {
node {
unstash 'scripts'
try {
utils.pushDockerhub('mozorg/bedrock_base')
utils.pushDockerhub('mozorg/bedrock_code')
utils.pushDockerhub('mozorg/bedrock_l10n', 'mozorg/bedrock')
} catch(err) {
utils.ircNotification(config, [stage: 'Dockerhub Push Failed', status: 'warning'])
}
}
}
}
/**
* Do region first because deployment and testing should work like this:
* region1:
* push image -> deploy app1 -> test app1 -> deploy app2 -> test app2
* region2:
* push image -> deploy app1 -> test app1 -> deploy app2 -> test app2
*
* A failure at any step of the above should fail the entire job
*/
if ( branchConfig.apps ) {
milestone()
// default to usw only
def regions = branchConfig.regions ?: ['usw']
for (regionId in regions) {
def region = config.regions[regionId]
def stageName = "Private Push: ${region.name}"
stage (stageName) {
node {
unstash 'scripts'
try {
utils.pushPrivateReg(region.registry_port, branchConfig.apps)
} catch(err) {
utils.ircNotification(config, [stage: stageName, status: 'failure'])
throw err
}
}
}
for (appname in branchConfig.apps) {
def appSuffix = branchConfig.app_name_suffix ?: ''
def appURL = "https://${appname}${appSuffix}.${region.name}.moz.works"
stageName = "Deploy ${appname}-${region.name}"
// ensure no deploy/test cycle happens in parallel for an app/region
lock (stageName) {
milestone()
stage (stageName) {
node {
unstash 'scripts'
withEnv(["DEIS_PROFILE=${region.deis_profile}",
"DOCKER_REPOSITORY=${appname}",
"DEIS_APPLICATION=${appname}"]) {
try {
retry(3) {
sh 'docker/bin/push2deis.sh'
}
} catch(err) {
utils.ircNotification(config, [stage: stageName, status: 'failure'])
throw err
}
}
}
}
if ( branchConfig.apps ) {
// queue up test closures
def allTests = [:]
for (filename in branchConfig.integration_tests) {
allTests[filename] = utils.integrationTestJob(filename, appURL)
}
stage ("Test ${appname}-${region.name}") {
try {
// wait for server to be ready
sleep(time: 10, unit: 'SECONDS')
parallel allTests
} catch(err) {
node {
unstash 'scripts'
utils.ircNotification(config, [stage: "Integration Tests ${appname}-${region.name}", status: 'failure'])
}
throw err
}
}
}
node {
unstash 'scripts'
// huge success \o/
utils.ircNotification(config, [message: appURL, status: 'shipped'])
}
}
}
}
}
}
/**
* Deploy demo branches
*/
else if ( env.BRANCH_NAME ==~ /^demo__[\w-]+$/ ) {
node {
utils.ircNotification(config, [stage: 'Demo Deploy', status: 'starting'])
stage ('build') {
lock ("bedrock-docker-${env.GIT_COMMIT}") {
milestone()
try {
sh 'docker/bin/build_images.sh --demo'
} catch(err) {
utils.ircNotification(config, [stage: 'Demo Build', status: 'failure'])
throw err
}
}
}
def appname = utils.demoAppName(env.BRANCH_NAME)
stage ('deploy') {
lock (appname) {
milestone()
try {
withCredentials([[$class: 'StringBinding',
credentialsId: 'SENTRY_DEMO_DSN',
variable: 'SENTRY_DEMO_DSN']]) {
withEnv(['DEIS_PROFILE=usw',
"DEIS_APP_NAME=${appname}",
"PRIVATE_REGISTRY=localhost:${config.regions.usw.registry_port}"]) {
sh './docker/bin/demo_deploy.sh'
}
}
} catch(err) {
utils.ircNotification(config, [stage: 'Demo Deploy', status: 'failure'])
throw err
}
}
}
utils.ircNotification(config, [message: utils.demoAppURL(appname), status: 'shipped'])
}
}
else {
echo "Doing nothing for ${env.BRANCH_NAME}"
}