Skip to content

Instantly share code, notes, and snippets.

@or-shachar
Created April 1, 2018 09:14
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 or-shachar/02c22527b54b5be46a7723c6a2b454eb to your computer and use it in GitHub Desktop.
Save or-shachar/02c22527b54b5be46a7723c6a2b454eb to your computer and use it in GitHub Desktop.
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