forked from bcgov/queue-management
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile.groovy
567 lines (521 loc) · 24.6 KB
/
Jenkinsfile.groovy
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
// This Jenkins build requires a configmap called jenkin-config with the following in it:
//
// client_secret=<keycloak client secret>
// zap_with_url_staff=<queue frontend for staff URL>
// zap_with_url=<zap command including dev url for analysis>
// namespace=<openshift project namespace>
// url=<url of api>/api/v1/
// auth_url=<Keycloak domain>
// clientid=<keycload Client ID>
// realm=<keycloak realm>
// dev_namespace=<openshift dev namepaces for testing>
// userid_qtxn=postman tester
// password_qtxn=<cfms-postman-operator password>
// userid_nonqtxn=cfms-postman-non-operator userid>
// password_nonqtxn=<cfms-postman-non-operator password>
// public_user_id=cfms-postman-public-user
// public_user_password=<cfms-postman-public-user password>
// public_url=<public api url>
// sonarqube_key=<sonarqube key>
def WAIT_TIMEOUT = 20
def TAG_NAMES = ['dev', 'test', 'prod']
def BUILDS = ['queue-management-api', 'queue-management-npm-build', 'queue-management-frontend', 'appointment-npm-build', 'appointment-frontend','send-appointment-reminder-crond']
def DEP_ENV_NAMES = ['dev', 'test', 'prod']
def label = "mypod-${UUID.randomUUID().toString()}"
def API_IMAGE_HASH = ""
def FRONTEND_IMAGE_HASH = ""
def APPOINTMENT_IMAGE_HASH = ""
def REMINDER_IMAGE_HASH = ""
String getNameSpace() {
def NAMESPACE = sh (
script: 'oc describe configmap jenkin-config | awk -F "=" \'/^namespace/{print $2}\'',
returnStdout: true
).trim()
return NAMESPACE
}
// Get an image's hash tag
String getImageTagHash(String imageName, String tag = "") {
if(!tag?.trim()) {
tag = "latest"
}
def istag = openshift.raw("get istag ${imageName}:${tag} -o template --template='{{.image.dockerImageReference}}'")
return istag.out.tokenize('@')[1].trim()
}
podTemplate(
label: label,
name: 'jenkins-agent-nodejs',
serviceAccount: 'jenkins',
cloud: 'openshift',
containers: [
containerTemplate(
name: 'jnlp',
image: 'registry.redhat.io/openshift3/jenkins-agent-nodejs-12-rhel7',
resourceRequestCpu: '500m',
resourceLimitCpu: '1000m',
resourceRequestMemory: '3Gi',
resourceLimitMemory: '4Gi',
workingDir: '/tmp',
command: '',
args: '${computer.jnlpmac} ${computer.name}'
)
]
){
node(label) {
stage('Checkout Source') {
echo "checking out source"
checkout scm
}
stage('SonarQube Analysis') {
echo ">>> Performing static analysis <<<"
SONAR_ROUTE_NAME = 'sonarqube'
SONAR_ROUTE_NAMESPACE = sh (
script: 'oc describe configmap jenkin-config | awk -F "=" \'/^namespace/{print $2}\'',
returnStdout: true
).trim()
SONAR_PROJECT_NAME = 'Queue Management'
SONAR_PROJECT_KEY = 'queue-management'
SONAR_PROJECT_BASE_DIR = sh (
script: "pwd",
returnStdout: true
).trim()
SONAR_SOURCES = 'api,frontend,appointment-frontend,jobs'
SONARQUBE_PWD = sh (
script: 'oc describe configmap jenkin-config | awk -F "=" \'/^sonarqube_key/{print $2}\'',
returnStdout: true
).trim()
SONARQUBE_URL = sh (
script: 'oc get routes -o wide --no-headers | awk \'/sonarqube/{ print match($0,/edge/) ? "https://"$2 : "http://"$2 }\'',
returnStdout: true
).trim()
dir('sonar-runner') {
sh (
returnStdout: true,
script: "./gradlew sonarqube --stacktrace --info \
-Dsonar.verbose=true \
-Dsonar.login=${SONARQUBE_PWD} \
-Dsonar.host.url=${SONARQUBE_URL} \
-Dsonar.projectName='${SONAR_PROJECT_NAME}' \
-Dsonar.projectKey=${SONAR_PROJECT_KEY} \
-Dsonar.projectBaseDir=${SONAR_PROJECT_BASE_DIR} \
-Dsonar.sources=${SONAR_SOURCES}"
)
}
}
parallel Build_Staff_FE_NPM: {
stage("Build Front End NPM..") {
script: {
openshift.withCluster() {
openshift.withProject() {
echo "Building Front End NPM"
openshift.selector("bc", "${BUILDS[1]}").startBuild("--wait")
}
echo "Staff Front End NPM Completed ..."
}
}
}
}, Build_Appointment_FE_NPM: {
stage("Build Appointment NPM") {
script: {
openshift.withCluster() {
openshift.withProject() {
echo "Bulding Appoitment Front End NPM"
openshift.selector("bc", "${BUILDS[3]}").startBuild("--wait")
}
echo "Appointment NPM ..."
}
}
}
}, Build_Api: {
stage("Build API..") {
script: {
openshift.withCluster() {
openshift.withProject() {
openshift.selector("bc", "${BUILDS[0]}").startBuild("--wait")
}
echo "API Build complete ..."
}
}
}
}, Build_Cron_Pod: {
stage("Build Mail Cron Pod..") {
script: {
openshift.withCluster() {
openshift.withProject() {
openshift.selector("bc", "${BUILDS[5]}").startBuild("--wait")
}
echo "Cron Mail Build complete ..."
}
}
}
}
parallel Build_Staff_FE: {
stage("Build Staff Front End ..") {
script: {
openshift.withCluster() {
openshift.withProject() {
echo "Building Front End Final"
openshift.selector("bc", "${BUILDS[2]}").startBuild("--wait")
}
echo "Staff Front End Completed ..."
}
}
}
}, Build_Appointment_FE: {
stage("Build Appointment Front End") {
script: {
openshift.withCluster() {
openshift.withProject() {
echo "Bulding Appoitment Front End Final"
openshift.selector("bc", "${BUILDS[4]}").startBuild("--wait")
}
echo "Appointment Online complete ..."
}
}
}
}
parallel Depoy_API_Dev: {
stage("Deploy API to Dev") {
script: {
openshift.withCluster() {
openshift.withProject() {
echo "Tagging ${BUILDS[0]} for deployment to ${TAG_NAMES[0]} ..."
// Don't tag with BUILD_ID so the pruner can do it's job; it won't delete tagged images.
// Tag the images for deployment based on the image's hash
API_IMAGE_HASH = getImageTagHash("${BUILDS[0]}")
echo "API_IMAGE_HASH: ${API_IMAGE_HASH}"
openshift.tag("${BUILDS[0]}@${API_IMAGE_HASH}", "${BUILDS[0]}:${TAG_NAMES[0]}")
}
def NAME_SPACE = getNameSpace()
openshift.withProject("${NAME_SPACE}-${DEP_ENV_NAMES[0]}") {
def dc = openshift.selector('dc', "${BUILDS[0]}")
// Wait for the deployment to complete.
// This will wait until the desired replicas are all available
dc.rollout().status()
}
echo "API Deployment Complete."
}
}
}
}, Depoy_Cron_Dev: {
stage("Deploy Email Cron to Dev") {
script: {
openshift.withCluster() {
openshift.withProject() {
echo "Tagging ${BUILDS[5]} for deployment to ${TAG_NAMES[0]} ..."
// Don't tag with BUILD_ID so the pruner can do it's job; it won't delete tagged images.
// Tag the images for deployment based on the image's hash
REMINDER_IMAGE_HASH = getImageTagHash("${BUILDS[5]}")
echo "REMINDER_IMAGE_HASH: ${REMINDER_IMAGE_HASH}"
openshift.tag("${BUILDS[5]}@${REMINDER_IMAGE_HASH}", "${BUILDS[5]}:${TAG_NAMES[0]}")
}
}
}
}
}, Deploy_Staff_FE_Dev: {
stage("Deploy Frontend to Dev") {
script: {
openshift.withCluster() {
openshift.withProject() {
echo "Tagging ${BUILDS[2]} for deployment to ${TAG_NAMES[0]} ..."
// Don't tag with BUILD_ID so the pruner can do it's job; it won't delete tagged images.
// Tag the images for deployment based on the image's hash
FRONTEND_IMAGE_HASH = getImageTagHash("${BUILDS[2]}")
echo "FRONTEND_IMAGE_HASH: ${FRONTEND_IMAGE_HASH}"
openshift.tag("${BUILDS[2]}@${FRONTEND_IMAGE_HASH}", "${BUILDS[2]}:${TAG_NAMES[0]}")
}
def NAME_SPACE = getNameSpace()
openshift.withProject("${NAME_SPACE}-${DEP_ENV_NAMES[0]}") {
dc = openshift.selector('dc', "${BUILDS[2]}")
// Wait for the deployment to complete.
// This will wait until the desired replicas are all available
dc.rollout().status()
}
echo "Front End Deployment Complete."
}
}
}
}, Deploy_Appointment_Dev: {
stage("Deploy Appointment to Dev") {
script: {
openshift.withCluster() {
openshift.withProject() {
echo "Tagging ${BUILDS[4]} for deployment to ${TAG_NAMES[0]} ..."
// Don't tag with BUILD_ID so the pruner can do it's job; it won't delete tagged images.
// Tag the images for deployment based on the image's hash
APPOINTMENT_IMAGE_HASH = getImageTagHash("${BUILDS[4]}")
echo "APPOINTMENT_IMAGE_HASH: ${APPOINTMENT_IMAGE_HASH}"
openshift.tag("${BUILDS[4]}@${APPOINTMENT_IMAGE_HASH}", "${BUILDS[4]}:${TAG_NAMES[0]}")
}
def NAME_SPACE = getNameSpace()
openshift.withProject("${NAME_SPACE}-${DEP_ENV_NAMES[0]}") {
dc = openshift.selector('dc', "${BUILDS[4]}")
// Wait for the deployment to complete.
// This will wait until the desired replicas are all available
dc.rollout().status()
}
echo "Appointment Online Complete."
}
}
}
}
stage('Newman Tests') {
dir('api/postman') {
sh (
returnStdout: true,
script: "npm init -y"
)
sh (
returnStdout: true,
script: "npm install newman"
)
USERID = sh (
script: 'oc describe configmap jenkin-config | awk -F "=" \'/^userid_qtxn/{print $2}\'',
returnStdout: true
).trim()
PASSWORD = sh (
script: 'oc describe configmap jenkin-config | awk -F "=" \'/^password_qtxn/{print $2}\'',
returnStdout: true
).trim()
USERID_NONQTXN = sh (
script: 'oc describe configmap jenkin-config | awk -F "=" \'/^userid_nonqtxn/{print $2}\'',
returnStdout: true
).trim()
PASSWORD_NONQTXN = sh (
script: 'oc describe configmap jenkin-config | awk -F "=" \'/^password_nonqtxn/{print $2}\'',
returnStdout: true
).trim()
CLIENT_SECRET = sh (
script: 'oc describe configmap jenkin-config | awk -F "=" \'/^client_secret/{print $2}\'',
returnStdout: true
).trim()
REALM = sh (
script: 'oc describe configmap jenkin-config | awk -F "=" \'/^realm/{print $2}\'',
returnStdout: true
).trim()
API_URL = sh (
script: 'oc describe configmap jenkin-config | awk -F "=" \'/^url/{print $2}\'',
returnStdout: true
).trim()
AUTH_URL = sh (
script: 'oc describe configmap jenkin-config | awk -F "=" \'/auth_url/{print $2}\'',
returnStdout: true
).trim()
CLIENTID = sh (
script: 'oc describe configmap jenkin-config | awk -F "=" \'/^clientid/{print $2}\'',
returnStdout: true
).trim()
PUBLIC_USERID = sh (
script: 'oc describe configmap jenkin-config | awk -F "=" \'/^public_user_id/{print $2}\'',
returnStdout: true
).trim()
PASSWORD_PUBLIC_USER = sh (
script: 'oc describe configmap jenkin-config | awk -F "=" \'/^public_user_password/{print $2}\'',
returnStdout: true
).trim()
PUBLIC_API_URL = sh (
script: 'oc describe configmap jenkin-config | awk -F "=" \'/^public_url/{print $2}\'',
returnStdout: true
).trim()
NODE_OPTIONS='--max_old_space_size=2048'
sleep(time:10,unit:"SECONDS")
sh (
returnStdout: true,
script: "./node_modules/newman/bin/newman.js run API_Test_TheQ_Booking.json --delay-request 250 -e postman_env.json --global-var 'userid=${USERID}' --global-var 'password=${PASSWORD}' --global-var 'userid_nonqtxn=${USERID_NONQTXN}' --global-var 'password_nonqtxn=${PASSWORD_NONQTXN}' --global-var 'client_secret=${CLIENT_SECRET}' --global-var 'url=${API_URL}' --global-var 'auth_url=${AUTH_URL}' --global-var 'clientid=${CLIENTID}' --global-var 'realm=${REALM}' --global-var public_url=${PUBLIC_API_URL} --global-var public_user_id=${PUBLIC_USERID} --global-var public_user_password=${PASSWORD_PUBLIC_USER}"
)
}
}
}
}
node {
stage("Deploy to test") {
input "Deploy to test?"
}
}
node {
parallel Depoy_API_Test: {
stage("Deploy API - test") {
script: {
openshift.withCluster() {
openshift.withProject() {
echo "Tagging ${BUILDS[0]} for deployment to ${TAG_NAMES[1]} ..."
// Don't tag with BUILD_ID so the pruner can do it's job; it won't delete tagged images.
// Tag the images for deployment based on the image's hash
echo "API_IMAGE_HASH: ${API_IMAGE_HASH}"
openshift.tag("${BUILDS[0]}@${API_IMAGE_HASH}", "${BUILDS[0]}:${TAG_NAMES[1]}")
}
def NAME_SPACE = getNameSpace()
openshift.withProject("${NAME_SPACE}-${DEP_ENV_NAMES[1]}") {
def dc = openshift.selector('dc', "${BUILDS[0]}")
// Wait for the deployment to complete.
// This will wait until the desired replicas are all available
dc.rollout().status()
}
echo "API Deployment Complete."
}
}
}
}, Deploy_Staff_FE_Test: {
stage("Deploy Frontend - Test") {
script: {
openshift.withCluster() {
openshift.withProject() {
echo "Tagging ${BUILDS[2]} for deployment to ${TAG_NAMES[1]} ..."
// Don't tag with BUILD_ID so the pruner can do it's job; it won't delete tagged images.
// Tag the images for deployment based on the image's hash
echo "FRONTEND_IMAGE_HASH: ${FRONTEND_IMAGE_HASH}"
openshift.tag("${BUILDS[2]}@${FRONTEND_IMAGE_HASH}", "${BUILDS[2]}:${TAG_NAMES[1]}")
}
def NAME_SPACE = getNameSpace()
openshift.withProject("${NAME_SPACE}-${DEP_ENV_NAMES[1]}") {
dc = openshift.selector('dc', "${BUILDS[2]}")
// Wait for the deployment to complete.
// This will wait until the desired replicas are all available
dc.rollout().status()
}
echo "Front End Deployment Complete."
}
}
}
}, Deploy_Appointment_Test: {
stage("Deploy Appointment - Test") {
script: {
openshift.withCluster() {
openshift.withProject() {
echo "Tagging ${BUILDS[4]} for deployment to ${TAG_NAMES[1]} ..."
// Don't tag with BUILD_ID so the pruner can do it's job; it won't delete tagged images.
// Tag the images for deployment based on the image's hash
echo "APPOINTMENT_IMAGE_HASH: ${APPOINTMENT_IMAGE_HASH}"
openshift.tag("${BUILDS[4]}@${APPOINTMENT_IMAGE_HASH}", "${BUILDS[4]}:${TAG_NAMES[1]}")
}
def NAME_SPACE = getNameSpace()
openshift.withProject("${NAME_SPACE}-${DEP_ENV_NAMES[1]}") {
dc = openshift.selector('dc', "${BUILDS[4]}")
// Wait for the deployment to complete.
// This will wait until the desired replicas are all available
dc.rollout().status()
}
echo "Front End Deployment Complete."
}
}
}
}, Deploy_Cron_Email_Test: {
stage("Deploy Appt Reminder - test") {
script: {
openshift.withCluster() {
openshift.withProject() {
echo "Tagging ${BUILDS[5]} for deployment to ${TAG_NAMES[1]} ..."
// Don't tag with BUILD_ID so the pruner can do it's job; it won't delete tagged images.
// Tag the images for deployment based on the image's hash
echo "REMINDER_IMAGE_HASH: ${REMINDER_IMAGE_HASH}"
openshift.tag("${BUILDS[5]}@${REMINDER_IMAGE_HASH}", "${BUILDS[5]}:${TAG_NAMES[1]}")
}
echo "Appt Reminder Deployment Complete."
}
}
}
}
}
node {
stage("Deploy to prod") {
input "Deploy to Prod?"
}
}
node {
stage("Update Production") {
script: {
openshift.withCluster() {
openshift.withProject() {
echo "Tagging Production to Stable"
openshift.tag("${BUILDS[0]}:prod", "${BUILDS[0]}:stable")
openshift.tag("${BUILDS[2]}:prod", "${BUILDS[2]}:stable")
openshift.tag("${BUILDS[4]}:prod", "${BUILDS[4]}:stable")
openshift.tag("${BUILDS[5]}:prod", "${BUILDS[5]}:stable")
}
}
}
}
}
node {
parallel Depoy_API_Prod: {
stage("Deploy API - Prod") {
script: {
openshift.withCluster() {
openshift.withProject() {
echo "Tagging ${BUILDS[0]} for deployment to ${TAG_NAMES[2]} ..."
// Don't tag with BUILD_ID so the pruner can do it's job; it won't delete tagged images.
// Tag the images for deployment based on the image's hash
echo "API_IMAGE_HASH: ${API_IMAGE_HASH}"
openshift.tag("${BUILDS[0]}@${API_IMAGE_HASH}", "${BUILDS[0]}:${TAG_NAMES[2]}")
}
def NAME_SPACE = getNameSpace()
openshift.withProject("${NAME_SPACE}-${DEP_ENV_NAMES[2]}") {
def dc = openshift.selector('dc', "${BUILDS[0]}")
// Wait for the deployment to complete.
// This will wait until the desired replicas are all available
dc.rollout().status()
}
echo "API Deployment Complete."
}
}
}
}, Deploy_Staff_FE_Prod: {
stage("Deploy Frontend - Prod") {
script: {
openshift.withCluster() {
openshift.withProject() {
echo "Tagging ${BUILDS[2]} for deployment to ${TAG_NAMES[2]} ..."
// Don't tag with BUILD_ID so the pruner can do it's job; it won't delete tagged images.
// Tag the images for deployment based on the image's hash
echo "FRONTEND_IMAGE_HASH: ${FRONTEND_IMAGE_HASH}"
openshift.tag("${BUILDS[2]}@${FRONTEND_IMAGE_HASH}", "${BUILDS[2]}:${TAG_NAMES[2]}")
}
def NAME_SPACE = getNameSpace()
openshift.withProject("${NAME_SPACE}-${DEP_ENV_NAMES[2]}") {
dc = openshift.selector('dc', "${BUILDS[2]}")
// Wait for the deployment to complete.
// This will wait until the desired replicas are all available
dc.rollout().status()
}
echo "Front End Deployment Complete."
}
}
}
}, Deploy_Appointment_Prod: {
stage("Deploy Appointment - Prod") {
script: {
openshift.withCluster() {
openshift.withProject() {
echo "Tagging ${BUILDS[4]} for deployment to ${TAG_NAMES[2]} ..."
// Don't tag with BUILD_ID so the pruner can do it's job; it won't delete tagged images.
// Tag the images for deployment based on the image's hash
echo "APPOINTMENT_IMAGE_HASH: ${APPOINTMENT_IMAGE_HASH}"
openshift.tag("${BUILDS[4]}@${APPOINTMENT_IMAGE_HASH}", "${BUILDS[4]}:${TAG_NAMES[2]}")
}
def NAME_SPACE = getNameSpace()
openshift.withProject("${NAME_SPACE}-${DEP_ENV_NAMES[2]}") {
dc = openshift.selector('dc', "${BUILDS[4]}")
// Wait for the deployment to complete.
// This will wait until the desired replicas are all available
dc.rollout().status()
}
echo "Front End Deployment Complete."
}
}
}
}, Deploy_Cron_Email_Prod: {
stage("Deploy Appt Reminders - Prod") {
script: {
openshift.withCluster() {
openshift.withProject() {
echo "Tagging ${BUILDS[5]} for deployment to ${TAG_NAMES[2]} ..."
// Don't tag with BUILD_ID so the pruner can do it's job; it won't delete tagged images.
// Tag the images for deployment based on the image's hash
echo "REMINDER_IMAGE_HASH: ${REMINDER_IMAGE_HASH}"
openshift.tag("${BUILDS[5]}@${REMINDER_IMAGE_HASH}", "${BUILDS[5]}:${TAG_NAMES[2]}")
}
echo "Appt Reminders Deployment Complete."
}
}
}
}
}