forked from bcgov/esm-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
144 lines (131 loc) · 4.84 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
// Switch to using https://github.com/BCDevOps/jenkins-pipeline-shared-lib when stable.
@NonCPS
import groovy.json.JsonOutput
/*
* Sends a slack notification
*/
def notifySlack(text, url, channel, attachments) {
def slackURL = url
def jenkinsIcon = 'https://wiki.jenkins-ci.org/download/attachments/2916393/logo.png'
def payload = JsonOutput.toJson([
text: text,
channel: channel,
username: "Jenkins",
icon_url: jenkinsIcon,
attachments: attachments
])
def encodedReq = URLEncoder.encode(payload, "UTF-8")
sh("curl -s -S -X POST --data \'payload=${encodedReq}\' ${slackURL}")
}
/*
* 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"
node('master') {
/*
* Extract secrets and create relevant environment variables.
* The contents of the secret are extracted in as many files as the keys contained in the secret.
* The files are named as the key, and contain the corresponding value.
*/
sh("oc extract secret/slack-secrets --to=${env.WORKSPACE} --confirm")
SLACK_HOOK = sh(script: "cat webhook", returnStdout: true)
DEV_CHANNEL = sh(script: "cat dev-channel", returnStdout: true)
withEnv(["SLACK_HOOK=${SLACK_HOOK}", "DEV_CHANNEL=${DEV_CHANNEL}"]){
stage('Build'){
// isolate last successful builds and then get the changelog
pastBuilds = []
buildsSinceLastSuccess(pastBuilds, currentBuild);
CHANGELOG = getChangeLog(pastBuilds);
echo ">>>>>>Changelog: \n ${CHANGELOG}"
try {
echo "Building..."
openshiftBuild bldCfg: 'esm-server', showBuildLogs: 'true'
echo "Build done"
echo "Tagging image..."
// 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 esm-server:latest -o template --template=\"{{.image.dockerImageReference}}\"|awk -F \":\" \'{print \$3}\'""",
returnStdout: true).trim()
echo ">> IMAGE_HASH: ${IMAGE_HASH}"
openshiftTag destStream: 'esm-server', verbose: 'true', destTag: "${IMAGE_HASH}", srcStream: 'esm-server', srcTag: 'latest'
echo "Tagging done"
} catch (error) {
notifySlack(
"The latest esm-server build seems to have broken\n'${error.message}'",
SLACK_HOOK,
DEV_CHANNEL,
[]
)
throw error
}
}
}
}
node('master') {
/*
* Extract secrets and create relevant environment variables.
* The contents of the secret are extracted in as many files as the keys contained in the secret.
* The files are named as the key, and contain the corresponding value.
*/
sh("oc extract secret/slack-secrets --to=${env.WORKSPACE} --confirm")
SLACK_HOOK = sh(script: "cat webhook", returnStdout: true)
DEPLOY_CHANNEL = sh(script: "cat deploy-channel", returnStdout: true)
QA_CHANNEL = sh(script: "cat qa-channel", returnStdout: true)
withEnv(["SLACK_HOOK=${SLACK_HOOK}", "DEPLOY_CHANNEL=${DEPLOY_CHANNEL}", "QA_CHANNEL=${QA_CHANNEL}"]){
stage('Deploy to Test'){
try {
echo "Deploying to test..."
openshiftTag destStream: 'esm-server', verbose: 'true', destTag: 'test', srcStream: 'esm-server', srcTag: 'latest'
sleep 5
openshiftVerifyDeployment depCfg: 'esm-test', namespace: 'esm-test', replicaCount: 1, verbose: 'false', verifyReplicaCount: 'false', waitTime: 600000
echo ">>>> Deployment Complete"
notifySlack(
"A new version of esm-server is now in Test. \n Changes: \n ${CHANGELOG}",
SLACK_HOOK,
DEPLOY_CHANNEL,
[]
)
notifySlack(
"A new version of esm-server is now in Test and ready for QA. \n Changes to test: \n ${CHANGELOG}",
SLACK_HOOK,
QA_CHANNEL,
[]
)
} catch (error) {
notifySlack(
"The latest deployment of esm-server to Test seems to have failed\n'${error.message}'",
SLACK_HOOK,
DEPLOY_CHANNEL,
[]
)
}
}
}
}