Skip to content

Instantly share code, notes, and snippets.

@bsnux
Created July 14, 2020 17:00
Show Gist options
  • Save bsnux/5e68ba4adf387ac99efd9047dc0a4e7e to your computer and use it in GitHub Desktop.
Save bsnux/5e68ba4adf387ac99efd9047dc0a4e7e to your computer and use it in GitHub Desktop.
Delete old Jenkins pipeline job builds
import java.util.ArrayList
def MULTIBRANCH_JOBNAME = "project"
def BRANCH_NAME = "develop"
def MAX_TO_KEEP = 10
def hi = hudson.model.Hudson.instance
def item = hi.getItemByFullName(MULTIBRANCH_JOBNAME)
def jobs = item.getAllJobs()
Iterator<?> iterator = jobs.iterator()
while (iterator.hasNext()) {
def job = iterator.next()
def recent = job.builds.limit(MAX_TO_KEEP)
if (job.name != BRANCH_NAME) {
continue
}
for (build in job.builds) {
if (!recent.contains(build)) {
println "Preparing to delete: " + build
build.delete()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment