Skip to content

Instantly share code, notes, and snippets.

@sasjo
Last active October 31, 2024 08:21
Show Gist options
  • Save sasjo/6c0159d2a438f256b1127d1ef69b522d to your computer and use it in GitHub Desktop.
Save sasjo/6c0159d2a438f256b1127d1ef69b522d to your computer and use it in GitHub Desktop.
Drain Jenkins build queue and stop all running jobs
Jenkins.instance.queue.items.findAll { !it.task.name.contains("Extenda") }.each {
println "Cancel ${it.task.name}"
Jenkins.instance.queue.cancel(it.task)
}
Jenkins.instance.items.each {
stopJobs(it)
}
def stopJobs(job) {
if (job in jenkins.branch.OrganizationFolder) {
// Git behaves well so no need to traverse it.
return
} else if (job in com.cloudbees.hudson.plugins.folder.Folder) {
job.items.each { stopJobs(it) }
} else if (job in org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) {
job.items.each { stopJobs(it) }
} else if (job in org.jenkinsci.plugins.workflow.job.WorkflowJob) {
if (job.isBuilding() || job.isInQueue() || job.isBuildBlocked()) {
job.builds.findAll { it.inProgress || it.building }.each { build ->
println "Kill $build"
build.finish(hudson.model.Result.ABORTED, new java.io.IOException("Aborted from Script Console"));
}
}
}
}
return true
@avdhoot
Copy link

avdhoot commented Mar 23, 2021

Thanks 🙏

@nikolagrbovic13
Copy link

Thx a lot!
I tried like hundred combination for job stopping on mutli-branch. All failed but this.

@ragazzid
Copy link

Thank you!

@KingZaddy
Copy link

Focus on one thing and your definitely gonna get it,that's just the logic people don't understand

@david-shepard
Copy link

I found that Git misbehaves, and I had to traverse the first block

@liucx1201
Copy link

Thank you

@javanaut-de
Copy link

Thnx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment