Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save GarimaDamani/70291b0431e2b9a4b89d3817ad577cce to your computer and use it in GitHub Desktop.
Save GarimaDamani/70291b0431e2b9a4b89d3817ad577cce to your computer and use it in GitHub Desktop.
Save space in Jenkins slave by setting BuildDiscarderProperty
import hudson.model.Job
import jenkins.model.Jenkins
import hudson.tasks.LogRotator
import jenkins.model.BuildDiscarderProperty
name = "test_master_builder_medium_blog"
job_daystokeep = -1
job_numtokeep = 20
artifact_numtokeep = 20
artifact_daystokeep = -1
Jenkins.instance.allItems(Job).each { job ->
try {
// Comment the below if statement to run for all the jobs.
if (job.fullDisplayName == name) {
// After removing above if all the jobs config will be updated.
if (job.isBuildable() && job.getProperty(BuildDiscarderProperty) && job.supportsLogRotator()) {
def log = new LogRotator(int daysToKeep = job_daystokeep, int numToKeep = job_numtokeep, int artifactDaysToKeep = artifact_daystokeep, int artifactNumToKeep = artifact_numtokeep)
def builderdiscard = new BuildDiscarderProperty(log)
job.addProperty(builderdiscard)
println "Updated ${job.fullDisplayName}"
}
else {
println "Error : unable to update ${job.fullDisplayName}"
}
}
}
catch (Exception e) {
println ' Ignoring exception ' + e
}
}
return;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment