source: Jenkinsfile @ 47b3235

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since 47b3235 was 47b3235, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Tentative supress of extraneous output

  • Property mode set to 100644
File size: 11.5 KB
Line 
1#!groovy
2
3//===========================================================================================================
4// Main loop of the compilation
5//===========================================================================================================
6set -x
7
8node ('master'){
9
10        boolean bIsSandbox = env.BRANCH_NAME == "jenkins-sandbox"
11        def err = null
12        def log_needed = false
13
14        stage_name              = ''
15
16        compiler                = null
17        arch_name               = ''
18        architecture    = ''
19
20        do_alltests             = false
21        do_benchmark    = false
22        do_doc          = false
23        do_publish              = false
24        do_sendemail    = true
25
26        builddir = pwd tmp: true
27        srcdir   = pwd tmp: false
28
29        currentBuild.result = "SUCCESS"
30
31        try {
32                //Wrap build to add timestamp to command line
33                wrap([$class: 'TimestamperBuildWrapper']) {
34
35                        notify_server(0)
36
37                        prepare_build()
38
39                        clean()
40
41                        checkout()
42
43                        notify_server(0)
44
45                        build()
46
47                        test()
48
49                        benchmark()
50
51                        build_doc()
52
53                        publish()
54
55                        notify_server(45)
56                }
57        }
58
59        //If an exception is caught we need to change the status and remember to
60        //attach the build log to the email
61        catch (Exception caughtError) {
62                //rethrow error later
63                err = caughtError
64
65                //An error has occured, the build log is relevent
66                log_needed = true
67
68                //Store the result of the build log
69                currentBuild.result = "${stage_name} FAILURE".trim()
70        }
71
72        finally {
73                //Send email with final results if this is not a full build
74                if( do_sendemail ) {
75                        echo 'Notifying users of result'
76                        email(currentBuild.result, log_needed, bIsSandbox)
77                }
78
79                echo 'Build Completed'
80
81                /* Must re-throw exception to propagate error */
82                if (err) {
83                        throw err
84                }
85        }
86}
87
88//===========================================================================================================
89// Helper classes/variables/routines
90//===========================================================================================================
91//Helper routine to collect information about the git history
92def collect_git_info() {
93
94        checkout scm
95
96        //create the temporary output directory in case it doesn't already exist
97        def out_dir = pwd tmp: true
98        sh "mkdir -p ${out_dir}"
99
100        //parse git logs to find what changed
101        gitRefName = env.BRANCH_NAME
102        sh "git reflog > ${out_dir}/GIT_COMMIT"
103        git_reflog = readFile("${out_dir}/GIT_COMMIT")
104        gitRefOldValue = (git_reflog =~ /moving from (.+) to (.+)/)[0][1]
105        gitRefNewValue = (git_reflog =~ /moving from (.+) to (.+)/)[0][2]
106}
107
108def prepare_build() {
109        properties ([                                                                                                   \
110                [$class: 'ParametersDefinitionProperty',                                                                \
111                        parameterDefinitions: [                                                                         \
112                                [$class: 'ChoiceParameterDefinition',                                           \
113                                        description: 'Which compiler to use',                                   \
114                                        name: 'Compiler',                                                                       \
115                                        choices: 'gcc-6\ngcc-5\ngcc-4.9\nclang',                                        \
116                                        defaultValue: 'gcc-6',                                                          \
117                                ],                                                                                              \
118                                [$class: 'ChoiceParameterDefinition',                                           \
119                                        description: 'The target architecture',                                 \
120                                        name: 'Architecture',                                                           \
121                                        choices: 'x64\nx86',                                                            \
122                                        defaultValue: 'x64',                                                            \
123                                ],                                                                                              \
124                                [$class: 'BooleanParameterDefinition',                                                  \
125                                        description: 'If false, only the quick test suite is ran',              \
126                                        name: 'RunAllTests',                                                            \
127                                        defaultValue: false,                                                            \
128                                ],                                                                                              \
129                                [$class: 'BooleanParameterDefinition',                                                  \
130                                        description: 'If true, jenkins also runs benchmarks',           \
131                                        name: 'RunBenchmark',                                                           \
132                                        defaultValue: false,                                                            \
133                                ],                                                                                              \
134                                [$class: 'BooleanParameterDefinition',                                                  \
135                                        description: 'If true, jenkins also builds documentation',              \
136                                        name: 'BuildDocumentation',                                                     \
137                                        defaultValue: true,                                                             \
138                                ],                                                                                              \
139                                [$class: 'BooleanParameterDefinition',                                                  \
140                                        description: 'If true, jenkins also publishes results',                 \
141                                        name: 'Publish',                                                                        \
142                                        defaultValue: false,                                                            \
143                                ],                                                                                              \
144                                [$class: 'BooleanParameterDefinition',                                                  \
145                                        description: 'If true, jenkins will not send emails',           \
146                                        name: 'Silent',                                                                         \
147                                        defaultValue: false,                                                            \
148                                ],                                                                                              \
149                        ],
150                ]])
151
152        compiler                = compiler_from_params( params.Compiler )
153        arch_name               = params.Architecture
154        architecture    = architecture_from_params( arch_name )
155
156        do_alltests             = (params.RunAllTests == 'true')
157        do_benchmark    = (params.RunBenchmark == 'true')
158        do_doc          = (params.BuildDocumentation == 'true')
159        do_publish              = (params.Publish == 'true')
160        do_sendemail    = ! (params.Silent == 'true')
161
162        echo params.RunAllTests.getClass().toString();
163
164        collect_git_info()
165
166        def full = do_alltests ? " (Full)" : ""
167        currentBuild.description = "${compiler.cc_name}:${arch_name}${full}"
168
169        echo """Compiler                 : ${ compiler.cc_name } (${ compiler.cpp_cc }/${ compiler.cfa_cc })
170Architecture            : ${ arch_name }
171Arc Flags               : ${ architecture }
172Run All Tests           : ${ params.RunAllTests.toString() }
173Run Benchmark           : ${ params.RunBenchmark.toString() }
174Build Documentation     : ${ params.BuildDocumentation.toString() }
175Publish                 : ${ params.Publish.toString() }
176Silent                  : ${ params.Silent.toString() }
177"""
178}
179
180def build_stage(String name, Closure block ) {
181        stage_name = name
182        stage(name, block)
183}
184
185def notify_server(int wait) {
186        sh """curl --silent --show-error --data "wait=${wait}" -X POST https://cforall.uwaterloo.ca:8082/jenkins/notify > /dev/null || true"""
187        return
188}
189
190def make_doc() {
191        def err = null
192        try {
193                sh 'make clean > /dev/null'
194                sh 'make > /dev/null 2>&1'
195        }
196        catch (Exception caughtError) {
197                err = caughtError //rethrow error later
198                sh 'cat *.log'
199        }
200        finally {
201                if (err) throw err // Must re-throw exception to propagate error
202        }
203}
204
205//Description of a compiler (Must be serializable since pipelines are persistent)
206class CC_Desc implements Serializable {
207        public String cc_name
208        public String cpp_cc
209        public String cfa_cc
210
211        CC_Desc(String cc_name, String cpp_cc, String cfa_cc) {
212                this.cc_name = cc_name
213                this.cpp_cc = cpp_cc
214                this.cfa_cc = cfa_cc
215        }
216}
217
218def compiler_from_params(cc) {
219        switch( cc ) {
220                case 'gcc-6':
221                        return new CC_Desc('gcc-6', 'g++-6', 'gcc-6')
222                break
223                case 'gcc-5':
224                        return new CC_Desc('gcc-5', 'g++-5', 'gcc-5')
225                break
226                case 'gcc-4.9':
227                        return new CC_Desc('gcc-4.9', 'g++-4.9', 'gcc-4.9')
228                break
229                case 'clang':
230                        return new CC_Desc('clang', 'clang++', 'gcc-6')
231                break
232                default :
233                        error "Unhandled compiler : ${cc}"
234        }
235}
236
237def architecture_from_params( arch ) {
238        switch( arch ) {
239                case 'x64':
240                        return '--host=x86_64'
241                break
242                case 'x86':
243                        return '--host=i386'
244                break
245                default :
246                        error "Unhandled architecture : ${arch}"
247        }
248}
249
250//===========================================================================================================
251// Main compilation routines
252//===========================================================================================================
253def clean() {
254        build_stage('Cleanup') {
255                // clean the build by wipping the build directory
256                dir(builddir) {
257                        deleteDir()
258                }
259
260                //Clean all temporary files to make sure no artifacts of the previous build remain
261                sh 'git clean -fdqx'
262
263                //Reset the git repo so no local changes persist
264                sh 'git reset --hard'
265        }
266}
267
268//Compilation script is done here but environnement set-up and error handling is done in main loop
269def checkout() {
270        build_stage('Checkout') {
271                //checkout the source code and clean the repo
272                checkout scm
273        }
274}
275
276def build() {
277        build_stage('Build') {
278                // Build outside of the src tree to ease cleaning
279                dir (builddir) {
280                        //Configure the conpilation (Output is not relevant)
281                        //Use the current directory as the installation target so nothing escapes the sandbox
282                        //Also specify the compiler by hand
283                        targets=""
284                        if(do_alltests) {
285                                targets="--with-target-hosts='host:debug,host:nodebug'"
286                        } else {
287                                targets="--with-target-hosts='host:debug'"
288                        }
289
290                        sh "${srcdir}/configure CXX=${compiler.cpp_cc} ${architecture} ${targets} --with-backend-compiler=${compiler.cfa_cc} --quiet"
291
292                        //Compile the project
293                        sh 'make -j 8 --no-print-directory'
294                }
295        }
296}
297
298def test() {
299        build_stage('Test') {
300
301                dir (builddir) {
302                        //Run the tests from the tests directory
303                        if ( do_alltests ) {
304                                sh 'make --no-print-directory -C tests all-tests debug=yes'
305                                sh 'make --no-print-directory -C tests all-tests debug=no '
306                        }
307                        else {
308                                sh 'make --no-print-directory -C tests'
309                        }
310                }
311        }
312}
313
314def benchmark() {
315        build_stage('Benchmark') {
316
317                if( !do_benchmark ) return
318
319                dir (builddir) {
320                        //Append bench results
321                        sh "make --no-print-directory -C benchmark jenkins githash=${gitRefNewValue} arch=${arch_name} | tee ${srcdir}/bench.json"
322                }
323        }
324}
325
326def build_doc() {
327        build_stage('Documentation') {
328
329                if( !do_doc ) return
330
331                dir ('doc/user') {
332                        make_doc()
333                }
334
335                dir ('doc/refrat') {
336                        make_doc()
337                }
338        }
339}
340
341def publish() {
342        build_stage('Publish') {
343
344                if( !do_publish ) return
345
346                //Then publish the results
347                sh 'curl --silent --show-error -H \'Content-Type: application/json\' --data @bench.json https://cforall.uwaterloo.ca:8082/jenkins/publish > /dev/null || true'
348        }
349}
350
351//===========================================================================================================
352//Routine responsible of sending the email notification once the build is completed
353//===========================================================================================================
354def gitBranchUpdate(String gitRefOldValue, String gitRefNewValue) {
355        def update = ""
356        sh "git rev-list ${gitRefOldValue}..${gitRefNewValue} > GIT_LOG";
357        readFile('GIT_LOG').eachLine { rev ->
358                sh "git cat-file -t ${rev} > GIT_TYPE"
359                def type = readFile('GIT_TYPE')
360
361                update += "       via  ${rev} (${type})\n"
362        }
363        def rev = gitRefOldValue
364        sh "git cat-file -t ${rev} > GIT_TYPE"
365        def type = readFile('GIT_TYPE')
366
367        update += "      from  ${rev} (${type})\n"
368        return update
369
370def output=readFile('result').trim()
371echo "output=$output";
372}
373
374//Standard build email notification
375def email(String status, boolean log, boolean bIsSandbox) {
376        //Since tokenizer doesn't work, figure stuff out from the environnement variables and command line
377        //Configurations for email format
378        def project_name = (env.JOB_NAME =~ /(.+)\/.+/)[0][1].toLowerCase()
379
380        def gitLog = 'Error retrieving git logs'
381        def gitDiff = 'Error retrieving git diff'
382        def gitUpdate = 'Error retrieving update'
383
384        try {
385                gitUpdate = gitBranchUpdate(gitRefOldValue, gitRefNewValue)
386
387                sh "git rev-list --format=short ${gitRefOldValue}...${gitRefNewValue} > GIT_LOG"
388                gitLog = readFile('GIT_LOG')
389
390                sh "git diff --stat ${gitRefNewValue} ${gitRefOldValue} > GIT_DIFF"
391                gitDiff = readFile('GIT_DIFF')
392        }
393        catch (Exception error) {
394                echo error.toString()
395                echo error.getMessage()
396        }
397
398        def email_subject = "[${project_name} git][BUILD# ${env.BUILD_NUMBER} - ${status}] - branch ${env.BRANCH_NAME}"
399        def email_body = """This is an automated email from the Jenkins build machine. It was
400generated because of a git hooks/post-receive script following
401a ref change was pushed to the repository containing
402the project "UNNAMED PROJECT".
403
404The branch ${env.BRANCH_NAME} has been updated.
405${gitUpdate}
406
407Check console output at ${env.BUILD_URL} to view the results.
408
409- Status --------------------------------------------------------------
410
411BUILD# ${env.BUILD_NUMBER} - ${status}
412
413- Log -----------------------------------------------------------------
414${gitLog}
415-----------------------------------------------------------------------
416Summary of changes:
417${gitDiff}
418"""
419
420        def email_to = "cforall@lists.uwaterloo.ca"
421
422        if( !bIsSandbox ) {
423                //send email notification
424                emailext body: email_body, subject: email_subject, to: email_to, attachLog: log
425        } else {
426                echo "Would send email to: ${email_to}"
427                echo "With title: ${email_subject}"
428                echo "Content: \n${email_body}"
429        }
430}
Note: See TracBrowser for help on using the repository browser.