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