Last active
June 3, 2023 17:24
-
-
Save realityforge/c57eb3d1854320d14252ac881fc6cedf to your computer and use it in GitHub Desktop.
Kill all queued jenkins jobs
This file contains hidden or 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
MAX_BUILDS = 5 | |
MAX_ENV_BUILDS = 2 | |
Jenkins.instance.getAllItems(org.jenkinsci.plugins.workflow.job.WorkflowJob.class).each { it -> | |
def job = Jenkins.instance.getItemByFullName(it.fullName, Job.class) | |
def limit = (it.fullName =~ /^environment/) ? MAX_ENV_BUILDS : MAX_BUILDS | |
def recent = job.getBuilds().limit(limit) | |
println "Processing job " + it.fullName + " " + it.class + " BuildCount: " + job.getBuilds().size() + " Limit: " + limit | |
for (build in job.getBuilds()) { | |
//println "Considering: " + build | |
if (!recent.contains(build)) { | |
println "Deleting build from job " + it | |
// Delete build | |
build.delete() | |
} | |
} | |
}; | |
jenkins.model.Jenkins.instance.getAllItems(hudson.model.AbstractProject).each { job -> | |
if (job.building) { | |
println "Skipping job $job.name, currently building" | |
} else { | |
println "Wiping out workspace of $job.name" | |
job.doDoWipeOutWorkspace() | |
} | |
} | |
print "Done!" |
This file contains hidden or 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
import hudson.model.* | |
def q = Jenkins.instance.queue | |
q.items.each { | |
if (it =~ /deploy-to/) { | |
q.cancel(it.task) | |
} | |
} |
This file contains hidden or 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
import hudson.model.* | |
def q = Jenkins.instance.queue | |
q.items.each { q.cancel(it.task) } |
why not just
import hudson.model.*
def queue = Hudson.instance.queue
println "Queue contains ${queue.items.length} items"
queue.clear()
println "Queue cleared"
src: https://github.com/jenkinsci/jenkins-scripts/blob/master/scriptler/clearBuildQueue.groovy
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Kill all 'queued deploy' jobs: