-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jenkinsfile
437 lines (407 loc) · 21.8 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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// Common Variables
slackChannel = 'research-hub'
slackCredentials = 'UoA-Slack-Access-Research-Hub'
awsProfile = ''
pipeline {
parameters {
booleanParam(name: "FORCE_REDEPLOY_WEB", defaultValue: false, description: 'Force redeploy the web frontend even if there are no code changes.' )
booleanParam(name: "FORCE_REDEPLOY_CG", defaultValue: false, description: 'Force redeploy the cer-graphql API even if there are no code changes.')
booleanParam(name: "FORCE_REDEPLOY_SP", defaultValue: false, description: 'Force redeploy the search-proxy Lambda even if there are no code changes.')
booleanParam(name: "FORCE_REDEPLOY_SLC", defaultValue: false, description: 'Force redeploy the subhub-link-checker Contentful App even if there are no code changes.')
}
agent {
label("uoa-buildtools-ionic")
}
options {
buildDiscarder(
logRotator(
numToKeepStr: "3"
)
)
}
stages {
stage('Checkout') {
steps {
checkout scm
slackSend(channel: slackChannel, tokenCredentialId: slackCredentials, color: "#D4DADF", message: "Build Started - ${env.JOB_NAME} ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)")
}
}
stage('Set environment variables') {
steps {
script {
echo 'Setting environment variables'
env.awsRegion = 'ap-southeast-2'
if (BRANCH_NAME.matches('sandbox(.*)')) {
echo 'Setting variables for sandbox deployment'
env.BRANCH_NAME = 'sandbox'
env.awsCredentialsId = 'aws-sandbox-user'
env.awsTokenId = 'aws-sandbox-token'
env.awsAccountId = '416527880812'
awsProfile = 'uoa-sandbox'
env.SCHEMA_PATH = 'https://rhubcpapi.sandbox.amazon.auckland.ac.nz/'
} else if (BRANCH_NAME.matches('dev(.*)')) {
echo 'Setting variables for dev deployment'
env.BRANCH_NAME = 'dev'
env.awsCredentialsId = 'aws-its-nonprod-access'
env.awsTokenId = 'aws-its-nonprod-token'
env.awsAccountId = '518380838815'
awsProfile = 'uoa-its-nonprod'
env.SCHEMA_PATH = 'https://rhubcpapi-dev.connect.test.amazon.auckland.ac.nz/cer-graphql-service/'
} else if (BRANCH_NAME == 'test') {
echo 'Setting variables for test deployment'
env.awsCredentialsId = 'aws-its-nonprod-access'
env.awsTokenId = 'aws-its-nonprod-token'
env.awsAccountId = '518380838815'
awsProfile = 'uoa-its-nonprod'
env.SCHEMA_PATH = 'https://rhubcpapi.connect.test.amazon.auckland.ac.nz/cer-graphql-service/'
} else if (BRANCH_NAME == 'prod') {
echo 'Setting variables for prod deployment'
env.awsCredentialsId = 'aws-its-prod'
env.awsTokenId = 'Access token for ITS Prod Account'
env.awsAccountId = '291148375163'
awsProfile = 'uoa-its-prod'
env.SCHEMA_PATH = 'https://rhubcpapi.auckland.ac.nz/cer-graphql-service/'
} else {
echo 'You are not on an environment branch, defaulting to sandbox'
env.BRANCH_NAME = 'sandbox'
env.awsCredentialsId = 'aws-sandbox-user'
env.awsTokenId = 'aws-sandbox-token'
env.awsAccountId = '416527880812'
awsProfile = 'uoa-sandbox'
env.SCHEMA_PATH = 'https://rhubcpapi.sandbox.amazon.auckland.ac.nz/'
}
echo "Copying in credentials file"
// Copy in secrets file from Jenkins so build and test
// work properly.
withCredentials([
file(credentialsId: "credentials-${BRANCH_NAME}",variable:"credentialsfile")
]) {
def filename = (
env.BRANCH_NAME == 'prod' ? '.prod.env' :
env.BRANCH_NAME == 'test' ? '.test.env' :
'.env'
)
sh "cp $credentialsfile ${filename}"
}
}
}
}
stage('AWS Credential Grab') {
steps{
script {
echo "☯ Authenticating with AWS"
withCredentials([
usernamePassword(credentialsId: "${awsCredentialsId}", passwordVariable: 'awsPassword', usernameVariable: 'awsUsername'),
string(credentialsId: "${awsTokenId}", variable: 'awsToken')
]) {
sh "python3 /home/jenkins/aws_saml_login.py --idp iam.auckland.ac.nz --user $awsUsername --password $awsPassword --token $awsToken --role devops --profile " + awsProfile
}
}
}
}
stage('Build cer-graphql, subhub-link-checker and search-proxy projects') {
stages {
stage('Build search-proxy') {
when {
anyOf {
changeset "**/hub-search-proxy/**/*.*"
equals expected: true, actual: params.FORCE_REDEPLOY_SP
}
}
steps {
dir("hub-search-proxy") {
echo 'Installing search-proxy dependencies...'
sh "npm install"
}
}
}
stage('Build cer-graphql') {
when {
anyOf {
changeset "**/cer-graphql/**/*.*"
equals expected: true, actual: params.FORCE_REDEPLOY_CG
}
}
steps {
echo 'Building cer-graphql project'
dir("cer-graphql") {
echo "Building the docker image and tag it as latest"
sh "docker build . -t cer-graphql:latest"
}
}
}
stage('Build subhub-link-checker') {
when {
anyOf {
changeset "**/subhub-link-checker/**/*.*"
equals expected: true, actual: params.FORCE_REDEPLOY_SLC
}
}
steps {
echo 'Building subhub-link-checker project'
dir("subhub-link-checker") {
echo 'Installing subhub-link-checker dependencies...'
sh "npm ci"
sh "npm run build"
}
}
}
}
}
stage('Run cer-graphql and search-proxy tests') {
stages {
stage('Run cer-graphql tests') {
when {
anyOf {
changeset "**/cer-graphql/**/*.*"
equals expected: true, actual: params.FORCE_REDEPLOY_CG
}
}
steps {
script {
if (env.BRANCH_NAME != 'prod') { // tests rely on 2FAB so we do not test in prod
echo 'Testing cer-graphql project'
dir('cer-graphql') {
sh "npm install"
sh "export stage=${BRANCH_NAME} && npm run test -- --aws-profile=${awsProfile}"
}
}
}
}
}
stage('Run search-proxy tests') {
when {
anyOf {
changeset "**/hub-search-proxy/**/*.*"
equals expected: true, actual: params.FORCE_REDEPLOY_SP
}
}
steps {
script {
if (env.BRANCH_NAME != 'prod') {
echo "Testing hub-search-proxy project"
dir('hub-search-proxy') {
sh "npm run test -- --aws-profile ${awsProfile} --stage ${env.BRANCH_NAME}"
}
}
}
}
}
}
}
stage('Deploy cer-graphql, search-proxy and subhub-link-checker projects') {
parallel {
stage('Deploy cer-graphql') {
when {
anyOf {
changeset "**/cer-graphql/**/*.*"
equals expected: true, actual: params.FORCE_REDEPLOY_CG
}
}
steps { // TODO: may need to modify commands once we have both dev & test in nonprod account
echo 'Deploying cer-graphql image to ECR on ' + BRANCH_NAME
echo "Logging in to ECR"
sh "(aws ecr get-login --no-include-email --region ${awsRegion} --profile=${awsProfile}) | /bin/bash"
echo "Tagging built image with ECR tag"
sh "docker tag cer-graphql:latest ${awsAccountId}.dkr.ecr.${awsRegion}.amazonaws.com/research-hub/cer-graphql-${BRANCH_NAME}:latest"
echo "Pushing built image to ECR"
sh "docker push ${awsAccountId}.dkr.ecr.${awsRegion}.amazonaws.com/research-hub/cer-graphql-${BRANCH_NAME}:latest"
echo 'Deploying cer-graphql image from ECR to Fargate on ' + BRANCH_NAME
sh "aws ecs update-service --profile ${awsProfile} --cluster cer-graphql-cluster-${BRANCH_NAME} --service cer-graphql-service-${BRANCH_NAME} --task-definition cer-graphql-${BRANCH_NAME} --force-new-deployment --region ${awsRegion}"
sh "aws ecs update-service --profile ${awsProfile} --cluster cer-graphql-cluster-${BRANCH_NAME} --service cer-graphql-preview-service-${BRANCH_NAME} --task-definition cer-graphql-preview-${BRANCH_NAME} --force-new-deployment --region ${awsRegion}"
}
}
stage('Deploy search-proxy') {
when {
anyOf {
changeset "**/hub-search-proxy/**/*.*"
equals expected: true, actual: params.FORCE_REDEPLOY_SP
}
}
steps {
echo "Deploying hub-search-proxy Lambda function to ${BRANCH_NAME}"
script {
dir("hub-search-proxy") {
sh "npm run deploy -- --aws-profile ${awsProfile} --stage ${BRANCH_NAME}"
}
}
echo "Deploy to ${BRANCH_NAME} complete"
}
}
stage('Deploy subhub-link-checker') {
when {
anyOf {
changeset "**/subhub-link-checker/**/*.*"
equals expected: true, actual: params.FORCE_REDEPLOY_SLC
}
}
steps {
dir("subhub-link-checker") {
// Grab relevant credentials
withCredentials([
string(credentialsId: "contentful-pat", variable: "contentfulPat"),
string(credentialsId: "contentful-link-checker-app-id-${BRANCH_NAME}", variable: "contentfulSLCAppId"),
string(credentialsId: "contentful-org-id", variable: "contentfulOrgId")
]) {
sh "npm run upload-ci -- --organization-id ${contentfulOrgId} --definition-id ${contentfulSLCAppId} --token ${contentfulPat}"
}
}
}
}
}
}
stage('Research Hub Web steps') {
stages {
stage('Build research-hub-web') {
when {
anyOf {
changeset "**/research-hub-web/**/*.*"
equals expected: true, actual: params.FORCE_REDEPLOY_WEB
}
}
stages {
stage ('Building and caching new node_modules') {
// when {
// anyOf {
// changeset "**/research-hub-web/package.json"
// equals expected: true, actual: params.FORCE_REDEPLOY_WEB
// }
// }
steps {
echo 'Installing research-hub-web dependencies.'
dir("research-hub-web") {
sh "npm install"
sh "mkdir -p ${HOME}/research-hub-web/"
// sh "tar cvfz ./node_modules.tar.gz node_modules" // Cache new node_modules/ folder
// script {
// archiveArtifacts artifacts: "node_modules.tar.gz", onlyIfSuccessful: true
// }
}
}
}
// stage ('Using cached node_modules from archive') {
// when {
// not {
// anyOf {
// changeset "**/research-hub-web/package.json"
// }
// }
// }
// steps {
// echo 'Building research-hub-web project from stored dependencies.'
// dir("research-hub-web") {
// copyArtifacts filter: 'node_modules.tar.gz', fingerprintArtifacts: true, optional: true, projectName: "Centre for eResearch (CeR)/hub-stack-pipeline/${BRANCH_NAME}" , selector: lastWithArtifacts()
// sh "tar xf ./node_modules.tar.gz" // Unzip cached node_modules/ folder
// sh "npm install"
// }
// }
// }
stage ('Building for production') {
steps {
dir("research-hub-web") {
echo 'Building for production'
sh "npm run build -- -c ${BRANCH_NAME}"
echo 'Building preview for production'
sh "npm run build -- -c ${BRANCH_NAME}-preview --output-path www-preview"
}
}
}
}
}
stage('Run research-hub-web tests') {
when {
anyOf {
changeset "**/research-hub-web/**/*.*"
equals expected: true, actual: params.FORCE_REDEPLOY_WEB
}
}
steps {
script {
echo 'Testing research-hub-web project'
// need to trim the trailing slash from the graphql server url for the e2e tests
def graphqlServer = env.SCHEMA_PATH.substring(0, env.SCHEMA_PATH.length() - 1)
dir("research-hub-web") {
echo 'Running research-hub-web unit tests'
sh 'npm run test-ci'
echo 'Running research-hub-web e2e tests'
// set the graphql server url as an env variable for Cypress
// for intercepting some of the graphql queries and returning mocked data
sh "export cypress_graphql_server=${graphqlServer} && npm run e2e-ci"
}
}
}
}
stage('Deploy research-hub-web') {
when {
anyOf {
changeset "**/research-hub-web/**/*.*"
equals expected: true, actual: params.FORCE_REDEPLOY_WEB
}
}
stages {
stage('Deploy to S3 bucket') {
steps {
script {
echo 'Deploying research-hub-web to S3 on ' + BRANCH_NAME
def s3BucketName = (
env.BRANCH_NAME == 'prod' ? 'research-hub.auckland.ac.nz' :
env.BRANCH_NAME == 'test' ? 'research-hub.connect.test.amazon.auckland.ac.nz' :
env.BRANCH_NAME == 'dev' ? 'research-hub-dev.connect.test.amazon.auckland.ac.nz' :
'research-hub-web'
)
def previewS3BucketName = (
env.BRANCH_NAME == 'prod' ? 'research-hub-preview.auckland.ac.nz' :
env.BRANCH_NAME == 'test' ? 'research-hub-preview.connect.test.amazon.auckland.ac.nz' :
env.BRANCH_NAME == 'dev' ? 'research-hub-dev-preview.connect.test.amazon.auckland.ac.nz' :
'research-hub-web-preview'
)
dir("research-hub-web") {
sh "aws s3 sync www s3://${s3BucketName} --delete --profile ${awsProfile}"
echo "Sync complete"
sh "aws s3 sync www-preview s3://${previewS3BucketName} --delete --profile ${awsProfile}"
echo "Preview sync complete"
}
}
}
}
stage('Invalidate CloudFront') {
steps {
script {
echo "Invalidating..."
// TODO: Enter dev/test/prod CloudFrontDistroIds
def awsCloudFrontDistroId = (
env.BRANCH_NAME == 'prod' ? 'E3P3Z3YL0II0MW' :
env.BRANCH_NAME == 'test' ? 'E1HU1AQ31JKDT9' :
env.BRANCH_NAME == 'dev' ? 'E35ROORLYFFYM4' :
'E20R95KPAKSWTG'
)
def previewAwsCloudFrontDistroId = (
env.BRANCH_NAME == 'prod' ? 'E1PEITWMDUR8EF' :
env.BRANCH_NAME == 'test' ? 'E1U7DUEU5EBP41' :
env.BRANCH_NAME == 'dev' ? 'E2MW26HILK658J' :
'E2GBENCKM7YT9Q'
)
echo "Cloudfront distro id: ${awsCloudFrontDistroId}"
sh "aws cloudfront create-invalidation --distribution-id ${awsCloudFrontDistroId} --paths '/*' --profile ${awsProfile}"
echo "Invalidation started"
sh "aws cloudfront create-invalidation --distribution-id ${previewAwsCloudFrontDistroId} --paths '/*' --profile ${awsProfile}"
echo "Preview invalidation started"
}
}
}
}
}
}
}
}
post {
success {
echo 'Deployed to ' + BRANCH_NAME
slackSend(channel: slackChannel, tokenCredentialId: slackCredentials, color: "#5eff00", message: "🚀 Deploy successful - ${env.JOB_NAME} ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>).")
}
failure {
echo 'Jenkins job failed :('
slackSend(channel: slackChannel, tokenCredentialId: slackCredentials, color: "#FF9FA1", message: "🔥 Build failed - ${env.JOB_NAME} ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)")
}
}
}