source: Jenkinsfile @ 8f466d4

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

Removed use of params inside BuildSettings? ctor

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