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