Created
July 14, 2020 17:00
-
-
Save bsnux/5e68ba4adf387ac99efd9047dc0a4e7e to your computer and use it in GitHub Desktop.
Delete old Jenkins pipeline job builds
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 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