forked from bcgov/eagle-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
275 lines (248 loc) · 8.57 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
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}")
}
/*
* takes in a sonarqube status json payload
* and returns the status string
*/
def sonarGetStatus (jsonPayload) {
def jsonSlurper = new JsonSlurper()
return jsonSlurper.parseText(jsonPayload).projectStatus.status
}
/*
* 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 nodejsTester () {
openshift.withCluster() {
openshift.withProject() {
podTemplate(
label: 'node-tester',
name: 'node-tester',
serviceAccount: 'jenkins',
cloud: 'openshift',
slaveConnectTimeout: 300,
containers: [
containerTemplate(
name: 'jnlp',
image: 'registry.access.redhat.com/openshift3/jenkins-agent-nodejs-8-rhel7',
resourceRequestCpu: '500m',
resourceLimitCpu: '1000m',
resourceRequestMemory: '2Gi',
resourceLimitMemory: '4Gi',
workingDir: '/tmp',
command: '',
)
]
) {
node("node-tester") {
checkout scm
sh 'npm i'
try {
sh 'npm run tests'
} finally {
echo "Unit Tests Passed"
}
}
}
return true
}
}
}
// todo templates can be pulled from a repository, instead of declared here
def nodejsSonarqube () {
openshift.withCluster() {
openshift.withProject() {
podTemplate(
label: 'node-sonarqube',
name: 'node-sonarqube',
serviceAccount: 'jenkins',
cloud: 'openshift',
slaveConnectTimeout: 300,
containers: [
containerTemplate(
name: 'jnlp',
image: 'registry.access.redhat.com/openshift3/jenkins-agent-nodejs-8-rhel7',
resourceRequestCpu: '500m',
resourceLimitCpu: '1000m',
resourceRequestMemory: '2Gi',
resourceLimitMemory: '4Gi',
workingDir: '/tmp',
command: '',
args: '${computer.jnlpmac} ${computer.name}',
)
]
) {
node("node-sonarqube") {
checkout scm
dir('sonar-runner') {
try {
// run scan
sh("oc extract secret/sonarqube-secrets --to=${env.WORKSPACE}/sonar-runner --confirm")
SONARQUBE_URL = sh(returnStdout: true, script: 'cat sonarqube-route-url')
sh "npm install typescript"
sh returnStdout: true, script: "./gradlew sonarqube -Dsonar.host.url=${SONARQUBE_URL} -Dsonar. -Dsonar.verbose=true --stacktrace --info"
// wiat for scan status to update
sleep(30)
// check if sonarqube passed
sh("oc extract secret/sonarqube-status-urls --to=${env.WORKSPACE}/sonar-runner --confirm")
SONARQUBE_STATUS_URL = sh(returnStdout: true, script: 'cat sonarqube-status-api')
SONARQUBE_STATUS_JSON = sh(returnStdout: true, script: "curl -w '%{http_code}' '${SONARQUBE_STATUS_URL}'")
SONARQUBE_STATUS = sonarGetStatus (SONARQUBE_STATUS_JSON)
if ( "${SONARQUBE_STATUS}" == "ERROR") {
echo "Scan Failed"
notifyRocketChat(
"@all The latest build ${env.BUILD_DISPLAY_NAME} of eagle-api seems to be broken. \n ${env.BUILD_URL}\n Error: \n Sonarqube scan failed",
ROCKET_DEPLOY_WEBHOOK
)
currentBuild.result = 'FAILURE'
exit 1
} else {
echo "Scan Passed"
}
} catch (error) {
notifyRocketChat(
"@all The latest build ${env.BUILD_DISPLAY_NAME} of eagle-api seems to be broken. \n ${env.BUILD_URL}\n Error: \n ${error}",
ROCKET_DEPLOY_WEBHOOK
)
throw error
} finally {
echo "Scan Complete"
}
}
}
}
return true
}
}
}
def CHANGELOG = "No new changes"
def IMAGE_HASH = "latest"
pipeline {
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 develop 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 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.BUILD_URL}\n Error: \n ${error.message}",
ROCKET_DEPLOY_WEBHOOK
)
throw error
}
}
}
}
stage('Unit Tests') {
steps {
script {
echo "Running Unit Tests"
def result = nodejsTester()
}
}
}
stage('Sonarqube') {
steps {
script {
echo "Running Sonarqube"
def result = nodejsSonarqube()
}
}
}
}
}
stage('Deploy to dev'){
steps {
script {
try {
echo "Deploying to dev..."
openshiftTag destStream: 'eagle-api', verbose: 'false', destTag: 'dev', srcStream: 'eagle-api', srcTag: "${IMAGE_HASH}"
sleep 5
// todo eagle-test? what depCfg?
openshiftVerifyDeployment depCfg: 'eagle-api', namespace: 'esm-dev', replicaCount: 1, verbose: 'false', verifyReplicaCount: 'false', waitTime: 600000
echo ">>>> Deployment Complete"
notifyRocketChat(
"A new version of eagle-api is now in Dev, build: ${env.BUILD_DISPLAY_NAME} \n Changes: \n ${CHANGELOG}",
ROCKET_DEPLOY_WEBHOOK
)
notifyRocketChat(
"@all A new version of eagle-api is now in Dev and ready for QA. \n Changes to Dev: \n ${CHANGELOG}",
ROCKET_QA_WEBHOOK
)
} catch (error) {
notifyRocketChat(
"@all The build ${env.BUILD_DISPLAY_NAME} of eagle-api, seems to be broken.\n ${env.BUILD_URL}\n Error: \n ${error.message}",
ROCKET_DEPLOY_WEBHOOK
)
currentBuild.result = "FAILURE"
throw new Exception("Deploy failed")
}
}
}
}
}
}