forked from ICLDisco/dplasma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
348 lines (322 loc) · 12.6 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import groovy.json.JsonSlurper
import hudson.util.RemotingDiagnostics
import jenkins.model.Jenkins
import java.util.regex.Pattern
import java.util.regex.Matcher
@groovy.transform.Field Properties config
// https://qa.nuxeo.org/jenkins/pipeline-syntax/globals
def PROJECT_NAME = "DPLASMA"
//node {
// sh 'env > env.txt'
// for (String i : readFile('env.txt').split("\r?\n")) {
// println i
// }
//}
config = new Properties()
//String configFileName = System.getenv('JENKINS_HOME') + "/dplasma.jenkins.conf"
// For some obscure reason the JENKINS_HOME is always NULL
String configFileName = "${env.JENKINS_HOME}/dplasma.jenkins.conf"
try {
File configFile = new File(configFileName)
config.load(configFile.newDataInputStream())
} catch (Exception ex) {
println "Exception: " + ex.getMessage()
error("Cant load local configuration file ${configFileName} (required for connection to bitbucket)")
}
propertiesData = [disableConcurrentBuilds()]
properties(propertiesData)
node {
def regex = "^https://bitbucket\\.org/(.+?)/(.+?)/pull-requests/(\\d+)"
try {
def l = Pattern.compile(regex).matcher(env.CHANGE_URL)
if( l.find() ) {
config.put('organization', l.group(1))
config.put('repository', l.group(2))
}
println "${config.organization} -> ${config.repository}"
} catch (Exception ex) {
println "Exception: " + ex.getMessage() + " (${env.CHANGE_URL})"
println "Cant identify the organization and repository from ${env.CHANGE_URL} using ${regex}"
}
if( (null == config.get('organization')) || (null == config.get('repository')) ) {
println "Missing organization or repository"
error("Missing organization or repository")
}
if( null == config.get('useSlack') ) {
config.put('useSlack', false)
}
String userpass = config.userName + ":" + config.userPassword;
String basicAuth = "Basic " + userpass.bytes.encodeBase64().toString()
config.put('basicAuth', basicAuth)
}
def COLOR_MAP = [
'SUCCESS': 'good',
'FAILURE': 'danger',
]
def slackResponse = null
// Save the master of the lack check
//git log --format="%H" -n 1 master
//currentBuild.properties.each { println "currentBuild.${it.key} -> ${it.value}" }
//propertiesData.properties.each { println "$it.key -> $it.value" }
//config.each { println "config.${it.key} -> ${it.value}" }
// To mark the starting of a new build the previous approval should be removed
node {
unapprovePullRequest(config.repository, env.CHANGE_ID)
}
pipeline {
agent any
stages {
stage ('Clone') {
when {
beforeAgent true // execute the when clause early
anyOf {
expression {
// https://github.com/jenkinsci/jenkins/blob/master/core/src/main/java/hudson/model/Result.java
return isApprovedBranch(config.repository, env.CHANGE_ID, env.CHANGE_AUTHOR)
}
}
}
steps {
script {
try {
blocks = [
[
"type": "section",
"text": [
"type": "mrkdwn",
"text": "${PROJECT_NAME} Jenkins: [${env.BUILD_URL}](${env.JOB_NAME} [${env.BUILD_NUMBER})"
]
]]
slackResponse = slackSend(channel: '#ci', blocks:blocks)
} catch (Exception ex) { //disable Slack
config.useSlack = false
}
}
checkout scm
}
}
stage ('Fetch Submodule PaRSEC') {
steps {
sh "git --work-tree=${env.WORKSPACE} --git-dir=${env.WORKSPACE}/.git submodule update --force --init --recursive --remote"
}
}
stage ('Build and Test') {
environment {
BUILDDIR = "build"
}
parallel {
stage ('Debug w/mod-parsec') {
environment {
BUILDTYPE = "Debug"
BUILDDIR = "${env.BUILDDIR}/${env.BUILDTYPE}"
}
options {
timeout(time:1, unit: 'HOURS')
}
steps {
sh "echo 'Prepare for a ${env.BUILDTYPE} build'"
sh "mkdir -p ${env.BUILDDIR}"
dir (env.BUILDDIR) {
sh "${env.WORKSPACE}/contrib/jenkins/script.saturn ${env.BUILDTYPE}"
}
}
}
stage ('Release w/mod-parsec') {
environment {
BUILDTYPE = "Release"
BUILDDIR = "${env.BUILDDIR}/${env.BUILDTYPE}"
}
options {
timeout(time:1, unit: 'HOURS')
}
steps {
sh "echo 'Prepare for a ${env.BUILDTYPE} build'"
sh "mkdir -p ${env.BUILDDIR}"
dir (env.BUILDDIR) {
sh "${env.WORKSPACE}/contrib/jenkins/script.saturn ${env.BUILDTYPE}"
}
}
}
stage ('Debug w/ext-parsec') {
environment {
BUILDTYPE = "Debug-ext"
BUILDDIR = "${env.BUILDDIR}/${env.BUILDTYPE}"
}
options {
timeout(time:1, unit: 'HOURS')
}
steps {
sh "echo 'Prepare for a ${env.BUILDTYPE} w/external PaRSEC build'"
sh "mkdir -p ${env.BUILDDIR}"
dir (env.BUILDDIR) {
sh "${env.WORKSPACE}/contrib/jenkins/script.saturn ${env.BUILDTYPE}"
}
}
}
}
}
}
post {
always {
script {
BUILD_USER = env.CHANGE_AUTHOR
}
slackSend channel: '#ci',
color: COLOR_MAP[currentBuild.currentResult],
message: "*${currentBuild.currentResult}:* Job ${env.JOB_NAME} build ${env.BUILD_NUMBER} by ${BUILD_USER}\n More info at: ${env.BUILD_URL}"
}
regression {
slackSend channel: slackResponse.channelId, timestamp: slackResponse.ts,
message: "${PROJECT_NAME} REGRESSION: Job <a href=\"${env.BUILD_URL}\">${env.JOB_NAME} [${env.BUILD_NUMBER}]</a><br>" +
"Pull Request <a href=\"${env.CHANGE_URL}\">#${env.CHANGE_ID}</a>."
}
success {
approvePullRequest(config.repository, env.CHANGE_ID)
}
failure {
unapprovePullRequest(config.repository, env.CHANGE_ID)
}
}
}
// Find the DPLASMA version from the CMakeLists.txt file
String getVersion() {
try {
def p = ['grep', '(DPLASMA_VERSION_', 'CMakeLists.txt'].execute() | ['awk', '/^set/ {printf \"%d.\", $3}'].execute()
p.waitFor()
if( 0 == p.exitValue() ) {
return p.in.text[0..-2]
} else {
println "ERROR: ${p.exitValue()} -> ${p.err.text}"
}
} catch (err) {
println "ERROR: while retrieving DPLASMA version from CMakeLists.txt"
throw err
}
return "0.0"
}
def displayServerResponse( InputStream is ) {
BufferedReader input = new BufferedReader(new InputStreamReader(is))
String inputLine;
while ((inputLine = input.readLine()) != null)
println inputLine
input.close();
}
// Check if the branch author has validated his work by approving
// the branch.
def isApprovedBranch(repository, pr, byWho) {
Boolean prApproved = false
url = "https://api.bitbucket.org/2.0/repositories/${config.organization}/${repository}/pullrequests/${pr}/"
println "Check URL: ${url}"
def url = new URL(url)
def conn = url.openConnection()
conn.setRequestProperty( "Authorization", config.basicAuth)
try {
statusCode = conn.getResponseCode()
if (statusCode==200) {
InputStream inputStream = conn.getInputStream()
def names = new groovy.json.JsonSlurper().parseText(inputStream.text)
//names.each{ k, v ->
// println "element name: ${k}, element value: ${v}"
//}
inputStream.close()
names['participants'].each {
if( it['approved'] ) {
if( it['user']['username'] == byWho ) {
prApproved = true
println "Pullrequest ${pr} approved by ${byWho}"
}
}
}
} else {
println "[Check Approval] Connection status code: $statusCode "
println "[Check Approval] URL ${url.toString()}"
println "[Check Approval] Server response:"
println "[Check Approval] -----"
response=displayServerResponse(conn.getErrorStream())
println "[Check Approval] -----"
}
} catch (Exception e) {
println "[Check Approval] Error connecting to the URL"
println e.getMessage()
} finally {
if (conn != null) {
conn.disconnect();
}
}
return prApproved
}
def approvePullRequest(repository, pr) {
Boolean prApproved = false
def url = new URL("https://api.bitbucket.org/2.0/repositories/${config.organization}/${repository}/pullrequests/${pr}/approve")
def HttpURLConnection conn = (HttpURLConnection)url.openConnection()
conn.setRequestMethod("POST")
conn.setRequestProperty( "Authorization", config.basicAuth)
conn.setRequestProperty( "Content-type", "application/x-www-form-urlencoded")
conn.setRequestProperty( "Content-Length", "0")
conn.setDoOutput(true)
conn.connect()
try {
statusCode = conn.getResponseCode()
if (statusCode==200) {
InputStream inputStream = conn.getInputStream()
def names = new groovy.json.JsonSlurper().parseText(inputStream.text)
//names.each{ k, v ->
// println "element name: ${k}, element value: ${v}"
//}
inputStream.close()
prApproved = true
println "[Approve RP] PR succesfully approved"
} else if (statusCode==409) {
println "[Approve RP] PR already approved by this user"
prApproved = true
} else {
println "[Approve PR] Connection status code: $statusCode "
println "[Approve PR] URL ${url.toString()}"
println "[Approve PR] Server response:"
response = displayServerResponse(conn.getErrorStream())
println "[Approve PR] ${response}"
println "[Approve PR] -----"
}
} catch (Exception e) {
println "[Approve PR] Error connecting to the URL"
println e.getMessage()
} finally {
if (conn != null) {
conn.disconnect();
}
}
return prApproved
}
def unapprovePullRequest(repository, pr) {
Boolean prUnapproved = false
def url = new URL("https://api.bitbucket.org/2.0/repositories/${config.organization}/${repository}/pullrequests/${pr}/approve")
def HttpURLConnection conn = (HttpURLConnection)url.openConnection()
conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded")
conn.setRequestProperty( "Authorization", config.basicAuth)
conn.setRequestMethod("DELETE")
try {
statusCode = conn.getResponseCode()
if (statusCode==204) {
println "[Unapprove RP] PR succesfully unapproved"
prUnapproved = true
} else if (statusCode==404) {
println "[Unapprove RP] PR not approved by this user"
prUnapproved = true
} else {
println "[Unapprove PR] Connection status code: ${statusCode}"
println "[Unapprove PR] URL ${url.toString()}"
println "[Unapprove PR] Server response:"
response = displayServerResponse(conn.getErrorStream())
println "[Unapprove PR] ${response}"
println "[Unapprove PR] -----"
}
} catch (Exception e) {
println "[Unapprove PR] Error connecting to ${url.toString()}"
println e.getMessage()
} finally {
if (conn != null) {
conn.disconnect();
}
}
return prUnapproved
}