Last active
October 31, 2024 08:21
-
-
Save sasjo/6c0159d2a438f256b1127d1ef69b522d to your computer and use it in GitHub Desktop.
Drain Jenkins build queue and stop all running jobs
This file contains 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
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 |
Thx a lot!
I tried like hundred combination for job stopping on mutli-branch. All failed but this.
Thank you!
Focus on one thing and your definitely gonna get it,that's just the logic people don't understand
I found that Git misbehaves, and I had to traverse the first block
Thank you
Thnx!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks 🙏