Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ryanwoodsmall/ced7ae1f9b523e6d2841572e01ffb1e6 to your computer and use it in GitHub Desktop.
Save ryanwoodsmall/ced7ae1f9b523e6d2841572e01ffb1e6 to your computer and use it in GitHub Desktop.
jenkins-abort-running-clearqueue.groovy
/*
* https://docs.cloudbees.com/docs/cloudbees-ci-kb/latest/client-and-managed-controllers/how-can-i-purge-or-clean-the-build-queue
*/
Jenkins.instance.queue.clear()
/*
* https://stackoverflow.com/questions/12305244/cancel-queued-builds-and-aborting-executing-builds-using-groovy-for-jenkins
*/
import java.util.ArrayList
import hudson.model.*;
import jenkins.model.Jenkins
// 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