-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathJenkinsfile - deploy to prod
186 lines (164 loc) · 6.36 KB
/
Jenkinsfile - deploy to prod
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
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import java.util.regex.Pattern
/*
* Sends a rocket chat notification
*/
def notifyRocketChat(text, url) {
def rocketChatURL = url
def message = text.replaceAll(~/\'/, "")
def payload = JsonOutput.toJson([
"username":"Jenkins",
"icon_url":"https://wiki.jenkins.io/download/attachments/2916393/headshot.png",
"text": message
])
sh("curl -X POST -H 'Content-Type: application/json' --data \'${payload}\' ${rocketChatURL}")
}
boolean imageTaggingComplete ( String sourceTag, String destinationTag, String action, def iterations = 6 ) {
def sourceImageName = sh returnStdout: true, script: "oc describe istag/lup-admin-static:${sourceTag} | head -n 1".trim()
def destinationImageName = sh returnStdout: true, script: "oc describe istag/lup-admin-static:${destinationTag} | head -n 1".trim()
int delay = 0
for (int i=0; i<iterations; i++){
echo "waiting to ${action}, iterator is: ${i}, the max iterator is: ${iterations} \n ${sourceTag}: ${sourceImageName} ${destinationTag}: ${destinationImageName}"
if(sourceImageName == destinationImageName){
echo "${action} complete"
return true
} else {
delay = (1<<i) // exponential backoff
sleep(delay)
destinationImageName = sh returnStdout: true, script: "oc describe istag/lup-admin-static:${destinationTag} | head -n 1".trim()
}
}
return false
}
/*
* Updates the global pastBuilds array: it will iterate recursively
* and add all the builds prior to the current one that had a result
* different than 'SUCCESS'.
*/
def buildsSinceLastSuccess(previousBuild, build) {
if ((build != null) && (build.result != 'SUCCESS')) {
pastBuilds.add(build)
buildsSinceLastSuccess(pastBuilds, build.getPreviousBuild())
}
}
/*
* Generates a string containing all the commit messages from
* the builds in pastBuilds.
*/
@NonCPS
def getChangeLog(pastBuilds) {
def log = ""
for (int x = 0; x < pastBuilds.size(); x++) {
for (int i = 0; i < pastBuilds[x].changeSets.size(); i++) {
def entries = pastBuilds[x].changeSets[i].items
for (int j = 0; j < entries.length; j++) {
def entry = entries[j]
log += "* ${entry.msg} by ${entry.author} \n"
}
}
}
return log;
}
def CHANGELOG = "No new changes"
def IMAGE_HASH = "latest"
pipeline {
agent any
options {
disableResume()
}
stages {
stage('Prepare for deployment') {
agent any
steps {
script {
pastBuilds = []
buildsSinceLastSuccess(pastBuilds, currentBuild);
CHANGELOG = getChangeLog(pastBuilds);
echo ">>>>>>Changelog: \n ${CHANGELOG}"
try {
sh("oc extract secret/rocket-chat-secrets --to=${env.WORKSPACE} --confirm")
ROCKET_DEPLOY_WEBHOOK = sh(returnStdout: true, script: 'cat rocket-deploy-webhook')
} catch (error) {
notifyRocketChat(
"@all *[ADMIN][ERROR]* \n The deployment ${env.BUILD_DISPLAY_NAME} of landuseplanning-admin, seems to be broken.\n ${env.BUILD_URL}\n Error: \n ${error.message}",
ROCKET_DEPLOY_WEBHOOK
)
throw error
}
}
}
}
stage('Tag image') {
agent any
steps {
script {
try {
// backup
echo "Backing up prod image..."
openshiftTag destStream: 'lup-admin-static', verbose: 'false', destTag: 'prod-backup', srcStream: 'lup-admin-static', srcTag: 'prod'
// wait for backup to complete
if( !imageTaggingComplete ('prod', 'prod-backup', 'backup')) {
echo "Prod image backup failed"
notifyRocketChat(
"@all *[ADMIN][ERROR]* \n The latest deployment, ${env.BUILD_DISPLAY_NAME} of landuseplanning-admin seems to be broken. \n ${env.BUILD_URL}\n Error: \n prod image backup failed",
ROCKET_DEPLOY_WEBHOOK
)
currentBuild.result = "FAILURE"
exit 1
}
// deploy
echo "Tagging prod image"
openshiftTag destStream: 'lup-admin-static', verbose: 'false', destTag: 'prod', srcStream: 'lup-admin-static', srcTag: "test"
// wait for deployment to complete
if ( !imageTaggingComplete ('latest', 'prod', 'deploy')) {
echo "Prod image deployment failed"
notifyRocketChat(
"@all *[ADMIN][ERROR]* \n The latest deployment, ${env.BUILD_DISPLAY_NAME} of landuseplanning-admin seems to be broken. \n ${env.BUILD_URL}\n Error: \n Prod image deployment failed",
ROCKET_DEPLOY_WEBHOOK
)
currentBuild.result = "FAILURE"
exit 1
} else {
sleep (5)
}
} catch (error) {
notifyRocketChat(
"@all *[ADMIN][ERROR]* \n The deployment ${env.BUILD_DISPLAY_NAME} of landuseplanning-admin, seems to be broken.\n ${env.BUILD_URL}\n Error: \n ${error.message}",
ROCKET_DEPLOY_WEBHOOK
)
throw error
}
}
}
}
stage('Verify deployment'){
steps {
script {
try {
echo "Verifying that the deployment was successful"
openshiftVerifyDeployment depCfg: 'landuseplanning-admin-prod', namespace: 'xti26n-prod', replicaCount: 1, verbose: 'false', verifyReplicaCount: 'false', waitTime: 600000
echo ">>>> Deployment Complete"
} catch (error) {
notifyRocketChat(
"@all *[ADMIN][ERROR]* \n The deployment ${env.BUILD_DISPLAY_NAME} of landuseplanning-admin, seems to be broken.\n ${env.BUILD_URL}\n Error: ${error.message}",
ROCKET_DEPLOY_WEBHOOK
)
currentBuild.result = "FAILURE"
throw new Exception("Deploy failed")
}
}
}
}
stage('Success Notifications') {
steps {
script {
notifyRocketChat(
"*[ADMIN][PROD]* \n A new version of landuseplanning-admin is now in Production, build ${env.BUILD_DISPLAY_NAME} \n Changes: \n ${CHANGELOG}",
ROCKET_DEPLOY_WEBHOOK
)
}
}
}
}
}