jenkinsjenkins-pluginshudsonhudson-api

Hudson/jenkins: Get list of jobs who's build is older than a month


I have a hudson instance running, Where i have 100's of jobs running everyday.

I want to get a list of jobs who's last successful job was x days old, so that i can disable such unwanted jobs.

Example: Some jobs are there on hudson which had last successful build a year back which is not needed anymore. I want a way to query and get a list of old jobs.


Solution

  • The following script will list all jobs which are older than 30 days (change the first variable for different number of days):

    // Set how old the jobs to list should be (in days)
    def numDaysBack = 30
    
    
    def cutOfDate = System.currentTimeMillis() - 1000L * 60 * 60 * 24 * numDaysBack
    
    for (job in Jenkins.instance.getAllItems(Job.class)) {
      build = job.getLastSuccessfulBuild()
      if (build != null && build.getTimeInMillis() < cutOfDate) {
        println job.getFullName()
      }
    }
    

    You execute it by going to Manage Jenkins -> Script console. This is for Jenkins, you might need to adapt it slightly for Hudson.