source: Jenkinsfile @ deb6185

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

Add missing global for git ref

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