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