Skip to content

Instantly share code, notes, and snippets.

@pmanvi
Last active March 31, 2020 05:20
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 pmanvi/b00e857afd1e9eedbb9757b75a7ecc06 to your computer and use it in GitHub Desktop.
Save pmanvi/b00e857afd1e9eedbb9757b75a7ecc06 to your computer and use it in GitHub Desktop.
// Lets keep only last 10 builds
MAX_BUILDS = 10
def jobs = Jenkins.instance.items;
println "Total Jobs" + jobs.size()
for (job in jobs) {
println "Job: " + job.name
try {
if(job instanceof jenkins.branch.MultiBranchProject) {
println "Multibranch"
job = job.getJob("master")
}
def recent = job.builds.limit(MAX_BUILDS)
println "Recent Builds: " + recent
println "============================="
for (build in job.builds) {
if (!recent.contains(build) && !build.isBuilding()) {
println "Cleaning up the build : " + build
build.delete()
println "Deleted successfully"
}
}
} catch(Exception ex) {
println "Neglecting the error"
ex.printStackTrace()
continue
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment