SHARE
TWEET

Jenkinsfile-rescan-MBPs

jimklimov Mar 16th, 2018 321 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env groovy
  2.  
  3. // Jenkinsfile-rescan-MBPs
  4. // (C) 2018 by Jim Klimov
  5. // This Jenkinsfile makes-believe it is a Declarative pipeline for
  6. // the sake of commonality of our pipeline scripts (build args,
  7. // various options and stuff), but in fact this is a scripted
  8. // pipeline most of the way.
  9. // Its job is to find MultiBranchPipeline items spawned inside an
  10. // organization folder and issue a rescan request (build operation),
  11. // so we can detect new or closed PRs and branches quickly (due to
  12. // JENKINS-49526 MBPs are otherwise polled once a day, hardcoded).
  13.  
  14. import jenkins.model.*
  15. import hudson.model.*
  16. import hudson.util.PersistedList
  17. import jenkins.branch.*
  18.  
  19. // This shared array variable will be populated with the list of parallel stages
  20. def scan_stages = [:]
  21.  
  22. @NonCPS
  23. def parallelSubtasks(String verbose) {
  24.     def subbuilds = [:]
  25.     def jobs = Jenkins.instance.getAllItems()
  26.  
  27.     jobs.each { j ->
  28.         if (j instanceof com.cloudbees.hudson.plugins.folder.Folder) {
  29.             if (verbose.equals("true")) {
  30.                 echo 'Ignoring JOB which is a com.cloudbees.hudson.plugins.folder.Folder : ' + j.fullName
  31.             }
  32.             return
  33.         }
  34.         if (j instanceof jenkins.branch.OrganizationFolder) {
  35.             if (verbose.equals("true")) {
  36.                 echo 'Ignoring JOB which is a jenkins.branch.OrganizationFolder : ' + j.fullName
  37.             }
  38.             return
  39.         }
  40.         if (! (j instanceof org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) ) {
  41.             if (verbose.equals("true")) {
  42.                 echo 'Ignoring JOB which is not an MBP: ' + j.fullName
  43.             }
  44.             return;
  45.         }
  46.         if ( ! j.fullName.contains("/") ) {
  47.             echo 'Ignoring MBP JOB which is not under a (organization) folder, so has and hopefully honours a schedule of its own: ' + j.fullName
  48.             return;
  49.         }
  50.         // TODO: Determine that MBP's schedule and only fire below if it is "once a day" (hardcoded)?
  51.  
  52.         subbuilds["${j.fullName}"] = {
  53.             stage("Scan ${j.fullName}") {
  54.                 // Per non-declarative pipeline syntax, no steps{} here
  55.                     echo "Rescanning '${j.fullName}' ..."
  56.                     // Jenkins refuses to "wait" for this type of job...
  57.                     // TODO: Sleep-poll the results (and logs?) of the
  58.                     // spawned child jobs.
  59.                     try {
  60.                         def bbb = build job: "${j.fullName}", quietPeriod: 0, wait: false, propagate: true
  61.                     } catch (Exception e) {
  62.                         // e.g. a disabled repo, like one where a Jenkinsfile
  63.                         // script existed earlier but was removed
  64.                         echo "Failed to trigger scan for ${j.fullName}, skipped"
  65.                     }
  66.             }
  67.         } // end of def subbuilds[j.fullName]
  68.  
  69.     } // jobs.each() clause
  70.  
  71.     return subbuilds
  72. }
  73.  
  74.  
  75. // A declarative pipeline shiny wrapper (we need it
  76. // for common ways of autosetup and params, mostly)
  77. pipeline {
  78.     options {
  79. /*
  80.         description("This job runs regularly to find and rescan multibranch pipeline jobs (e.g. generated via organization folders) to add monitoring of new PRs (and disable monitoring of merged PRs) in a timely fashion.")
  81. */
  82.         disableConcurrentBuilds()
  83.         disableResume()
  84.         durabilityHint('PERFORMANCE_OPTIMIZED')
  85.         buildDiscarder(logRotator(numToKeepStr: '10'))
  86.         skipDefaultCheckout true
  87.     }
  88.     triggers {
  89.         cron('H/15 * * * *')
  90.     }
  91.     agent {label "master||master-real||master-worker"}
  92.     parameters {
  93.         booleanParam (
  94.             defaultValue: false,
  95.             description: 'Print found and skipped items?',
  96.             name: 'JOBLIST_VERBOSE'
  97.         )
  98.     }
  99.     stages {
  100.         stage('Single-exec milestone') {
  101.             steps {
  102.                 milestone 1
  103.             } // steps clause
  104.         }
  105.     }
  106. }
  107.  
  108. // Non-declarative pipeline payload.
  109. // This code below does not work inside a pipeline{} stage{}
  110. // clause nor in the post{} clause - not even with the added
  111. // try/catches for exceptions all around.
  112. // Curiously, when this pipeline job is built by hand
  113. // or triggered by timer, it fails with lots of CPS and
  114. // marshalling exception messages in the end. Replaying
  115. // the same groovy script with no changes just works.
  116. try {
  117.     echo "DISCOVERING JOBS..."
  118.     scan_stages = parallelSubtasks( "${params.JOBLIST_VERBOSE}" )
  119. } catch (java.io.NotSerializableException e) {
  120.     echo "Ignoring NotSerializableException during parallelSubtasks()"
  121. } catch (Exception e) {
  122.     echo "Failed to find jobs or set up parallel scans"
  123.     currentBuild.result = 'FAILED'
  124.     manager.buildAborted()
  125. }
  126.  
  127. try {
  128.     echo "SCHEDULING RESCANS..."
  129.     parallel scan_stages
  130.     echo "SCHEDULING OF RESCANS COMPLETED"
  131. } catch (java.io.NotSerializableException e) {
  132.     echo "Ignoring NotSerializableException during parallelized work"
  133. } catch (Exception e) {
  134.     echo "Failed to trigger parallel scans"
  135.     currentBuild.result = 'FAILED'
  136.     manager.buildAborted()
  137. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top