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