Skip to content

Instantly share code, notes, and snippets.

@webguywalker
Last active August 6, 2020 22:41
Show Gist options
  • Save webguywalker/3c7ec10b42b593f3facfb039c007d489 to your computer and use it in GitHub Desktop.
Save webguywalker/3c7ec10b42b593f3facfb039c007d489 to your computer and use it in GitHub Desktop.
Jenkins Optimization: limit all builds
// Jenkins: Limit the number of builds per job
// reference: https://support.cloudbees.com/hc/en-us/articles/215549798-Best-Strategy-for-Disk-Space-Management-Clean-Up-Old-Builds
// Jenkins Console: https://{jenkins_host}/script
def daysToKeep = -1
def numToKeep = 7
def artifactDaysToKeep = -1
def artifactNumToKeep = 7
Jenkins.instance.getAllItems(AbstractItem.class).each {
println("=====================")
println("JOB: " + it.name)
println("Job type: " + it.getClass())
if (it instanceof org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) {
try {
orpStrategy = new com.cloudbees.hudson.plugins.folder.computed.DefaultOrphanedItemStrategy(true, daysToKeep, numToKeep)
it.setOrphanedItemStrategy(orpStrategy)
it.save()
println("[SUCCESS - wmbp]")
} catch(Exception e) {
println "[WARNING - wmbp] Failed to update: ${e}"
}
}
if (it instanceof org.jenkinsci.plugins.workflow.job.WorkflowJob) {
try {
it.buildDiscarder = new hudson.tasks.LogRotator(daysToKeep,numToKeep, artifactDaysToKeep, artifactNumToKeep)
it.save()
println("[SUCCESS - wj]")
} catch(Exception e) {
println "[WARNING - wj] Failed to update: ${e}"
}
}
if (it instanceof hudson.model.FreeStyleProject) {
try {
it.buildDiscarder = new hudson.tasks.LogRotator(daysToKeep,numToKeep, artifactDaysToKeep, artifactNumToKeep)
it.save()
println("[SUCCESS - fp]")
} catch(Exception e) {
println "[WARNING - fp] Failed to update: ${e}"
}
}
}
return;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment