Skip to content

Instantly share code, notes, and snippets.

@actionjack
Forked from brandonfl/killAllBuilds.groovy
Created March 16, 2022 15:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save actionjack/111b0f3a3438dadd2caf0fbc6b08bc28 to your computer and use it in GitHub Desktop.
Save actionjack/111b0f3a3438dadd2caf0fbc6b08bc28 to your computer and use it in GitHub Desktop.
Kill all build and remove queued ones
import java.util.ArrayList;
import hudson.model.*;
def q = Jenkins.instance.queue
for (queued in Jenkins.instance.queue.items) {
q.cancel(queued.task)
}
for (job in Jenkins.instance.items) {
stopJobs(job)
}
def stopJobs(job) {
if (job in com.cloudbees.hudson.plugins.folder.Folder) {
for (child in job.items) {
stopJobs(child)
}
} else if (job in org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) {
for (child in job.items) {
stopJobs(child)
}
} else if (job in org.jenkinsci.plugins.workflow.job.WorkflowJob) {
if (job.isBuilding()) {
for (build in job.builds) {
build.doKill()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment