Created
March 9, 2022 06:52
-
-
Save bsnux/403626a87d589ceb565338ca4003a0da to your computer and use it in GitHub Desktop.
Deletes old builds leaving only the ones given by MAX_TO_KEEP variable
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 MAX_TO_KEEP = 10 | |
def JOB_NAME = "job_name_here" | |
def hi = hudson.model.Hudson.instance | |
def item = hi.getItemByFullName(JOB_NAME) | |
def jobs = item.getAllJobs() | |
Iterator<?> iterator = jobs.iterator() | |
while (iterator.hasNext()) { | |
def job = iterator.next() | |
def recent = job.builds.limit(MAX_TO_KEEP) | |
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