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