forked from bcgov/queue-management
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
403 lines (355 loc) · 15.6 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
// 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']
def DEP_ENV_NAMES = ['dev', 'test', 'prod']
def label = "mypod-${UUID.randomUUID().toString()}"
def API_IMAGE_HASH = ""
def FRONTEND_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 <<<"
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 -Dsonar.host.url=${SONARQUBE_URL} --stacktrace --info"
)
}
}
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("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("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("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('Newman Tests') {
dir('api/postman') {
sh "ls -alh"
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()
PUBLICID = sh (
script: 'oc describe configmap jenkin-config | awk -F "=" \'/^public_user_id/{print $2}\'',
returnStdout: true
).trim()
PUBLICPW = sh (
script: 'oc describe configmap jenkin-config | awk -F "=" \'/^public_user_password/{print $2}\'',
returnStdout: true
).trim()
PUBLICURL = 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 -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_user_id=${PUBLICID}' --global-var 'public_user_password=${PUBLICPW}' --global-var 'public_url=${PUBLICURL}'"
)
}
}
}
}
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 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."
}
}
}
}
node {
stage("Deploy API - Prod") {
input "Deploy to 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."
}
}
}
}