This repository has been archived by the owner on Feb 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Jenkinsfile
241 lines (217 loc) · 8.23 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
// Load Common Variables and utils
common = ""
node{
common = load "../workspace@script/Jenkinsfile.common.groovy"
}
// You shouldn't have to edit these if you're following the conventions
ARTIFACT_BUILD = common.APP_NAME+'-artifacts-build'
NGINX_BUILD = 'nginx-runtime'
RUNTIME_BUILD = common.APP_NAME+'-'+NGINX_BUILD+'-build'
YARN_BUILD = 'yarn-builder'
IMAGESTREAM_NAME = common.APP_NAME
stage('Assemble Builder & Runtime'){
node{
openshift.withProject(){
// Assemble Yarn Builder
try{
common.ensureBuildExists(YARN_BUILD,"openshift/templates/yarn-builder/yarn-builder-build.json")
common.buildAndVerify(YARN_BUILD)
}catch(error){
common.notifyError(
"Problem Assembling Yarn Builder 🤕",
"Error: ${error.message}"
)
}
// Assemble nginx Runtime
try{
common.ensureBuildExists(NGINX_BUILD,"openshift/templates/nginx-runtime/nginx-runtime-build.json")
common.buildAndVerify(NGINX_BUILD)
}catch(error){
common.notifyError(
"Problem Assembling nginx Runtime 🤕",
"Error: ${error.message}"
)
}
}
}
}
stage('Build ' + common.APP_NAME) {
node{
openshift.withProject() {
try{
// Make sure the frontend build configs exist
common.ensureBuildExists(ARTIFACT_BUILD,"openshift/templates/frontend/frontend-build.json")
// trigger the Artifact build and runtime build
common.buildAndVerify(ARTIFACT_BUILD)
common.buildAndVerify(RUNTIME_BUILD)
// 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
IMAGE_HASH = common.getLatestHash(IMAGESTREAM_NAME)
echo ">> IMAGE_HASH: ${IMAGE_HASH}"
}catch(error){
common.notifyError(
"${common.APP_NAME} Build Broken 🤕",
"Author:${env.CHANGE_AUTHOR_DISPLAY_NAME}\r\nError:'${error.message}'"
)
throw error
}
}
}
}
// We have Functional tests in our API project, commenting out these stages
// as we do not currently have e2e tests within our frontend.
// // Creating Emphemeral post-gress instance for testing
// stage('Emphemeral Test Environment'){
// node{
// try{
// echo "Creating Ephemeral Postgress instance for testing"
// POSTGRESS = sh (
// script: """oc project apndkr-tools; oc process -f "${work_space}/openshift/test/frontend-deploy.json" | oc create -f -; oc process -f "${work_space}/openshift/test/api-postgress-ephemeral.json" | oc create -f - """)
// echo ">> POSTGRESS: ${POSTGRESS}"
// } catch(error){
// echo "Error in creating postgress instance"
// throw error
// }
// }
// }
// //Running functional Test cases - in tools project
// stage('Run Test Cases'){
// node{
// try{
// echo "Run Test Case scripts here"
// POSTGRESS_DEL = sh (
// script: """oc project apndkr-tools; oc process -f "${work_space}/openshift/test/frontend-deploy.json" | oc delete -f -; oc process -f "${work_space}/openshift/test/api-postgress-ephemeral.json" | oc delete -f - """)
// echo ">> ${POSTGRESS_DEL}"
// echo "postgress instance deleted successfully"
// } catch(error){
// echo "Error while test cases are running"
// throw error
// }
// }
// }
// Deploying to Dev
stage("Deploy to ${common.environments.dev.name}") {
def environment = common.environments.dev.tag
def url = common.environments.dev.url
node{
try{
common.deployAndVerify(IMAGE_HASH,environment,IMAGESTREAM_NAME)
common.notifyNewDeployment(environment,url,"Deploy to ${common.environments.test.name}?")
}catch(error){
common.notifyDeploymentError(environment,error)
throw error
}
}
}
// Deploying to Test
stage("Deploy to ${common.environments.test.name}") {
def environment = common.environments.test.tag
def url = common.environments.test.url
timeout(time:7, unit: 'DAYS'){ input "Deploy to ${environment}?"}
node{
try{
common.deployAndVerify(IMAGE_HASH,environment,IMAGESTREAM_NAME)
common.notifyNewDeployment(environment,url,"Tag for ${common.environments.prod.name}?")
}catch(error){
common.notifyDeploymentError(environment,error)
throw error
}
}
}
// Tag for Prod
stage("Tag for ${common.environments.prod.name}") {
def environment = common.environments.prod.tag
timeout(time:7, unit: 'DAYS'){ input "Tag for ${common.environments.prod.name}?"}
node{
try{
common.tagImage(IMAGE_HASH,environment,IMAGESTREAM_NAME)
common.notifyGood(
"${common.APP_NAME} tagged for ${common.environments.prod.name}",
"Start production pipeline to push new images"
)
}catch(error){
common.notifyError(
"Couldn't tag ${common.APP_NAME} for ${common.environments.prod.name} 🤕",
"Error: '${error.message}'"
)
throw error
}
}
}
// STAGES FOR SUPPORTING BLUE/GREEN Deployment
// // Deploying to production
// stage('Tag Image to ' + TAG_NAMES[2]){
// def environment = TAG_NAMES[2]
// def url = APP_URLS[2]
// timeout(time:7, unit: 'DAYS'){ input "Deploy to ${environment}?"}
// node{
// try {
// // Check for current route target
// ROUT_CHK = sh (
// script: """oc project apndkr-prod; if [ `oc get route sheriff-scheduling-prod -o=jsonpath='{.spec.to.weight}'` == "100" ]; then `oc get route sheriff-scheduling-prod -o=jsonpath='{.spec.to.name}' > route-target`; else `oc get route sheriff-scheduling-prod -o=jsonpath='{.spec.alternateBackend[*].name}' > route-target`; fi ; cat route-target""")
// // Tag the new build as "prod"
// openshiftTag destStream: IMAGESTREAM_NAME, verbose: 'true', destTag: environment, srcStream: IMAGESTREAM_NAME, srcTag: "${IMAGE_HASH}", waitTime: '900000'
// slackNotify(
// "Current ${IMAGESTREAM_NAME} Image tagged to ${environment}",
// "To Deploy ${newTarget} stack and with prod tagged image",
// 'To switch to new version',
// env.SLACK_HOOK,
// SLACK_PROD_CHANNEL,
// [
// [
// type: "button",
// text: "switch route to new version on ${newTarget}?",
// style: "primary",
// url: "${currentBuild.absoluteUrl}/input"
// ]
// ])
// }catch(error){
// slackNotify(
// "Couldn't tag image to ${environment} 🤕",
// "The latest tagging of the image to ${environment} seems to have failed\n'${error.message}'",
// 'danger',
// env.SLACK_HOOK,
// SLACK_DEV_CHANNEL,
// [
// [
// type: "button",
// text: "View Build Logs",
// style:"danger",
// url: "${currentBuild.absoluteUrl}/console"
// ]
// ])
// echo "Build failed"
// }
// }
// }
// // Once approved (input step) switch production over to the new version.
// stage('Switch over to new production stack') {
// // Wait for administrator confirmation
// timeout(time:7, unit: 'DAYS'){ input id: 'Approval', message: "Switch Production stack?", submitter: 'ronald-garcia-admin', submitterParameter: 'approvingSubmitter'}
// node{
// try{
// //Trigger remote job
// def handle = build job: 'apndkr-prod-deploy'
// }catch(error){
// echo "Failed to switch route"
// throw error
// }
// }
// }
// // // Functions to check currentTarget (api-blue)deployment and mark to for deployment to newTarget(api-green) & vice versa
// def getCurrentTarget() {
// def currentTarget = readFile("route-target")
// return currentTarget
// }
// def getNewTarget() {
// def currentTarget = getCurrentTarget()
// def newTarget = ""
// if (currentTarget == 'frontend-blue') {
// newTarget = 'frontend-green'
// } else if (currentTarget == 'frontend-green') {
// newTarget = 'frontend-blue'
// } else {
// echo "OOPS, wrong target"
// }
// return newTarget
// }