Skip to content

Instantly share code, notes, and snippets.

@marcelaraujo
Forked from steven-terrana/kill_all.groovy
Last active October 21, 2020 14:30
Show Gist options
  • Save marcelaraujo/82e4ef3db15937116ad299e3b4f3a48c to your computer and use it in GitHub Desktop.
Save marcelaraujo/82e4ef3db15937116ad299e3b4f3a48c to your computer and use it in GitHub Desktop.

Revisions

  1. marcelaraujo revised this gist Oct 21, 2020. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions kill_all.groovy
    Original file line number Diff line number Diff line change
    @@ -23,10 +23,11 @@ def stopJobs(job) {
    }
    } else if (job in org.jenkinsci.plugins.workflow.job.WorkflowJob) {

    if (job.isBuilding()) {
    //if (job.isBuilding()) {
    for (build in job.builds) {
    build.doKill()
    //build.doKill()
    build.finish(hudson.model.Result.ABORTED, new java.io.IOException("Aborted from Script Console"));
    }
    }
    //}
    }
    }
  2. @steven-terrana steven-terrana created this gist Sep 10, 2019.
    32 changes: 32 additions & 0 deletions kill_all.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    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()
    }
    }
    }
    }