Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Temporarily disable SCM polling on Jenkins Server in System Groovy

We have a Jenkins server which is running somewhere between 20 and 30 jobs.

Since the build process is reasonably complex we're broken the actual build down into 1 sub-builds, some of which can run concurrently, others have to follow previous build steps. As a result we've grouped each of the build steps into 3 groups, which block while the builds are in pogress.

For example:

Main Build : GroupA  : Builds A1, A2 & A3

           : GroupB  : Builds B1, B2 & B3

           : GroupC  : Builds C1, C2, C3, C4, C5 & C6

           : GroupD  : HW_Tests T1, T2, T3, T4 & T5


Builds B1, B2 & B3 rely on the output from A1, A2, A3 etc

Since there are builds and tests running pretty much 24/7, I am finding it difficult to schedule a restart of the Jenkins Master. Choosing "Prepare for shutdown" will mean new jobs are queued, but it will invariably block a running job since, to use my example above, if GroupB is active, builds C1, C2, etc will be queued also, and the Main Build will be blocked.

As a work around, I would like to disable the SCM polling on the server until all running jobs have finished. This will prevent new jobs from triggering but also allow the running jobs to finish. I can then restart Jenkins and , re-enable SCM polling, allowing normal service to resume.

The SCM we are using is Perforce.

I have not been able to find anywhere which suggests the above is possible, however, I am sure it must be feasible in System Groovy ... just not sure how. Does anyone here have any ideas please?

Many Thanks

like image 562
Nick Holt Avatar asked Aug 23 '16 10:08

Nick Holt


2 Answers

You could disable only those jobs which have an SCM polling trigger. This groovy script will do that:

Hudson.instance.items.each { job ->
  if ( job.getTrigger(  hudson.triggers.SCMTrigger ) != null ) {
    println "will disable job ${job.name}"
    job.disable()
  }
}

Re-enabling the jobs will be left as an exercise : )

like image 84
Alex O Avatar answered Oct 07 '22 03:10

Alex O


Jenkins 2.204.1 + version of using the groovy script console to disable all jobs with an SCM trigger:

Jenkins.instance.getAllItems(Job.class).each{ job -> 
    if ( job.getSCMTrigger() != null ) {
        println "will disable job ${job.name}"
        job.setDisabled(true)
      }
}
like image 31
Will Brode Avatar answered Oct 07 '22 02:10

Will Brode