Temporarily disable SCM polling on Jenkins Server in System Groovy Temporarily disable SCM polling on Jenkins Server in System Groovy jenkins jenkins

Temporarily disable SCM polling on Jenkins Server in System Groovy


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 : )


  1. If it is one or 2 builds that are configured to do SCM polling, you can go into the configuration of each build and uncheck the box. It is that simple :)

  2. If you are using jenkins job builder, it should be even easier to change the configuration of several jobs at a time.

  3. If you using slaves or even on master, SCM polling is dependent on JAVA ? remove JAVA from the machine temporarily from the location where it is configured in master jenkins ;) The polling will fail :P This is a stupid hack ;)

I hope that helps !! ?


Try following Groovy script to comment out SCM Polling:

// WARNING: Use on your own risk! Without any warranty!import hudson.triggers.SCMTriggerimport hudson.triggers.TriggerDescriptor// from: https://issues.jenkins-ci.org/browse/JENKINS-12785TriggerDescriptor SCM_TRIGGER_DESCRIPTOR = Hudson.instance.getDescriptorOrDie(SCMTrigger.class)assert SCM_TRIGGER_DESCRIPTOR != null;MAGIC = "#MAGIC# "// comment out SCM Triggerdef disable_scmpoll_trigger(trig){   if ( !trig.spec.startsWith(MAGIC) ){    return new SCMTrigger(MAGIC + trig.spec)  }  return null  }// enable commented out SCM Triggerdef enable_scmpoll_trigger(trig){   if ( trig.spec.startsWith(MAGIC) ){    return new SCMTrigger(trig.spec.substring(MAGIC.length()))  }  return null  }Hudson.instance.items.each {  job ->   //println("Checking job ${job.name} of type ${job.getClass().getName()} ...")  // from https://stackoverflow.com/a/39100687  def trig = job.getTrigger( hudson.triggers.SCMTrigger )  if ( trig == null ) return  println("Job ${job.name} has SCMTrigger: '${trig.spec}'")  SCMTrigger newTrig = disable_scmpoll_trigger(trig)  // SCMTrigger newTrig = enable_scmpoll_trigger(trig)  if (newTrig != null ){    newTrig.ignorePostCommitHooks = trig.ignorePostCommitHooks    newTrig.job = job    println("Updating SCMTrigger '${trig.spec}' -> '${newTrig.spec}' for job: ${job.name}")    job.removeTrigger(SCM_TRIGGER_DESCRIPTOR)    job.addTrigger(newTrig)    job.save()  }    }return ''

To enable SCM polling again just change these two lines

  //SCMTrigger newTrig = disable_scmpoll_trigger(trig)  SCMTrigger newTrig = enable_scmpoll_trigger(trig)

Tested on Jenkins ver. 2.121.3

Known limitations:

  • supports single line "Schedule" (spec property) only