-
Notifications
You must be signed in to change notification settings - Fork 2
/
Jenkinsfile
334 lines (330 loc) · 11.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
// Set up the libraries
@Library('socrata-pipeline-library')
import com.socrata.ReleaseMetadataService
def rmsSupportedEnvironment = com.socrata.ReleaseMetadataService.SupportedEnvironment
// set up service and project variables
String service_server = 'soql-server-pg'
String project_wd_server = 'soql-server-pg'
String service_secondary = 'secondary-watcher-pg'
String project_wd_secondary = 'store-pg'
boolean isPr = env.CHANGE_ID != null
boolean isHotfix = isHotfixBranch(env.BRANCH_NAME)
boolean skip = false
String lastStage
// instanciate libraries
def sbtbuild = new com.socrata.SBTBuild(steps, service_server, '.', [project_wd_server, project_wd_secondary])
def dockerize_server = new com.socrata.Dockerize(steps, service_server, BUILD_NUMBER)
def dockerize_secondary = new com.socrata.Dockerize(steps, service_secondary, BUILD_NUMBER)
def releaseTag = new com.socrata.ReleaseTag(steps, service_server)
pipeline {
options {
ansiColor('xterm')
buildDiscarder(logRotator(numToKeepStr: '50'))
disableConcurrentBuilds(abortPrevious: true)
timeout(time: 100, unit: 'MINUTES')
}
parameters {
string(name: 'AGENT', defaultValue: 'build-worker-pg13', description: 'Which build agent to use?')
string(name: 'BRANCH_SPECIFIER', defaultValue: 'origin/main', description: 'Use this branch for building the artifact.')
booleanParam(name: 'RELEASE_BUILD', defaultValue: false, description: 'Are we building a release candidate?')
booleanParam(name: 'RELEASE_DRY_RUN', defaultValue: false, description: 'To test out the release build.')
string(name: 'RELEASE_NAME', description: 'For release builds, the release name which is used in the git tag and the build id.')
}
agent {
label params.AGENT
}
environment {
DEPLOY_PATTERN = "${service_server}*"
SECONDARY_DEPLOY_PATTERN = "${service_secondary}*"
WEBHOOK_ID = 'WEBHOOK_IQ'
}
stages {
stage('Hotfix') {
when {
expression { isHotfix }
}
steps {
script {
lastStage = env.STAGE_NAME
if (releaseTag.noCommitsOnHotfixBranch(env.BRANCH_NAME)) {
skip = true
echo "SKIP: Skipping the rest of the build because there are no commits on the hotfix branch yet"
} else {
env.CURRENT_RELEASE_NAME = releaseTag.getReleaseName(env.BRANCH_NAME)
env.HOTFIX_NAME = releaseTag.getHotfixName(env.CURRENT_RELEASE_NAME)
}
}
}
}
stage('Build') {
when {
not { expression { skip } }
}
steps {
script {
lastStage = env.STAGE_NAME
sbtbuild.setScalaVersion("2.12")
// This build is a little unusual; it actually has _two_ artifacts, this one
// and one for store-pg-assembly. sbtbuild isn't really set up to override names
// for two separate artifacts, so here we override the primary, and we'll override
// the secondary down below when it's passed to docker.
sbtbuild.setSrcJar("soql-server-pg/target/soql-server-pg-assembly.jar")
env.STORE_PG_ARTIFACT = "store-pg/target/store-pg-assembly.jar"
sbtbuild.build()
// This needs to be set before the docker build stages since they run in parallel
if (params.RELEASE_BUILD || isHotfix) {
env.VERSION = (isHotfix) ? env.HOTFIX_NAME : params.RELEASE_NAME
}
}
}
}
stage('Docker Build:') {
when {
allOf {
not { expression { isPr } }
not { expression { skip } }
}
}
parallel {
stage ('Docker Build - SoQL Server') {
steps {
script {
lastStage = env.STAGE_NAME
if (params.RELEASE_BUILD || isHotfix) {
env.DOCKER_TAG = dockerize_server.dockerBuildWithSpecificTag(
tag: env.VERSION,
path: sbtbuild.getDockerPath(project_wd_server),
artifacts: [sbtbuild.getDockerArtifact(project_wd_server)]
)
} else {
env.DOCKER_TAG = dockerize_server.dockerBuildWithDefaultTag(
version: 'STAGING',
sha: env.GIT_COMMIT,
path: sbtbuild.getDockerPath(project_wd_server),
artifacts: [sbtbuild.getDockerArtifact(project_wd_server)]
)
}
}
}
}
stage ('Docker Build - Secondary') {
steps {
script {
lastStage = env.STAGE_NAME
// Here's where we're getting the secondary artifact (named
// via env.STORE_PG_ARTIFACT) out for dockerizing, per the
// comment above.
if (params.RELEASE_BUILD || isHotfix) {
env.SECONDARY_DOCKER_TAG = dockerize_secondary.dockerBuildWithSpecificTag(
tag: env.VERSION,
path: sbtbuild.getDockerPath(project_wd_secondary),
artifacts: [env.STORE_PG_ARTIFACT]
)
} else {
env.SECONDARY_DOCKER_TAG = dockerize_secondary.dockerBuildWithDefaultTag(
version: 'STAGING',
sha: env.GIT_COMMIT,
path: sbtbuild.getDockerPath(project_wd_secondary),
artifacts: [env.STORE_PG_ARTIFACT]
)
}
}
}
}
}
post {
success {
script {
if (isHotfix) {
env.GIT_TAG = releaseTag.create(env.HOTFIX_NAME)
} else if (params.RELEASE_BUILD) {
env.GIT_TAG = releaseTag.getFormattedTag(params.RELEASE_NAME)
if (releaseTag.doesReleaseTagExist(params.RELEASE_NAME)) {
echo "REBUILD: Tag ${env.GIT_TAG} already exists"
return
}
if (params.RELEASE_DRY_RUN) {
echo "DRY RUN: Would have created ${env.GIT_TAG} and pushed it to the repo"
currentBuild.description = "${service_server}:${params.RELEASE_NAME} and ${service_secondary}:${params.RELEASE_NAME} - DRY RUN"
return
}
releaseTag.create(params.RELEASE_NAME)
}
}
}
}
}
stage('Publish:') {
when {
allOf {
not { expression { isPr } }
not { expression { skip } }
not { expression { return params.RELEASE_BUILD && params.RELEASE_DRY_RUN } }
}
}
stages {
stage('Publish - SoQL Server') {
steps {
script {
lastStage = env.STAGE_NAME
if (isHotfix || params.RELEASE_BUILD) {
env.BUILD_ID = dockerize_server.publish(sourceTag: env.DOCKER_TAG)
} else {
env.BUILD_ID = dockerize_server.publish(
sourceTag: env.DOCKER_TAG,
environments: ['internal']
)
}
}
}
post {
success {
script {
if (isHotfix || params.RELEASE_BUILD) {
// These environment variables apply for both services
// They cannot be run in parallel because in the post stages there are race conditions
env.PURPOSE = (isHotfix) ? 'hotfix' : 'initial'
env.RELEASE_ID = (isHotfix) ? env.CURRENT_RELEASE_NAME : params.RELEASE_NAME
Map buildInfo = [
"project_id": service_server,
"build_id": env.BUILD_ID,
"release_id": env.RELEASE_ID,
"git_tag": env.GIT_TAG,
"purpose": env.PURPOSE,
]
createBuild(
buildInfo,
rmsSupportedEnvironment.production
)
}
}
}
}
}
stage('Publish - Secondary') {
steps {
script {
lastStage = env.STAGE_NAME
if (isHotfix || params.RELEASE_BUILD) {
env.SECONDARY_BUILD_ID = dockerize_secondary.publish(sourceTag: env.SECONDARY_DOCKER_TAG)
} else {
env.SECONDARY_BUILD_ID = dockerize_secondary.publish(
sourceTag: env.SECONDARY_DOCKER_TAG,
environments: ['internal']
)
}
}
}
post {
success {
script {
if (isHotfix || params.RELEASE_BUILD) {
Map buildInfo = [
"project_id": service_secondary,
"build_id": env.SECONDARY_BUILD_ID,
"release_id": env.RELEASE_ID,
"git_tag": env.GIT_TAG,
"purpose": env.PURPOSE,
]
createBuild(
buildInfo,
rmsSupportedEnvironment.production
)
}
}
}
}
}
}
post {
success {
script {
currentBuild.description = "${env.BUILD_ID} & ${env.SECONDARY_BUILD_ID}"
}
}
}
}
stage('Deploy:') {
when {
allOf {
not { expression { isPr } }
not { expression { skip } }
not { expression { return params.RELEASE_BUILD} }
}
}
stages {
stage('Deploy - SoQL Server') {
steps {
script {
lastStage = env.STAGE_NAME
env.ENVIRONMENT = (isHotfix) ? 'rc' : 'staging'
marathonDeploy(
serviceName: env.DEPLOY_PATTERN,
tag: env.BUILD_ID,
environment: env.ENVIRONMENT,
waitTime: '60'
)
}
}
post {
success {
script {
if (isHotfix) {
Map deployInfo = [
"build_id": env.BUILD_ID,
"environment": env.ENVIRONMENT,
]
createDeployment(
deployInfo,
rmsSupportedEnvironment.production
)
}
}
}
}
}
stage('Deploy - Secondary') {
steps {
script {
lastStage = env.STAGE_NAME
marathonDeploy(
serviceName: env.SECONDARY_DEPLOY_PATTERN,
tag: env.SECONDARY_BUILD_ID,
environment: env.ENVIRONMENT,
)
}
}
post {
success {
script {
if (isHotfix) {
Map deployInfo = [
"build_id": env.SECONDARY_BUILD_ID,
"environment": env.ENVIRONMENT,
]
createDeployment(
deployInfo,
rmsSupportedEnvironment.production
)
}
}
}
}
}
}
}
}
post {
failure {
script {
boolean buildingMain = (env.JOB_NAME.contains("${service_server}/main"))
if (buildingMain) {
teamsMessage(
message: "[${currentBuild.fullDisplayName}](${env.BUILD_URL}) has failed in stage ${lastStage}",
webhookCredentialID: WEBHOOK_ID
)
}
}
}
}
}