Skip to content

Instantly share code, notes, and snippets.

@or-shachar
Created December 25, 2017 12: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 or-shachar/1389336af59ab81272633b2cec05c54d to your computer and use it in GitHub Desktop.
Save or-shachar/1389336af59ab81272633b2cec05c54d to your computer and use it in GitHub Desktop.
import org.jenkinsci.plugins.workflow.job.WorkflowRun
import org.jenkinsci.plugins.workflow.support.steps.StageStepExecution
projectFullName = 'folder/job-name'
dryRun = false
if(!(projectFullName in String)) {
throw new Exception('PARAMETER ERROR: projectFullName must be a string.')
}
Jenkins.instance.getItemByFullName(projectFullName).builds.each { Run item ->
if(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}"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment