This repository has been archived by the owner on Apr 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Jenkinsfile.cicd
313 lines (271 loc) · 11.8 KB
/
Jenkinsfile.cicd
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
#!groovy
import bcgov.GitHubHelper
// --------------------
// Declarative Pipeline
// --------------------
pipeline {
agent any
environment {
// Enable pipeline verbose debug output if greater than 0
DEBUG_OUTPUT = 'false'
// Get projects/namespaces from config maps
DEV_PROJECT = new File('/var/run/configs/ns/project.dev').getText('UTF-8').trim()
TEST_PROJECT = new File('/var/run/configs/ns/project.test').getText('UTF-8').trim()
PROD_PROJECT = new File('/var/run/configs/ns/project.prod').getText('UTF-8').trim()
TOOLS_PROJECT = new File('/var/run/configs/ns/project.tools').getText('UTF-8').trim()
// Get application config from config maps
REPO_OWNER = new File('/var/run/configs/jobs/repo.owner').getText('UTF-8').trim()
REPO_NAME = new File('/var/run/configs/jobs/repo.name').getText('UTF-8').trim()
APP_NAME = new File('/var/run/configs/jobs/app.name').getText('UTF-8').trim()
APP_DOMAIN = new File('/var/run/configs/jobs/app.domain').getText('UTF-8').trim()
// JOB_NAME should be the pull request/branch identifier (i.e. 'pr-5')
JOB_NAME = JOB_BASE_NAME.toLowerCase()
// SOURCE_REPO_* references git repository resources
SOURCE_REPO_RAW = "https://raw.githubusercontent.com/${REPO_OWNER}/${REPO_NAME}/master"
SOURCE_REPO_REF = "pull/${CHANGE_ID}/head"
SOURCE_REPO_URL = "https://github.com/${REPO_OWNER}/${REPO_NAME}.git"
// HOST_ROUTE is the full domain route endpoint (ie. 'appname-pr-5-9f0fbe-dev.pathfinder.gov.bc.ca')
DEV_HOST_ROUTE = "${APP_NAME}-${JOB_NAME}-${DEV_PROJECT}.${APP_DOMAIN}"
// SonarQube Endpoint URL
SONARQUBE_URL_INT = 'http://sonarqube:9000'
SONARQUBE_URL_EXT = "https://sonarqube-${TOOLS_PROJECT}.${APP_DOMAIN}"
}
options {
parallelsAlwaysFailFast()
}
stages {
stage('Initialize') {
agent any
steps {
// Cancel any running builds in progress
timeout(10) {
echo "Cancelling previous ${APP_NAME}-${JOB_NAME} builds in progress..."
abortAllPreviousBuildInProgress(currentBuild)
}
script {
if (DEBUG_OUTPUT.equalsIgnoreCase('true')) {
// Force OpenShift Plugin directives to be verbose
openshift.logLevel(1)
// Print all environment variables
echo 'DEBUG - All pipeline environment variables:'
echo sh(returnStdout: true, script: 'env')
}
}
}
}
stage('Tests') {
agent any
steps {
notifyStageStatus('Tests', 'PENDING')
script {
dir('app') {
try {
timeout(10) {
echo 'Installing NPM Dependencies...'
sh 'npm ci'
echo 'Reporting Outdated and Vulnerable Dependencies...'
sh 'npm audit || true'
sh 'npm outdated || true'
echo 'Linting and Testing...'
sh 'npm run test'
echo 'Lint Checks and Tests passed'
}
} catch (e) {
echo 'Lint Checks and Tests failed'
throw e
}
}
echo 'Performing SonarQube static code analysis...'
sh """
sonar-scanner \
-Dsonar.host.url='${SONARQUBE_URL_INT}' \
-Dsonar.projectKey='${REPO_NAME}-${JOB_NAME}' \
-Dsonar.projectName='Common Document Generation Service (${JOB_NAME.toUpperCase()})'
"""
}
}
post {
success {
echo 'All Lint Checks and Tests passed'
notifyStageStatus('Tests', 'SUCCESS')
}
failure {
echo 'Some Lint Checks and Tests failed'
notifyStageStatus('Tests', 'FAILURE')
}
}
}
stage('Build') {
agent any
steps {
script {
openshift.withCluster() {
openshift.withProject(TOOLS_PROJECT) {
if (DEBUG_OUTPUT.equalsIgnoreCase('true')) {
echo "DEBUG - Using project: ${openshift.project()}"
}
try {
notifyStageStatus('Build', 'PENDING')
echo "Processing BuildConfig ${REPO_NAME}-app..."
def bcApp = openshift.process('-f',
'openshift/app.bc.yaml',
"REPO_NAME=${REPO_NAME}",
"JOB_NAME=${JOB_NAME}",
"SOURCE_REPO_URL=${SOURCE_REPO_URL}",
"SOURCE_REPO_REF=${SOURCE_REPO_REF}"
)
echo "Building ImageStream ${REPO_NAME}-app..."
openshift.apply(bcApp).narrow('bc').startBuild('-w').logs('-f')
echo "Tagging Image ${REPO_NAME}-app:${JOB_NAME}..."
openshift.tag("${REPO_NAME}-app:latest",
"${REPO_NAME}-app:${JOB_NAME}"
)
echo 'App build successful'
notifyStageStatus('Build', 'SUCCESS')
} catch (e) {
echo 'App build failed'
notifyStageStatus('Build', 'FAILURE')
throw e
}
}
}
}
}
post {
success {
echo 'Cleanup BuildConfigs...'
script {
openshift.withCluster() {
openshift.withProject(TOOLS_PROJECT) {
if (DEBUG_OUTPUT.equalsIgnoreCase('true')) {
echo "DEBUG - Using project: ${openshift.project()}"
} else {
def bcApp = openshift.selector('bc', "${REPO_NAME}-app-${JOB_NAME}")
if (bcApp.exists()) {
echo "Removing BuildConfig ${REPO_NAME}-app-${JOB_NAME}..."
bcApp.delete()
}
}
}
}
}
}
}
}
stage('Deploy - Dev') {
agent any
steps {
script {
deployStage('Dev', DEV_PROJECT, DEV_HOST_ROUTE)
}
}
post {
success {
createDeploymentStatus(DEV_PROJECT, 'SUCCESS', DEV_HOST_ROUTE)
notifyStageStatus('Deploy - Dev', 'SUCCESS')
}
unsuccessful {
createDeploymentStatus(DEV_PROJECT, 'FAILURE', DEV_HOST_ROUTE)
notifyStageStatus('Deploy - Dev', 'FAILURE')
}
}
}
}
post {
changed {
script {
// Comment on Pull Request if build changes to successful
if (currentBuild.currentResult.equalsIgnoreCase('SUCCESS')) {
echo "Posting 'SonarQube Analysis: ${SONARQUBE_URL_EXT}/dashboard?id=${REPO_NAME}-${JOB_NAME}' to Pull Request..."
commentOnPR("SonarQube Analysis: ${SONARQUBE_URL_EXT}/dashboard?id=${REPO_NAME}-${JOB_NAME}")
}
}
}
}
}
// ------------------
// Pipeline Functions
// ------------------
// Parameterized deploy stage
def deployStage(String stageEnv, String projectEnv, String hostRouteEnv) {
if (!stageEnv.equalsIgnoreCase('Dev')) {
input("Deploy to ${projectEnv}?")
}
notifyStageStatus("Deploy - ${stageEnv}", 'PENDING')
openshift.withCluster() {
openshift.withProject(projectEnv) {
if (DEBUG_OUTPUT.equalsIgnoreCase('true')) {
echo "DEBUG - Using project: ${openshift.project()}"
}
echo "Checking for ConfigMaps and Secrets in project ${openshift.project()}..."
if (!(openshift.selector('cm', "cdogs-keycloak-config").exists() &&
openshift.selector('cm', "cdogs-server-config").exists() &&
openshift.selector('secret', "cdogs-keycloak-secret").exists())) {
echo 'Some ConfigMaps and/or Secrets are missing. Please consult the openshift readme for details.'
throw new Exception('Missing ConfigMaps and/or Secrets')
}
createDeploymentStatus(projectEnv, 'PENDING', hostRouteEnv)
// Wait for deployments to roll out
timeout(10) {
// Apply Server
echo "Tagging Image ${REPO_NAME}-app:${JOB_NAME}..."
openshift.tag("${TOOLS_PROJECT}/${REPO_NAME}-app:${JOB_NAME}", "${REPO_NAME}-app:${JOB_NAME}")
echo "Processing DeploymentConfig ${REPO_NAME}-app..."
def dcAppTemplate = openshift.process('-f',
'openshift/app.dc.yaml',
"REPO_NAME=${REPO_NAME}",
"JOB_NAME=${JOB_NAME}",
"NAMESPACE=${projectEnv}",
"APP_NAME=${APP_NAME}",
"HOST_ROUTE=${hostRouteEnv}"
)
echo "Applying ${REPO_NAME}-app Deployment..."
def dcApp = openshift.apply(dcAppTemplate).narrow('dc')
dcApp.rollout().status('--watch=true')
}
}
}
}
// --------------------
// Supporting Functions
// --------------------
// Notify stage status and pass to Jenkins-GitHub library
def notifyStageStatus(String name, String status) {
def sha1 = GIT_COMMIT
if (JOB_BASE_NAME.startsWith('PR-')) {
sha1 = GitHubHelper.getPullRequestLastCommitId(this)
}
GitHubHelper.createCommitStatus(
this, sha1, status, BUILD_URL, '', "Stage: ${name}"
)
}
// Create deployment status and pass to Jenkins-GitHub library
def createDeploymentStatus(String environment, String status, String hostUrl) {
def ghDeploymentId = new GitHubHelper().createDeployment(
this,
SOURCE_REPO_REF,
[
'environment': environment,
'task' : "deploy:pull:${CHANGE_ID}"
]
)
new GitHubHelper().createDeploymentStatus(
this,
ghDeploymentId,
status,
['targetUrl': "https://${hostUrl}"]
)
if (status.equalsIgnoreCase('SUCCESS')) {
echo "${environment} deployment successful at https://${hostUrl}"
} else if (status.equalsIgnoreCase('PENDING')) {
echo "${environment} deployment pending..."
} else if (status.equalsIgnoreCase('FAILURE')) {
echo "${environment} deployment failed"
}
}
// Creates a comment and pass to Jenkins-GitHub library
def commentOnPR(String comment) {
if (JOB_BASE_NAME.startsWith('PR-')) {
GitHubHelper.commentOnPullRequest(this, comment)
}
}