Skip to content

Instantly share code, notes, and snippets.

@scoheb
Last active March 28, 2017 12:09
Show Gist options
  • Save scoheb/4319f5dd5d5f5df5197ad7951344615a to your computer and use it in GitHub Desktop.
Save scoheb/4319f5dd5d5f5df5197ad7951344615a to your computer and use it in GitHub Desktop.
Disable ALL Jenkins jobs ONE TIME ONLY
import hudson.model.*
import jenkins.model.*
/** This script attempts to disable ALL jobs ONLY ONCE
* It uses JENKINS_HOME/disableAllJobs.done as the control file
*/
File controlFile = new File(Jenkins.instance.getRootDir(), "disableAllJobs.done")
if (controlFile.exists()) {
println("Disable All Jobs ALREADY completed: " + controlFile.getAbsolutePath() + " exists...")
} else {
disableChildren(Jenkins.instance.items, controlFile)
}
def disableChildren(items, controlFile) {
for (item in items) {
if (item.class.canonicalName == 'com.cloudbees.hudson.plugins.folder.Folder') {
disableChildren(((com.cloudbees.hudson.plugins.folder.Folder) item).getItems(), controlFile)
} else if ((item.class.name != 'org.jenkinsci.plugins.workflow.job.WorkflowJob') &&
(item.class.name != 'hudson.model.ExternalJob') &&
(item.class.name != 'org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject')) {
if (!item.disabled) {
item.disabled=true
item.save()
println("Disabled " + item.name)
}
}
}
hudson.Util.touch(controlFile)
println("Disable All Jobs completed.")
if (controlFile.exists()) {
println(controlFile.getAbsolutePath() + " created")
} else {
println("WARNING: could not create control file " + controlFile.getAbsolutePath() + "!!")
println("WARNING: remove init.groovy from " + Jenkins.instance.getRootDir().getAbsolutePath() + " to disable")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment