generated from bcgov/vue-scaffold
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jenkinsfile
184 lines (165 loc) · 5.64 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
#!groovy
// --------------------
// 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}/${JOB_NAME}"
SOURCE_REPO_REF = "${JOB_NAME}"
SOURCE_REPO_URL = "https://github.com/${REPO_OWNER}/${REPO_NAME}.git"
// ENV_HOST is the full domain without the path (ie. 'appname-dev.pathfinder.gov.bc.ca')
DEV_HOST = "${REPO_NAME}-dev.${APP_DOMAIN}"
TEST_HOST = "${REPO_NAME}-test.${APP_DOMAIN}"
PROD_HOST = "${REPO_NAME}.${APP_DOMAIN}"
// PATH_ROOT will be appended to ENV_HOST
PATH_ROOT = "/${JOB_NAME.equalsIgnoreCase('master') ? 'app' : JOB_NAME}"
}
options {
parallelsAlwaysFailFast()
}
stages {
stage('Initialize') {
agent any
steps {
// Cancel any running builds in progress
timeout(time: 10, unit: 'MINUTES') {
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')
}
loadCommonPipeline()
}
}
}
stage('Build') {
agent any
steps {
script {
loadCommonPipeline()
commonPipeline.runStageBuild()
}
}
post {
success {
echo 'Cleanup App 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 {
loadCommonPipeline()
commonPipeline.runStageDeploy('Dev', DEV_PROJECT, DEV_HOST, PATH_ROOT)
}
}
post {
success {
script {
commonPipeline.createDeploymentStatus(DEV_PROJECT, 'SUCCESS', JOB_NAME, DEV_HOST, PATH_ROOT)
commonPipeline.notifyStageStatus('Deploy - Dev', 'SUCCESS')
}
}
unsuccessful {
script {
commonPipeline.createDeploymentStatus(DEV_PROJECT, 'FAILURE', JOB_NAME, DEV_HOST, PATH_ROOT)
commonPipeline.notifyStageStatus('Deploy - Dev', 'FAILURE')
}
}
}
}
stage('Deploy - Test') {
agent any
steps {
script {
loadCommonPipeline()
commonPipeline.runStageDeploy('Test', TEST_PROJECT, TEST_HOST, PATH_ROOT)
}
}
post {
success {
script {
commonPipeline.createDeploymentStatus(TEST_PROJECT, 'SUCCESS', JOB_NAME, TEST_HOST, PATH_ROOT)
commonPipeline.notifyStageStatus('Deploy - Test', 'SUCCESS')
}
}
unsuccessful {
script {
commonPipeline.createDeploymentStatus(TEST_PROJECT, 'FAILURE', JOB_NAME, TEST_HOST, PATH_ROOT)
commonPipeline.notifyStageStatus('Deploy - Test', 'FAILURE')
}
}
}
}
stage('Deploy - Prod') {
agent any
steps {
script {
loadCommonPipeline()
commonPipeline.runStageDeploy('Prod', PROD_PROJECT, PROD_HOST, PATH_ROOT)
}
}
post {
success {
script {
commonPipeline.createDeploymentStatus(PROD_PROJECT, 'SUCCESS', JOB_NAME, PROD_HOST, PATH_ROOT)
commonPipeline.notifyStageStatus('Deploy - Prod', 'SUCCESS')
}
}
unsuccessful {
script {
commonPipeline.createDeploymentStatus(PROD_PROJECT, 'FAILURE', JOB_NAME, PROD_HOST, PATH_ROOT)
commonPipeline.notifyStageStatus('Deploy - Prod', 'FAILURE')
}
}
}
}
}
}
// --------------------
// Supporting Functions
// --------------------
// Load Common Code as Global Variable
def loadCommonPipeline() {
if (!binding.hasVariable('commonPipeline')) {
commonPipeline = load "${WORKSPACE}/openshift/commonPipeline.groovy"
}
}