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