Skip to content

Instantly share code, notes, and snippets.

@dee-kryvenko
Last active April 12, 2022 06:00
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 dee-kryvenko/51fdafaeff886e9dac5386bff555246c to your computer and use it in GitHub Desktop.
Save dee-kryvenko/51fdafaeff886e9dac5386bff555246c to your computer and use it in GitHub Desktop.
Jenkins find and abort builds stuck without executor
import com.cloudbees.hudson.plugins.folder.AbstractFolder
import org.jenkinsci.plugins.workflow.job.WorkflowJob
import hudson.model.FreeStyleProject
import org.jenkinsci.plugins.workflow.steps.FlowInterruptedException
import hudson.model.Result
projects = []
def getProjects(def items) {
items.each {
if (it instanceof AbstractFolder) {
getProjects(it.getItems())
} else if (it instanceof WorkflowJob || it instanceof FreeStyleProject) {
projects << it
} else {
throw new RuntimeException("Unrecognized job type: ${it.class}")
}
}
}
getProjects(Jenkins.instance.getAllItems())
projects.each {
it.getBuilds().each {
if (it.isBuilding() && (it.parent.isDisabled() || it.getExecutor() == null)) {
println it.getFullDisplayName() + " : " + it.class
it.doTerm()
it.doStop()
it.doKill()
FlowInterruptedException suddenDeath = new FlowInterruptedException(Result.ABORTED)
it.finish(Result.ABORTED, suddenDeath)
it.getSettableExecutionPromise().setException(suddenDeath)
}
}
}
println projects.size()
return "done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment