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