source: Jenkinsfile @ 7e288c4

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

Remove redundant booleans in Jenkinsfile

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