1 | #!groovy |
---|
2 | |
---|
3 | //=========================================================================================================== |
---|
4 | // Main loop of the compilation |
---|
5 | //=========================================================================================================== |
---|
6 | node ('master'){ |
---|
7 | |
---|
8 | boolean bIsSandbox = env.BRANCH_NAME == "jenkins-sandbox" |
---|
9 | def err = null |
---|
10 | def log_needed = false |
---|
11 | |
---|
12 | stage_name = '' |
---|
13 | |
---|
14 | compiler = null |
---|
15 | arch_name = '' |
---|
16 | architecture = '' |
---|
17 | |
---|
18 | do_alltests = false |
---|
19 | do_benchmark = false |
---|
20 | do_doc = false |
---|
21 | do_publish = false |
---|
22 | do_sendemail = true |
---|
23 | |
---|
24 | builddir = pwd tmp: true |
---|
25 | srcdir = pwd tmp: false |
---|
26 | |
---|
27 | currentBuild.result = "SUCCESS" |
---|
28 | |
---|
29 | try { |
---|
30 | //Wrap build to add timestamp to command line |
---|
31 | wrap([$class: 'TimestamperBuildWrapper']) { |
---|
32 | |
---|
33 | notify_server(0) |
---|
34 | |
---|
35 | prepare_build() |
---|
36 | |
---|
37 | clean() |
---|
38 | |
---|
39 | checkout() |
---|
40 | |
---|
41 | notify_server(0) |
---|
42 | |
---|
43 | build() |
---|
44 | |
---|
45 | test() |
---|
46 | |
---|
47 | benchmark() |
---|
48 | |
---|
49 | build_doc() |
---|
50 | |
---|
51 | publish() |
---|
52 | |
---|
53 | notify_server(45) |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | //If an exception is caught we need to change the status and remember to |
---|
58 | //attach the build log to the email |
---|
59 | catch (Exception caughtError) { |
---|
60 | //rethrow error later |
---|
61 | err = caughtError |
---|
62 | |
---|
63 | //An error has occured, the build log is relevent |
---|
64 | log_needed = true |
---|
65 | |
---|
66 | //Store the result of the build log |
---|
67 | currentBuild.result = "${stage_name} FAILURE".trim() |
---|
68 | } |
---|
69 | |
---|
70 | finally { |
---|
71 | //Send email with final results if this is not a full build |
---|
72 | if( do_sendemail ) { |
---|
73 | echo 'Notifying users of result' |
---|
74 | email(currentBuild.result, log_needed, bIsSandbox) |
---|
75 | } |
---|
76 | |
---|
77 | echo 'Build Completed' |
---|
78 | |
---|
79 | /* Must re-throw exception to propagate error */ |
---|
80 | if (err) { |
---|
81 | throw err |
---|
82 | } |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | //=========================================================================================================== |
---|
87 | // Helper classes/variables/routines |
---|
88 | //=========================================================================================================== |
---|
89 | //Helper routine to collect information about the git history |
---|
90 | def collect_git_info() { |
---|
91 | |
---|
92 | checkout scm |
---|
93 | |
---|
94 | //create the temporary output directory in case it doesn't already exist |
---|
95 | def out_dir = pwd tmp: true |
---|
96 | sh "mkdir -p ${out_dir}" |
---|
97 | |
---|
98 | //parse git logs to find what changed |
---|
99 | gitRefName = env.BRANCH_NAME |
---|
100 | sh "git reflog > ${out_dir}/GIT_COMMIT" |
---|
101 | git_reflog = readFile("${out_dir}/GIT_COMMIT") |
---|
102 | gitRefOldValue = (git_reflog =~ /moving from (.+) to (.+)/)[0][1] |
---|
103 | gitRefNewValue = (git_reflog =~ /moving from (.+) to (.+)/)[0][2] |
---|
104 | } |
---|
105 | |
---|
106 | def prepare_build() { |
---|
107 | properties ([ \ |
---|
108 | [$class: 'ParametersDefinitionProperty', \ |
---|
109 | parameterDefinitions: [ \ |
---|
110 | [$class: 'ChoiceParameterDefinition', \ |
---|
111 | description: 'Which compiler to use', \ |
---|
112 | name: 'pCompiler', \ |
---|
113 | choices: 'gcc-6\ngcc-5\ngcc-4.9\nclang', \ |
---|
114 | defaultValue: 'gcc-6', \ |
---|
115 | ], \ |
---|
116 | [$class: 'ChoiceParameterDefinition', \ |
---|
117 | description: 'The target architecture', \ |
---|
118 | name: 'pArchitecture', \ |
---|
119 | choices: 'x64\nx86', \ |
---|
120 | defaultValue: 'x64', \ |
---|
121 | ], \ |
---|
122 | [$class: 'BooleanParameterDefinition', \ |
---|
123 | description: 'If false, only the quick test suite is ran', \ |
---|
124 | name: 'pRunAllTests', \ |
---|
125 | defaultValue: false, \ |
---|
126 | ], \ |
---|
127 | [$class: 'BooleanParameterDefinition', \ |
---|
128 | description: 'If true, jenkins also runs benchmarks', \ |
---|
129 | name: 'pRunBenchmark', \ |
---|
130 | defaultValue: true, \ |
---|
131 | ], \ |
---|
132 | [$class: 'BooleanParameterDefinition', \ |
---|
133 | description: 'If true, jenkins also builds documentation', \ |
---|
134 | name: 'pBuildDocumentation', \ |
---|
135 | defaultValue: true, \ |
---|
136 | ], \ |
---|
137 | [$class: 'BooleanParameterDefinition', \ |
---|
138 | description: 'If true, jenkins also publishes results', \ |
---|
139 | name: 'pPublish', \ |
---|
140 | defaultValue: false, \ |
---|
141 | ], \ |
---|
142 | [$class: 'BooleanParameterDefinition', \ |
---|
143 | description: 'If true, jenkins will not send emails', \ |
---|
144 | name: 'pSilent', \ |
---|
145 | defaultValue: false, \ |
---|
146 | ], \ |
---|
147 | ], |
---|
148 | ]]) |
---|
149 | |
---|
150 | compiler = compiler_from_params( pCompiler ) |
---|
151 | arch_name = pArchitecture |
---|
152 | architecture = architecture_from_params( arch_name ) |
---|
153 | |
---|
154 | do_alltests = (pRunAllTests == 'true') |
---|
155 | do_benchmark = (pRunBenchmark == 'true') |
---|
156 | do_doc = (pBuildDocumentation == 'true') |
---|
157 | do_publish = (pPublish == 'true') |
---|
158 | do_sendemail = ! (pSilent == 'true') |
---|
159 | |
---|
160 | collect_git_info() |
---|
161 | |
---|
162 | def full = do_alltests ? " (Full)" : "" |
---|
163 | currentBuild.description = "${compiler.cc_name}:${arch_name}${full}" |
---|
164 | |
---|
165 | echo """Compiler : ${compiler.cc_name} (${compiler.cpp_cc}/${compiler.cfa_cc}) |
---|
166 | Architecture : ${arch_name} |
---|
167 | Arc Flags : ${architecture} |
---|
168 | Run All Tests : ${ pRunAllTests.toString() } |
---|
169 | Run Benchmark : ${ pRunBenchmark.toString() } |
---|
170 | Build Documentation : ${ pBuildDocumentation.toString() } |
---|
171 | Publish : ${ pPublish.toString() } |
---|
172 | Silent : ${ pSilent.toString() } |
---|
173 | """ |
---|
174 | |
---|
175 | |
---|
176 | //Since tokenizer doesn't work, figure stuff out from the environnement variables and command line |
---|
177 | //Configurations for email format |
---|
178 | def project_name = (env.JOB_NAME =~ /(.+)\/.+/)[0][1].toLowerCase() |
---|
179 | |
---|
180 | def gitLog = 'Error retrieving git logs' |
---|
181 | def gitDiff = 'Error retrieving git diff' |
---|
182 | def gitUpdate = 'Error retrieving update' |
---|
183 | |
---|
184 | try { |
---|
185 | gitUpdate = gitBranchUpdate(gitRefOldValue, gitRefNewValue) |
---|
186 | |
---|
187 | sh "git rev-list --format=short ${gitRefOldValue}...${gitRefNewValue} > GIT_LOG" |
---|
188 | gitLog = readFile('GIT_LOG') |
---|
189 | |
---|
190 | sh "git diff --stat ${gitRefNewValue} ${gitRefOldValue} > GIT_DIFF" |
---|
191 | gitDiff = readFile('GIT_DIFF') |
---|
192 | } |
---|
193 | catch (Exception error) { |
---|
194 | echo error.toString() |
---|
195 | echo error.getMessage() |
---|
196 | } |
---|
197 | |
---|
198 | def email_subject = "[${project_name} git][BUILD# ${env.BUILD_NUMBER} - ${status}] - branch ${env.BRANCH_NAME}" |
---|
199 | def email_body = """This is an automated email from the Jenkins build machine. It was |
---|
200 | generated because of a git hooks/post-receive script following |
---|
201 | a ref change was pushed to the repository containing |
---|
202 | the project "UNNAMED PROJECT". |
---|
203 | |
---|
204 | The branch ${env.BRANCH_NAME} has been updated. |
---|
205 | ${gitUpdate} |
---|
206 | |
---|
207 | Check console output at ${env.BUILD_URL} to view the results. |
---|
208 | |
---|
209 | - Status -------------------------------------------------------------- |
---|
210 | |
---|
211 | BUILD# ${env.BUILD_NUMBER} - ${status} |
---|
212 | |
---|
213 | - Log ----------------------------------------------------------------- |
---|
214 | ${gitLog} |
---|
215 | ----------------------------------------------------------------------- |
---|
216 | Summary of changes: |
---|
217 | ${gitDiff} |
---|
218 | """ |
---|
219 | |
---|
220 | echo email_body |
---|
221 | } |
---|
222 | |
---|
223 | def build_stage(String name, Closure block ) { |
---|
224 | stage_name = name |
---|
225 | stage(name, block) |
---|
226 | } |
---|
227 | |
---|
228 | def notify_server(int wait) { |
---|
229 | sh """curl --silent --show-error --data "wait=${wait}" -X POST https://cforall.uwaterloo.ca:8082/jenkins/notify > /dev/null || true""" |
---|
230 | return |
---|
231 | } |
---|
232 | |
---|
233 | def make_doc() { |
---|
234 | def err = null |
---|
235 | try { |
---|
236 | sh 'make clean > /dev/null' |
---|
237 | sh 'make > /dev/null 2>&1' |
---|
238 | } |
---|
239 | catch (Exception caughtError) { |
---|
240 | err = caughtError //rethrow error later |
---|
241 | sh 'cat *.log' |
---|
242 | } |
---|
243 | finally { |
---|
244 | if (err) throw err // Must re-throw exception to propagate error |
---|
245 | } |
---|
246 | } |
---|
247 | |
---|
248 | //Description of a compiler (Must be serializable since pipelines are persistent) |
---|
249 | class CC_Desc implements Serializable { |
---|
250 | public String cc_name |
---|
251 | public String cpp_cc |
---|
252 | public String cfa_cc |
---|
253 | |
---|
254 | CC_Desc(String cc_name, String cpp_cc, String cfa_cc) { |
---|
255 | this.cc_name = cc_name |
---|
256 | this.cpp_cc = cpp_cc |
---|
257 | this.cfa_cc = cfa_cc |
---|
258 | } |
---|
259 | } |
---|
260 | |
---|
261 | def compiler_from_params(cc) { |
---|
262 | switch( cc ) { |
---|
263 | case 'gcc-6': |
---|
264 | return new CC_Desc('gcc-6', 'g++-6', 'gcc-6') |
---|
265 | break |
---|
266 | case 'gcc-5': |
---|
267 | return new CC_Desc('gcc-5', 'g++-5', 'gcc-5') |
---|
268 | break |
---|
269 | case 'gcc-4.9': |
---|
270 | return new CC_Desc('gcc-4.9', 'g++-4.9', 'gcc-4.9') |
---|
271 | break |
---|
272 | case 'clang': |
---|
273 | return new CC_Desc('clang', 'clang++', 'gcc-6') |
---|
274 | break |
---|
275 | default : |
---|
276 | error "Unhandled compiler : ${cc}" |
---|
277 | } |
---|
278 | } |
---|
279 | |
---|
280 | def architecture_from_params( arch ) { |
---|
281 | switch( arch ) { |
---|
282 | case 'x64': |
---|
283 | return '--host=x86_64' |
---|
284 | break |
---|
285 | case 'x86': |
---|
286 | return '--host=i386' |
---|
287 | break |
---|
288 | default : |
---|
289 | error "Unhandled architecture : ${arch}" |
---|
290 | } |
---|
291 | } |
---|
292 | |
---|
293 | //=========================================================================================================== |
---|
294 | // Main compilation routines |
---|
295 | //=========================================================================================================== |
---|
296 | def clean() { |
---|
297 | build_stage('Cleanup') { |
---|
298 | // clean the build by wipping the build directory |
---|
299 | dir(builddir) { |
---|
300 | deleteDir() |
---|
301 | } |
---|
302 | |
---|
303 | //Clean all temporary files to make sure no artifacts of the previous build remain |
---|
304 | sh 'git clean -fdqx' |
---|
305 | |
---|
306 | //Reset the git repo so no local changes persist |
---|
307 | sh 'git reset --hard' |
---|
308 | } |
---|
309 | } |
---|
310 | |
---|
311 | //Compilation script is done here but environnement set-up and error handling is done in main loop |
---|
312 | def checkout() { |
---|
313 | build_stage('Checkout') { |
---|
314 | //checkout the source code and clean the repo |
---|
315 | checkout scm |
---|
316 | } |
---|
317 | } |
---|
318 | |
---|
319 | def build() { |
---|
320 | build_stage('Build') { |
---|
321 | // Build outside of the src tree to ease cleaning |
---|
322 | dir (builddir) { |
---|
323 | //Configure the conpilation (Output is not relevant) |
---|
324 | //Use the current directory as the installation target so nothing escapes the sandbox |
---|
325 | //Also specify the compiler by hand |
---|
326 | sh "${srcdir}/configure CXX=${compiler.cpp_cc} ${architecture} --with-backend-compiler=${compiler.cfa_cc} --enable-silent-rules --quiet" |
---|
327 | |
---|
328 | //Compile the project |
---|
329 | sh 'make -j 8 --no-print-directory' |
---|
330 | } |
---|
331 | } |
---|
332 | } |
---|
333 | |
---|
334 | def test() { |
---|
335 | build_stage('Test') { |
---|
336 | |
---|
337 | dir (builddir) { |
---|
338 | //Run the tests from the tests directory |
---|
339 | if ( do_alltests ) { |
---|
340 | sh 'make --no-print-directory -C src/tests all-tests debug=yes' |
---|
341 | sh 'make --no-print-directory -C src/tests all-tests debug=no ' |
---|
342 | } |
---|
343 | else { |
---|
344 | sh 'make --no-print-directory -C src/tests' |
---|
345 | } |
---|
346 | } |
---|
347 | } |
---|
348 | } |
---|
349 | |
---|
350 | def benchmark() { |
---|
351 | build_stage('Benchmark') { |
---|
352 | |
---|
353 | if( !do_benchmark ) return |
---|
354 | |
---|
355 | dir (builddir) { |
---|
356 | //Append bench results |
---|
357 | sh "make --no-print-directory -C src/benchmark jenkins githash=${gitRefNewValue} arch=${arch_name} | tee ${srcdir}/bench.json" |
---|
358 | } |
---|
359 | } |
---|
360 | } |
---|
361 | |
---|
362 | def build_doc() { |
---|
363 | build_stage('Documentation') { |
---|
364 | |
---|
365 | if( !do_doc ) return |
---|
366 | |
---|
367 | dir ('doc/user') { |
---|
368 | make_doc() |
---|
369 | } |
---|
370 | |
---|
371 | dir ('doc/refrat') { |
---|
372 | make_doc() |
---|
373 | } |
---|
374 | } |
---|
375 | } |
---|
376 | |
---|
377 | def publish() { |
---|
378 | build_stage('Publish') { |
---|
379 | |
---|
380 | if( !do_publish ) return |
---|
381 | |
---|
382 | //Then publish the results |
---|
383 | sh 'curl --silent --show-error -H \'Content-Type: application/json\' --data @bench.json https://cforall.uwaterloo.ca:8082/jenkins/publish > /dev/null || true' |
---|
384 | } |
---|
385 | } |
---|
386 | |
---|
387 | //=========================================================================================================== |
---|
388 | //Routine responsible of sending the email notification once the build is completed |
---|
389 | //=========================================================================================================== |
---|
390 | def gitBranchUpdate(String gitRefOldValue, String gitRefNewValue) { |
---|
391 | def update = "" |
---|
392 | sh "git rev-list ${gitRefOldValue}..${gitRefNewValue} > GIT_LOG"; |
---|
393 | readFile('GIT_LOG').eachLine { rev -> |
---|
394 | sh "git cat-file -t ${rev} > GIT_TYPE" |
---|
395 | def type = readFile('GIT_TYPE') |
---|
396 | |
---|
397 | update += " via ${rev} (${type})\n" |
---|
398 | } |
---|
399 | def rev = gitRefOldValue |
---|
400 | sh "git cat-file -t ${rev} > GIT_TYPE" |
---|
401 | def type = readFile('GIT_TYPE') |
---|
402 | |
---|
403 | update += " from ${rev} (${type})\n" |
---|
404 | return update |
---|
405 | |
---|
406 | def output=readFile('result').trim() |
---|
407 | echo "output=$output"; |
---|
408 | } |
---|
409 | |
---|
410 | //Standard build email notification |
---|
411 | def email(String status, boolean log, boolean bIsSandbox) { |
---|
412 | //Since tokenizer doesn't work, figure stuff out from the environnement variables and command line |
---|
413 | //Configurations for email format |
---|
414 | def project_name = (env.JOB_NAME =~ /(.+)\/.+/)[0][1].toLowerCase() |
---|
415 | |
---|
416 | def gitLog = 'Error retrieving git logs' |
---|
417 | def gitDiff = 'Error retrieving git diff' |
---|
418 | def gitUpdate = 'Error retrieving update' |
---|
419 | |
---|
420 | try { |
---|
421 | gitUpdate = gitBranchUpdate(gitRefOldValue, gitRefNewValue) |
---|
422 | |
---|
423 | sh "git rev-list --format=short ${gitRefOldValue}...${gitRefNewValue} > GIT_LOG" |
---|
424 | gitLog = readFile('GIT_LOG') |
---|
425 | |
---|
426 | sh "git diff --stat ${gitRefNewValue} ${gitRefOldValue} > GIT_DIFF" |
---|
427 | gitDiff = readFile('GIT_DIFF') |
---|
428 | } |
---|
429 | catch (Exception error) {} |
---|
430 | |
---|
431 | def email_subject = "[${project_name} git][BUILD# ${env.BUILD_NUMBER} - ${status}] - branch ${env.BRANCH_NAME}" |
---|
432 | def email_body = """This is an automated email from the Jenkins build machine. It was |
---|
433 | generated because of a git hooks/post-receive script following |
---|
434 | a ref change was pushed to the repository containing |
---|
435 | the project "UNNAMED PROJECT". |
---|
436 | |
---|
437 | The branch ${env.BRANCH_NAME} has been updated. |
---|
438 | ${gitUpdate} |
---|
439 | |
---|
440 | Check console output at ${env.BUILD_URL} to view the results. |
---|
441 | |
---|
442 | - Status -------------------------------------------------------------- |
---|
443 | |
---|
444 | BUILD# ${env.BUILD_NUMBER} - ${status} |
---|
445 | |
---|
446 | - Log ----------------------------------------------------------------- |
---|
447 | ${gitLog} |
---|
448 | ----------------------------------------------------------------------- |
---|
449 | Summary of changes: |
---|
450 | ${gitDiff} |
---|
451 | """ |
---|
452 | |
---|
453 | def email_to = "cforall@lists.uwaterloo.ca" |
---|
454 | |
---|
455 | if( !bIsSandbox ) { |
---|
456 | //send email notification |
---|
457 | emailext body: email_body, subject: email_subject, to: email_to, attachLog: log |
---|
458 | } else { |
---|
459 | echo "Would send email to: ${email_to}" |
---|
460 | echo "With title: ${email_subject}" |
---|
461 | echo "Content: \n${email_body}" |
---|
462 | } |
---|
463 | } |
---|