Skip to content

Instantly share code, notes, and snippets.

@chenchix
Forked from steven-terrana/kill_all.groovy
Created September 20, 2022 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chenchix/7fafc5a452d7142ef3817bc7ae45f6d8 to your computer and use it in GitHub Desktop.
Save chenchix/7fafc5a452d7142ef3817bc7ae45f6d8 to your computer and use it in GitHub Desktop.
[Kill All Builds] kill all queued and running jobs #Jenkins
import java.util.ArrayList
import hudson.model.*;
// Remove everything which is currently queued
def q = Jenkins.instance.queue
for (queued in Jenkins.instance.queue.items) {
q.cancel(queued.task)
}
// stop all the currently running jobs
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