-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathJenkinsfile-hotfix
146 lines (129 loc) · 4.73 KB
/
Jenkinsfile-hotfix
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
#!/usr/bin/env groovy
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import java.util.regex.Pattern
import java.util.UUID
/*
* 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}")
}
/*
* 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 {
environment {
TESTPROJECT = "${TEST_NAMESPACE ?: '6cdc9e-test'}"
}
agent any
options {
disableResume()
}
stages {
stage('Parallel Build Steps') {
failFast true
parallel {
stage('Build') {
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')
ROCKET_QA_WEBHOOK = sh(returnStdout: true, script: 'cat rocket-qa-webhook')
echo "Building eagle-api hotfix branch"
openshiftBuild bldCfg: 'eagle-api', showBuildLogs: 'true'
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 hotfix-eagle-api:latest -o template --template=\"{{.image.dockerImageReference}}\"|awk -F \":\" \'{print \$3}\'""",
returnStdout: true).trim()
echo ">> IMAGE_HASH: ${IMAGE_HASH}"
} catch (error) {
notifyRocketChat(
"@all The build ${env.BUILD_DISPLAY_NAME} of eagle-api, seems to be broken.\n ${env.RUN_DISPLAY_URL}\n Error: \n ${error.message}",
ROCKET_DEPLOY_WEBHOOK
)
throw error
}
}
}
}
}
}
stage('Deploy to hotfix'){
agent any
steps {
script {
try {
echo "Backing up..."
openshiftTag destStream: 'hotfix-eagle-api', verbose: 'false', destTag: 'backup-hotfix', srcStream: 'hotfix-eagle-api', srcTag: "hotfix"
sleep 5
echo "Deploying to hotfix..."
openshiftTag destStream: 'hotfix-eagle-api', verbose: 'false', destTag: 'hotfix', srcStream: 'hotfix-eagle-api', srcTag: "${IMAGE_HASH}"
sleep 5
openshiftVerifyDeployment depCfg: 'hotfix-eagle-api', namespace: TESTPROJECT, replicaCount: 1, verbose: 'false', verifyReplicaCount: 'false', waitTime: 600000
echo ">>>> Deployment Complete"
notifyRocketChat(
"A new hotfix image has been created, build: ${env.BUILD_DISPLAY_NAME} \n Changes: \n ${CHANGELOG}",
ROCKET_DEPLOY_WEBHOOK
)
notifyRocketChat(
"@all A new hotfix image has been created \n Changes to hotfix: \n ${CHANGELOG}",
ROCKET_QA_WEBHOOK
)
} catch (error) {
notifyRocketChat(
"@all The build ${env.BUILD_DISPLAY_NAME} of hotfix-eagle-api, seems to be broken.\n ${env.RUN_DISPLAY_URL}\n Error: \n ${error.message}",
ROCKET_DEPLOY_WEBHOOK
)
currentBuild.result = "FAILURE"
throw new Exception("Deploy failed")
}
}
}
}
}
}