Created
April 1, 2018 09:14
-
-
Save or-shachar/02c22527b54b5be46a7723c6a2b454eb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import com.cloudbees.hudson.plugins.folder.Folder | |
import org.jenkinsci.plugins.workflow.job.WorkflowRun | |
import org.jenkinsci.plugins.workflow.support.steps.StageStepExecution | |
dryRun = true | |
def printIfBuildIsRunning(job){ | |
def item = job.getLastBuild() | |
if(item!=null && item.isBuilding()) { | |
if(item in WorkflowRun) { | |
WorkflowRun run = (WorkflowRun) item | |
if(!dryRun) { | |
//hard kill | |
run.doKill() | |
//release pipeline concurrency locks | |
StageStepExecution.exit(run) | |
} | |
println "Killed ${run}" | |
} else if(item in FreeStyleBuild) { | |
FreeStyleBuild run = (FreeStyleBuild) item | |
if(!dryRun) { | |
run.executor.interrupt(Result.ABORTED) | |
} | |
println "Killed ${run}" | |
} else { | |
println "WARNING: Don't know how to handle ${item.class}" | |
} | |
} | |
} | |
def killZombies(folder){ | |
def a = folder.getItems() | |
a.each{ | |
if (it instanceof Folder) | |
killZombies(it) | |
else | |
printIfBuildIsRunning(it) | |
} | |
} | |
killZombies(Jenkins.instance) | |
true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment