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