forked from bcgov/gwells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
444 lines (402 loc) · 19.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
438
// Jenkinsfile (Scripted Pipeline)
/* Gotchas
- PodTemplate name/label has to be unique.
otherwise,there is some configuration caching that won't actually take the latest configuration
e.g.: Changing image, envvars, so on.
maybe only when overwriting global templates?
References:
- https://gist.github.com/matthiasbalke/3c9ecccbea1d460ee4c3fbc5843ede4a
*/
import hudson.model.Result;
import jenkins.model.CauseOfInterruption.UserInterruption;
import org.kohsuke.github.*
import bcgov.OpenShiftHelper
import bcgov.GitHubHelper
@NonCPS
private static String stackTraceAsString(Throwable t) {
StringWriter sw = new StringWriter();
t.printStackTrace(new PrintWriter(sw));
return sw.toString()
}
def _stage(String name, Map context, Closure body) {
def stageOpt =(context?.stages?:[:])[name]
boolean isEnabled=(stageOpt == null || stageOpt == true)
//echo "Stage - ${stage}"
echo "Running Stage '${name}' - enabled:${isEnabled}"
if (isEnabled){
stage(name) {
waitUntil {
boolean isDone=false
try{
body()
isDone=true
}catch (ex){
echo "${stackTraceAsString(ex)}"
def inputAction = input(
message: "This step (${name}) has failed. See error above.",
ok: 'Confirm',
parameters: [choice(name: 'action', choices: 'Re-run\nIgnore', description: 'What would you like to do?')]
)
if ('Ignore'.equalsIgnoreCase(inputAction)){
isDone=true
}
}
return isDone
} //end waitUntil
} //end Stage
}else{
stage(name) {
echo 'Skipping'
}
}
}
Map context = [
'name': 'gwells',
'uuid' : UUID.randomUUID().toString(),
'env': [
'dev':[:],
'test':[:],
'prod':['params':['host':'gwells-prod.pathfinder.gov.bc.ca', 'DB_PVC_SIZE':'5Gi']]
],
'templates': [
'build':[
['file':'openshift/postgresql.bc.json'],
['file':'openshift/backend.bc.json']
],
'deployment':[
['file':'openshift/postgresql.dc.json',
'params':[
'DATABASE_SERVICE_NAME':'gwells-pgsql${deploy.dcSuffix}',
'IMAGE_STREAM_NAMESPACE':'',
'IMAGE_STREAM_NAME':'gwells-postgresql${deploy.dcSuffix}',
'IMAGE_STREAM_VERSION':'${deploy.envName}',
'POSTGRESQL_DATABASE':'gwells',
'VOLUME_CAPACITY':'${env[DEPLOY_ENV_NAME]?.params?.DB_PVC_SIZE?:"1Gi"}'
]
],
['file':'openshift/backend.dc.json', 'params':['HOST':'${env[DEPLOY_ENV_NAME]?.params?.host?:("gwells" + deployments[DEPLOY_ENV_NAME].dcSuffix + "-" + deployments[DEPLOY_ENV_NAME].projectName + ".pathfinder.gov.bc.ca")}']]
]
],
stages:[
'Build': true,
'Unit Test': true,
'Code Quality': false,
'Readiness - DEV': true,
'Deploy - DEV': true,
'Full Test - DEV': true
]
]
properties([
buildDiscarder(logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '', numToKeepStr: '10')),
durabilityHint('MAX_SURVIVABILITY') /*, parameters([string(defaultValue: '', description: '', name: 'run_stages')]) */
])
stage('Prepare') {
abortAllPreviousBuildInProgress(currentBuild)
echo "BRANCH_NAME=${env.BRANCH_NAME}\nCHANGE_ID=${env.CHANGE_ID}\nCHANGE_TARGET=${env.CHANGE_TARGET}\nBUILD_URL=${env.BUILD_URL}"
}
/**
This function wrapper allows stages to be optional/skipped.
*/
_stage('Build', context) {
node('master') {
checkout scm
new OpenShiftHelper().build(this, context)
if ("master".equalsIgnoreCase(env.CHANGE_TARGET)) {
new OpenShiftHelper().prepareForCD(this, context)
}
}
} //end stage
_stage('Unit Test', context) {
podTemplate(label: "node-${context.uuid}", name:"node-${context.uuid}", serviceAccount: 'jenkins', cloud: 'openshift', containers: [
containerTemplate(name: 'jnlp', image: 'jenkins/jnlp-slave:3.10-1-alpine', args: '${computer.jnlpmac} ${computer.name}', resourceRequestCpu: '100m',resourceLimitCpu: '1000m'),
containerTemplate(name: 'app', image: "docker-registry.default.svc:5000/moe-gwells-tools/gwells${context.buildNameSuffix}:${context.buildEnvName}", ttyEnabled: true, command: 'cat',
resourceRequestCpu: '1000m',
resourceLimitCpu: '4000m',
resourceRequestMemory: '1Gi',
resourceLimitMemory: '4Gi')
]
) {
node("node-${context.uuid}") {
try {
container('app') {
sh script: '''#!/usr/bin/container-entrypoint /bin/sh
set -x
python --version
pip --version
node --version
npm --version
(cd /opt/app-root/src && python manage.py migrate)
(cd /opt/app-root/src && export ENABLE_DATA_ENTRY="True" && export NOSE_PROCESSES=4 && python manage.py test -c nose.cfg)
(cd /opt/app-root/src/frontend && npm test)
mkdir -p frontend/test/
cp -R /opt/app-root/src/frontend/test/unit ./frontend/test/
cp /opt/app-root/src/nosetests.xml ./
cp /opt/app-root/src/coverage.xml ./
cp /opt/app-root/src/frontend/junit.xml ./frontend/
'''
}
} finally {
archiveArtifacts allowEmptyArchive: true, artifacts: 'frontend/test/unit/**/*'
stash includes: 'nosetests.xml,coverage.xml', name: 'coverage'
stash includes: 'frontend/test/unit/coverage/clover.xml', name: 'nodecoverage'
stash includes: 'frontend/junit.xml', name: 'nodejunit'
junit 'nosetests.xml,frontend/junit.xml'
publishHTML (target: [
allowMissing: false,
alwaysLinkToLastBuild: false,
keepAll: true,
reportDir: 'frontend/test/unit/coverage/lcov-report/',
reportFiles: 'index.html',
reportName: "Node Coverage Report"
])
}
}
}
} //end stage
_stage('Code Quality', context) {
podTemplate(
name: "sonar-runner${context.uuid}",
label: "sonar-runner${context.uuid}",
serviceAccount: 'jenkins',
cloud: 'openshift',
containers:[
containerTemplate(
name: 'jnlp',
resourceRequestMemory: '1Gi',
resourceLimitMemory: '4Gi',
resourceRequestCpu: '500m',
resourceLimitCpu: '4000m',
image: 'registry.access.redhat.com/openshift3/jenkins-slave-maven-rhel7:v3.7',
workingDir: '/tmp',
args: '${computer.jnlpmac} ${computer.name}',
envVars: [
envVar(key:'GRADLE_USER_HOME', value: '/var/cache/artifacts/gradle')
]
)
],
volumes: [
persistentVolumeClaim(mountPath: '/var/cache/artifacts', claimName: 'cache', readOnly: false)
]
){
node("sonar-runner${context.uuid}") {
//the checkout is mandatory, otherwise code quality check would fail
echo "checking out source"
echo "Build: ${BUILD_ID}"
checkout scm
String SONARQUBE_URL = 'https://sonarqube-moe-gwells-tools.pathfinder.gov.bc.ca'
echo "SONARQUBE_URL: ${SONARQUBE_URL}"
dir('app') {
unstash 'nodejunit'
unstash 'nodecoverage'
}
dir('sonar-runner') {
unstash 'coverage'
sh './gradlew -q dependencies'
sh returnStdout: true, script: "./gradlew sonarqube -Dsonar.host.url=${SONARQUBE_URL} -Dsonar.verbose=true --stacktrace --info -Dsonar.sources=.."
}
}
}
} //end stage
def isCI = !"master".equalsIgnoreCase(env.CHANGE_TARGET)
def isCD = "master".equalsIgnoreCase(env.CHANGE_TARGET)
for(String envKeyName: context.env.keySet() as String[]){
String stageDeployName=envKeyName.toUpperCase()
if ("DEV".equalsIgnoreCase(stageDeployName) || isCD) {
_stage("Readiness - ${stageDeployName}", context) {
node('master') {
new OpenShiftHelper().waitUntilEnvironmentIsReady(this, context, envKeyName)
}
}
}
if (!"DEV".equalsIgnoreCase(stageDeployName) && isCD){
_stage("Approve - ${stageDeployName}", context) {
def inputResponse = null;
try{
inputResponse = input(id: "deploy_${stageDeployName.toLowerCase()}", message: "Deploy to ${stageDeployName}?", ok: 'Approve', submitterParameter: 'approved_by')
}catch(ex){
error "Pipeline has been aborted. - ${ex}"
}
//echo "inputResponse:${inputResponse}"
GitHubHelper.getPullRequest(this).comment("User '${inputResponse}' has approved deployment to '${stageDeployName}'")
}
}
if ("DEV".equalsIgnoreCase(stageDeployName) || isCD){
_stage("Deploy - ${stageDeployName}", context) {
node('master') {
new OpenShiftHelper().deploy(this, context, envKeyName)
}
}
}
if ("DEV".equalsIgnoreCase(stageDeployName)){
_stage("Load Fixtures - ${stageDeployName}", context) {
node('master'){
String podName=null
String projectName=context.deployments[envKeyName].projectName
String deploymentConfigName="gwells${context.deployments[envKeyName].dcSuffix}"
echo "env:${context.env[envKeyName]}"
echo "deployment:${context.deployments[envKeyName]}"
echo "projectName:${projectName}"
echo "deploymentConfigName:${deploymentConfigName}"
openshift.withProject(projectName){
podName=openshift.selector('pod', ['deploymentconfig':deploymentConfigName]).objects()[0].metadata.name
}
sh "oc exec '${podName}' -n '${projectName}' -- bash -c 'cd /opt/app-root/src && pwd && python manage.py flush --no-input'"
sh "oc exec '${podName}' -n '${projectName}' -- bash -c 'cd /opt/app-root/src && pwd && python manage.py loaddata wells registries'"
}
}
}
if ("DEV".equalsIgnoreCase(stageDeployName)){
_stage('API Test', context) {
String baseURL = context.deployments[envKeyName].environmentUrl.substring(0, context.deployments[envKeyName].environmentUrl.indexOf('/', 8) + 1)
podTemplate(label: "nodejs-${context.uuid}", name: "nodejs-${context.uuid}", serviceAccount: 'jenkins', cloud: 'openshift', containers: [
containerTemplate(
name: 'jnlp',
image: 'registry.access.redhat.com/openshift3/jenkins-slave-nodejs-rhel7',
resourceRequestCpu: '500m',
resourceLimitCpu: '1000m',
resourceRequestMemory: '1Gi',
resourceLimitMemory: '4Gi',
workingDir: '/tmp',
command: '',
args: '${computer.jnlpmac} ${computer.name}',
envVars: [
envVar(key:'BASEURL', value: "${baseURL}gwells"),
secretEnvVar(key: 'GWELLS_API_TEST_USER', secretName: 'apitest-secrets', secretKey: 'username'),
secretEnvVar(key: 'GWELLS_API_TEST_PASSWORD', secretName: 'apitest-secrets', secretKey: 'password'),
secretEnvVar(key: 'GWELLS_API_TEST_AUTH_SERVER', secretName: 'apitest-secrets', secretKey: 'auth_server'),
secretEnvVar(key: 'GWELLS_API_TEST_CLIENT_ID', secretName: 'apitest-secrets', secretKey: 'client_id'),
secretEnvVar(key: 'GWELLS_API_TEST_CLIENT_SECRET', secretName: 'apitest-secrets', secretKey: 'client_secret')
]
)
],envVars: [
envVar(key:'BASEURL', value: "${baseURL}gwells"),
secretEnvVar(key: 'GWELLS_API_TEST_USER', secretName: 'apitest-secrets', secretKey: 'username'),
secretEnvVar(key: 'GWELLS_API_TEST_PASSWORD', secretName: 'apitest-secrets', secretKey: 'password'),
secretEnvVar(key: 'GWELLS_API_TEST_AUTH_SERVER', secretName: 'apitest-secrets', secretKey: 'auth_server'),
secretEnvVar(key: 'GWELLS_API_TEST_CLIENT_ID', secretName: 'apitest-secrets', secretKey: 'client_id'),
secretEnvVar(key: 'GWELLS_API_TEST_CLIENT_SECRET', secretName: 'apitest-secrets', secretKey: 'client_secret')
])
{
node("nodejs-${context.uuid}") {
//the checkout is mandatory, otherwise functional test would fail
echo "checking out source"
echo "Build: ${BUILD_ID}"
echo "baseURL: ${baseURL}"
sh '''#!/bin/bash
echo BASEURL=$BASEURL
'''
//input(message: "Verify Environment variables. Continue?")
checkout scm
dir('api-tests') {
sh 'npm install -g newman'
try {
sh 'newman run ./registries_api_tests.json --global-var test_user=$GWELLS_API_TEST_USER --global-var test_password=$GWELLS_API_TEST_PASSWORD --global-var base_url="${BASEURL}" --global-var auth_server=$GWELLS_API_TEST_AUTH_SERVER --global-var client_id=$GWELLS_API_TEST_CLIENT_ID --global-var client_secret=$GWELLS_API_TEST_CLIENT_SECRET -r cli,junit,html;'
} finally {
junit 'newman/*.xml'
publishHTML (target: [
allowMissing: false,
alwaysLinkToLastBuild: false,
keepAll: true,
reportDir: 'newman',
reportFiles: 'newman*.html',
reportName: "API Test Report"
])
stash includes: 'newman/*.xml', name: 'api-tests'
}
} // end dir
} //end node
} //end podTemplate
} //end stage
}
if ("DEV".equalsIgnoreCase(stageDeployName) || isCD){
String testStageName="DEV".equalsIgnoreCase(stageDeployName)?"Full Test - DEV":"Smoke Test - ${stageDeployName}"
_stage(testStageName, context){
String baseURL = context.deployments[envKeyName].environmentUrl.substring(0, context.deployments[envKeyName].environmentUrl.indexOf('/', 8) + 1)
podTemplate(label: "bddstack-${context.uuid}", name: "bddstack-${context.uuid}", serviceAccount: 'jenkins', cloud: 'openshift',
containers: [
containerTemplate(
name: 'jnlp',
image: 'docker-registry.default.svc:5000/openshift/jenkins-slave-bddstack',
resourceRequestCpu: '500m',
resourceLimitCpu: '2000m',
resourceRequestMemory: '2Gi',
resourceLimitMemory: '4Gi',
workingDir: '/home/jenkins',
command: '',
args: '${computer.jnlpmac} ${computer.name}',
envVars: [
envVar(key:'BASEURL', value: baseURL),
envVar(key:'GRADLE_USER_HOME', value: '/var/cache/artifacts/gradle')
]
)
],
volumes: [
persistentVolumeClaim(mountPath: '/var/cache/artifacts', claimName: 'cache', readOnly: false)
]
){
node("bddstack-${context.uuid}") {
echo "Build: ${BUILD_ID}"
echo "baseURL: ${baseURL}"
sh 'echo "BASEURL=${BASEURL}"'
sh 'echo "GRADLE_USER_HOME=${GRADLE_USER_HOME}"'
//the checkout is mandatory, otherwise functional test would fail
echo "checking out source"
checkout scm
/*
dir('functional-tests/build/test-results') {
sh 'echo "BASEURL=${BASEURL}"'
unstash 'coverage'
sh 'rm coverage.xml'
unstash 'nodejunit'
}
*/
//dir('app') {
// sh 'python manage.py loaddata wells registries'
//}
dir('functional-tests') {
try {
//sh './gradlew -q dependencies'
if ("DEV".equalsIgnoreCase(stageDeployName)){
sh './gradlew chromeHeadlessTest'
}else{
sh './gradlew -DchromeHeadlessTest.single=WellDetails chromeHeadlessTest'
}
} finally {
archiveArtifacts allowEmptyArchive: true, artifacts: 'build/reports/geb/**/*'
junit testResults:'build/test-results/**/*.xml', allowEmptyResults:true
publishHTML (target: [
allowMissing: true,
alwaysLinkToLastBuild: false,
keepAll: true,
reportDir: 'build/reports/spock',
reportFiles: 'index.html',
reportName: "Test: BDD Spock Report"
])
publishHTML (target: [
allowMissing: true,
alwaysLinkToLastBuild: false,
keepAll: true,
reportDir: 'build/reports/tests/chromeHeadlessTest',
reportFiles: 'index.html',
reportName: "Test: Full Test Report"
])
//todo: install perf report plugin.
//perfReport compareBuildPrevious: true, excludeResponseTime: true, ignoreFailedBuilds: true, ignoreUnstableBuilds: true, modeEvaluation: true, modePerformancePerTestCase: true, percentiles: '0,50,90,100', relativeFailedThresholdNegative: 80.0, relativeFailedThresholdPositive: 20.0, relativeUnstableThresholdNegative: 50.0, relativeUnstableThresholdPositive: 50.0, sourceDataFiles: 'build/test-results/**/*.xml'
}
}
} //end node
} //end podTemplate
} //end stage
} //end if
} // end for
_stage('Cleanup', context) {
def inputResponse = null
try{
inputResponse=input(id: 'close_pr', message: "Ready to Accept/Merge, and Close pull-request #${env.CHANGE_ID}?", ok: 'Yes', submitter: 'authenticated', submitterParameter: 'approver')
}catch(ex){
error "Pipeline has been aborted. - ${ex}"
}
echo "inputResponse:${inputResponse}"
new OpenShiftHelper().cleanup(this, context)
GitHubHelper.mergeAndClosePullRequest(this)
}