-
Notifications
You must be signed in to change notification settings - Fork 27
/
Jenkinsfile-cicd
239 lines (221 loc) · 7.96 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
#!/usr/bin/env groovy
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import java.util.regex.Pattern
/*
* Sends a rocket chat notification
*/
def notifyRocketChat(text, target) {
def message = text.replaceAll(~/\'/, "")
def payload = JsonOutput.toJson([
"username":"Jenkins",
"icon_url":"https://wiki.jenkins.io/download/attachments/2916393/headshot.png",
"text": message
])
try {
def rocketChatURL;
if (target == 'deploy') {
rocketChatURL = ROCKET_DEPLOY_WEBHOOK
} else if (target == 'qa') {
rocketChatURL = ROCKET_QA_WEBHOOK
}
sh("curl -X POST -H 'Content-Type: application/json' --data \'${payload}\' ${rocketChatURL}")
} catch (error) {
echo "Error posting Rocket Chat message: ${error}"
}
}
// Print stack trace of error
@NonCPS
private static String stackTraceAsString(Throwable t) {
StringWriter sw = new StringWriter();
t.printStackTrace(new PrintWriter(sw));
return sw.toString()
}
def _openshift(String name, String project, Closure body) {
script {
openshift.withCluster() {
openshift.withProject(project) {
echo "Running Stage '${name}'"
waitUntil {
boolean isDone=false
try {
body()
isDone=true
echo "Completed Stage '${name}'"
} catch (error) {
echo "${stackTraceAsString(error)}"
def inputAction = input(
message: "This step (${name}) has failed. See related messages:",
ok: 'Confirm',
parameters: [
choice(
name: 'action',
choices: 'Re-run\nIgnore',
description: 'What would you like to do?'
)
]
)
if ('Ignore'.equalsIgnoreCase(inputAction)) {
notifyRocketChat(
"@all The build ${env.BUILD_DISPLAY_NAME} of eagle-public-pr, seems to be broken.\n ${env.RUN_DISPLAY_URL}\n Error: \n ${error.message}",
'deploy'
)
isDone=true
}
}
return isDone
}
}
}
}
}
def BUILD_DONE_STATUSES = ['Complete', 'Failed', 'Cancelled']
pipeline {
environment {
TOOLSPROJECT = "${TOOLS_NAMESPACE ?: '6cdc9e-tools'}"
}
agent any
stages {
stage('Build and Tag Dev'){
when {
expression { env.BRANCH_NAME == 'develop' }
}
steps {
script {
_openshift(env.STAGE_NAME, TOOLSPROJECT) {
timeout(20) {
try {
sh("oc extract secret/rocket-chat-secrets --to=${env.WORKSPACE} --confirm")
ROCKET_DEPLOY_WEBHOOK = sh(returnStdout: true, script: 'cat rocket-deploy-webhook')
ROCKET_QA_WEBHOOK = sh(returnStdout: true, script: 'cat rocket-qa-webhook')
} catch (error) {
echo "Error retrieviing rocket chat token"
}
echo "Building eagle-public develop branch"
// trigger and wait for s2i build to complete
def bcObj1 = openshift.selector('bc', "eagle-public-angular-builder")
bcObj1.startBuild()
def buildName1 = "eagle-public-angular-builder-${bcObj1.object().status.lastVersion}"
echo "Angular build name: ${buildName1}"
def buildSelector1 = openshift.selector('build', buildName1)
buildSelector1.untilEach(1) {
def phase = it.object().status.phase
if (phase == 'Failed') {
currentBuild.result = "FAILURE"
}
return ( BUILD_DONE_STATUSES.contains(phase) )
}
// trigger and wait for nginx image build to complete
def bcObj2 = openshift.selector('bc',"eagle-public-build" )
bcObj2.startBuild()
def buildName2 = "eagle-public-build-${bcObj2.object().status.lastVersion}"
echo "Ngix build name: ${buildName2}"
def buildSelector2 = openshift.selector('build', buildName2)
buildSelector2.untilEach(1) {
def phase = it.object().status.phase
if (phase == 'Failed') {
currentBuild.result = "FAILURE"
}
return ( BUILD_DONE_STATUSES.contains(phase) )
}
echo "Build done"
echo ">>> Get Image Hash"
// 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 = sh (
script: """oc get istag eagle-public:latest -o template --template=\"{{.image.dockerImageReference}}\"|awk -F \":\" \'{print \$3}\'""",
returnStdout: true).trim()
echo ">> IMAGE_HASH: ${IMAGE_HASH}"
}
echo "Tagging latest to dev, hash: ${IMAGE_HASH}"
openshift.tag("${TOOLSPROJECT}/eagle-public:latest", "${TOOLSPROJECT}/eagle-public:dev")
notifyRocketChat(
"eagle-public latest develop has been built and tagged to dev",
'deploy'
)
}
}
}
}
stage('Tag Dev as Test'){
when {
expression { env.BRANCH_NAME == 'test' }
}
steps {
script {
try {
sh("oc extract secret/rocket-chat-secrets --to=${env.WORKSPACE} --confirm")
ROCKET_DEPLOY_WEBHOOK = sh(returnStdout: true, script: 'cat rocket-deploy-webhook')
ROCKET_QA_WEBHOOK = sh(returnStdout: true, script: 'cat rocket-qa-webhook')
} catch (error) {
echo "Error retrieviing rocket chat token"
}
try {
echo "Backing up..."
sh("oc tag ${TOOLSPROJECT}/eagle-public:test ${TOOLSPROJECT}/eagle-public:test-backup")
sleep 5
echo "Tagging Dev as Test..."
sh("oc tag ${TOOLSPROJECT}/eagle-public:dev ${TOOLSPROJECT}/eagle-public:test")
sleep 5
echo ">>>> Tagging Complete"
notifyRocketChat(
"eagle-public dev has been tagged to test",
'deploy'
)
notifyRocketChat(
"@all eagle-public dev has been tagged as test",
'qa'
)
} catch (error) {
notifyRocketChat(
"@all tagging dev as test has failed",
'deploy'
)
currentBuild.result = "FAILURE"
throw new Exception("Deploy failed")
}
}
}
}
stage('Tag Test as Prod'){
when {
expression { env.BRANCH_NAME == 'master' }
}
steps {
script {
try {
sh("oc extract secret/rocket-chat-secrets --to=${env.WORKSPACE} --confirm")
ROCKET_DEPLOY_WEBHOOK = sh(returnStdout: true, script: 'cat rocket-deploy-webhook')
ROCKET_QA_WEBHOOK = sh(returnStdout: true, script: 'cat rocket-qa-webhook')
} catch (error) {
echo "Error retrieviing rocket chat token"
}
try {
echo "Backing up..."
sh("oc tag ${TOOLSPROJECT}/eagle-public:prod ${TOOLSPROJECT}/eagle-public:prod-backup")
sleep 5
echo "Tagging Test as Prod..."
sh("oc tag ${TOOLSPROJECT}/eagle-public:test ${TOOLSPROJECT}/eagle-public:prod")
sleep 5
echo ">>>> Tagging Complete"
notifyRocketChat(
"eagle-public test has been tagged to Prod",
'deploy'
)
notifyRocketChat(
"@all eagle-public test has been tagged to prod",
'qa'
)
} catch (error) {
notifyRocketChat(
"@all tagging test as prod has failed",
'deploy'
)
currentBuild.result = "FAILURE"
throw new Exception("Deploy failed")
}
}
}
}
}
}