Created
April 7, 2017 00:24
-
-
Save justintoo/fd65f441896b4af657407e4515b1a3ee to your computer and use it in GitHub Desktop.
Cancel all builds currently pending in the queue and all builds currently running.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//----------------------------------------------------------------------------- | |
// TOO1 (04/06/17): Created initial script to cancel all running/queued builds | |
//----------------------------------------------------------------------------- | |
// | |
// Add this script into the Jenkins Build Step "System Groovy Script" | |
import jenkins.model.Jenkins | |
import hudson.model.AbstractProject | |
def thisBuild = build | |
println "//-----------------------------------------------------------------------------" | |
println "// (1) Canceling all builds pending in the build queue" | |
println "//-----------------------------------------------------------------------------" | |
println "" | |
def q = Jenkins.instance.queue | |
def numCanceledInQ = 0 | |
q.items.each { q.cancel(it.task); ++numCanceledInQ } | |
println "Canceled (" + numCanceledInQ + ") builds in the queue." | |
println "" | |
println "//-----------------------------------------------------------------------------" | |
println "// (2) Canceling all currently running builds" | |
println "//-----------------------------------------------------------------------------" | |
println "" | |
def numCanceledInBuild = 0 | |
Jenkins.instance.getAllItems(AbstractProject.class).each {job -> | |
for (build in job.builds) { | |
if (!build.isBuilding()) | |
// don't need to cancel builds that are not building | |
continue; | |
else if (build.getFullDisplayName().equals(thisBuild.getFullDisplayName())) | |
// don't cancel this job | |
continue; | |
else | |
println "Canceling " + build.getFullDisplayName(); | |
build.doStop(); | |
++numCanceledInBuild; | |
} | |
} | |
println "Canceled (" + numCanceledInBuild + ") builds that were currently running." | |
println "" | |
println "//-----------------------------------------------------------------------------" | |
return; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment