Skip to content

Instantly share code, notes, and snippets.

@phaneesh
Last active March 10, 2020 05:38
Show Gist options
  • Save phaneesh/d7dbb0854e7a70a517ad29068cf8e9dd to your computer and use it in GitHub Desktop.
Save phaneesh/d7dbb0854e7a70a517ad29068cf8e9dd to your computer and use it in GitHub Desktop.
Cleanup Old Jenkins builds
import jenkins.model.Jenkins
import hudson.model.Job
MAX_BUILDS = 3
for (job in Jenkins.instance.items) {
if(job.name.toLowerCase().endsWith("develop") || job.name.toLowerCase().endsWith("stage")) {
println "Deleting old builds: " +job.name
def recent = job.builds.limit(MAX_BUILDS)
for (build in job.builds) {
if (!recent.contains(build)) {
build.delete()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment