forked from bcgov/queue-management
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JenkinsfileSBC
578 lines (522 loc) · 24.2 KB
/
JenkinsfileSBC
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
568
569
570
571
572
573
574
575
576
577
578
// This Jenkins build requires a configmap called jenkin-config with the following in it:
//
// password_qtxn=<cfms-postman-operator userid password>
// password_nonqtxn=<cfms-postman-non-operator userid password>
// client_secret=<keycloak client secret>
// zap_with_url=<zap command including dev url for analysis>
// namespace=<openshift project namespace>
// url=<url of api>/api/v1/
// authurl=<Keycloak domain>
// clientid=<keycload Client ID>
// realm=<keycloak realm>
def WAIT_TIMEOUT = 10
def TAG_NAMES = ['dev', 'test', 'production']
def BUILDS = ['queue-management-api', 'queue-management-npm-build', 'queue-management-frontend', 'appointment-npm-build', 'appointment-frontend','send-appointment-reminder']
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-python3nodejs',
serviceAccount: 'jenkins',
cloud: 'openshift',
containers: [
containerTemplate(
name: 'jnlp',
image: '172.50.0.2:5000/openshift/jenkins-slave-python3nodejs',
resourceRequestCpu: '1000m',
resourceLimitCpu: '2000m',
resourceRequestMemory: '2Gi',
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 = '../'
SONAR_SOURCES = './'
SONARQUBE_PWD = sh (
script: 'oc set env dc/sonarqube --list | awk -F "=" \'/SONARQUBE_ADMINPW/{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()
echo "PWD: ${SONARQUBE_PWD}"
echo "URL: ${SONARQUBE_URL}"
dir('sonar-runner') {
sh (
returnStdout: true,
script: "./gradlew sonarqube --stacktrace --info \
-Dsonar.verbose=true \
-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}"
)
}
}
stage("Build API..") {
script: {
openshift.withCluster() {
openshift.withProject() {
// Find all of the build configurations associated to the application using labels ...
def bc = openshift.selector("bc", "${BUILDS[0]}")
echo "Started builds: ${bc.names()}"
bc.startBuild("--wait").logs("-f")
}
echo "API Build complete ..."
}
}
}
stage("Build Appt Reminder..") {
script: {
openshift.withCluster() {
openshift.withProject() {
// Find all of the build configurations associated to the application using labels ...
def bc = openshift.selector("bc", "${BUILDS[5]}")
echo "Started builds: ${bc.names()}"
bc.startBuild("--wait").logs("-f")
}
echo "Appt Reminder Build complete ..."
}
}
}
stage("Build Front End..") {
script: {
openshift.withCluster() {
openshift.withProject() {
// Find all of the build configurations associated to the application using labels ...
bc = openshift.selector("bc", "${BUILDS[1]}")
echo "Started builds: ${bc.names()}"
bc.startBuild("--wait").logs("-f")
bc = openshift.selector("bc", "${BUILDS[2]}")
echo "Started builds: ${bc.names()}"
bc.startBuild("--wait").logs("-f")
}
echo "Front End complete ..."
}
}
}
stage("Build Appointment") {
script: {
openshift.withCluster() {
openshift.withProject() {
// Find all of the build configurations associated to the application using labels ...
bc = openshift.selector("bc", "${BUILDS[3]}")
echo "Started builds: ${bc.names()}"
bc.startBuild("--wait").logs("-f")
bc = openshift.selector("bc", "${BUILDS[4]}")
echo "Started builds: ${bc.names()}"
bc.startBuild("--wait").logs("-f")
}
echo "Appointment Online complete ..."
}
}
}
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."
}
}
}
stage("Deploy Email Reminder 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]}")
}
}
}
}
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."
}
}
}
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 "ls -alh"
sh (
returnStdout: true,
script: "npm init -y"
)
sh (
returnStdout: true,
script: "npm install newman@4.6.1"
)
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'
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}"
)
}
}
}
}
def owaspPodLabel = "owasp-zap-${UUID.randomUUID().toString()}"
podTemplate(
label: owaspPodLabel,
name: owaspPodLabel,
serviceAccount: 'jenkins',
cloud: 'openshift',
containers: [ containerTemplate(
name: 'jnlp',
image: '172.50.0.2:5000/openshift/jenkins-slave-zap',
resourceRequestCpu: '500m',
resourceLimitCpu: '1000m',
resourceRequestMemory: '3Gi',
resourceLimitMemory: '4Gi',
workingDir: '/home/jenkins',
command: '',
args: '${computer.jnlpmac} ${computer.name}'
)]
) {
node(owaspPodLabel) {
stage('ZAP Security Scan') {
sleep 60
ZAP_WITH_URL = sh (
script: 'oc describe configmap jenkin-config | awk -F "=" \'/^zap_with_url/{print $2}\'',
returnStdout: true
).trim()
def retVal = sh (
returnStatus: true,
script: "${ZAP_WITH_URL}"
)
publishHTML([
allowMissing: false,
alwaysLinkToLastBuild: false,
keepAll: true,
reportDir: '/zap/wrk',
reportFiles: 'baseline.html',
reportName: 'ZAP_Baseline_Scan',
reportTitles: 'ZAP_Baseline_Scan'
])
echo "Return value is: ${retVal}"
script {
if (retVal != 0) {
echo "MARKING BUILD AS UNSTABLE"
currentBuild.result = 'UNSTABLE'
}
}
}
}
}
node {
stage("Deploy API - test") {
input "Deploy to 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."
}
}
}
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."
}
}
}
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."
}
}
}
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."
}
}
}
}
node {
stage("Update Stable") {
input "Deploy to Prod?"
script: {
openshift.withCluster() {
openshift.withProject() {
echo "Tagging Production to Stable"
openshift.tag("${BUILDS[0]}:production", "${BUILDS[0]}:stable")
openshift.tag("${BUILDS[2]}:production", "${BUILDS[2]}:stable")
openshift.tag("${BUILDS[4]}:production", "${BUILDS[4]}:stable")
openshift.tag("${BUILDS[5]}:production", "${BUILDS[5]}:stable")
}
}
}
}
}
node {
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."
}
}
}
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."
}
}
}
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."
}
}
}
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."
}
}
}
}