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