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