Code-your-life DevOps Jenkins

Maintaining Jenkins servers

June 8, 2018

Do you know, if all you Jenkins jobs, follow your company standard with regards to log rotation scheme? do you have control over all the credentials in Jenkins? Which Java SDK’s is exposed in your installation? Follow this article to make sure your Jenkins ninja skills is fully up to date when it comes to maintaining Jenkins servers.

/**
This Script loops through all your Jenkins jobs, and reports all the jobs, where the configuration of the logRotator,
is out of standard.

This print out each Job in the format ${jobName} DaysToKeep {daysToKeep} #builds ${#number of builds for the job}
and in the bottom a total count of wrongly configured jobs.
Parameters:
_________________
 daysToKeep > The number of days that you wish to keep logs and build info
 adaysToKeep < The number of days to keep artifacts
*/
daysToKeep = 14
aDaysToKeep = 14
counter = 0
jenkins.model.Jenkins.instance.getAllItems(hudson.model.Job).each {
    if(it.logRotator!=null){
        if ((it.logRotator.daysToKeep <; 0 || it.logRotator.daysToKeep < daysToKeep)
            && (it.logRotator.artifactDaysToKeep < 0 ||it.logRotator.artifactDaysToKeep > aDaysToKeep )){
            
            println ""+it.fullDisplayName+" \n\tDaysToKeep -&gt; "+it.logRotator.daysToKeep+" \n\t#Builds "+it.builds.size()
            counter++
            if (!it instanceof hudson.matrix.MatrixBuild ){
                // days to keep, num to keep, artifact days to keep, num to keep
                it.logRotator = new hudson.tasks.LogRotator ( 14, -1, 14, -1)
            }
        }
    }
}
 
//Rotates the newly configured log settings
Jenkins.instance.getAllItems(Job.class).findAll { it.logRotator }.each {
    it.logRotator.perform(it)
}
println "#jobs, that did not pass the test: " +counter

Read more of my post’s about maintaining Jenkins servers [@jenkins-maintenance]({{ site.baseurl }}

Improvement is taken as merge request at jesstruck@github/jenkinsMaintinance

Tagged: #Groovy #Jenkins #Jenkins-Maintenance