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

6k Views Asked by At

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.

2

There are 2 best solutions below

0
Jon S On BEST ANSWER

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.

0
Goutham Nithyananda On

I modified the post by @Jon, to suit to hudson thanks @jon.

below is the script for hudson.

// Set how old the jobs to list should be (in days)
def numDaysBack = 30
def cutOfDate = System.currentTimeMillis() - 1000L * 60 * 60 * 24 * numDaysBack

//Initiallize it to zero
def oldJobsNumber = 0
def size = hudson.model.Hudson.instance.getItems().size()
println "Total Number of Jobs on hudson :" + size


for (i=0;i<size;i++){
def  allJob= hudson.model.Hudson.getInstance().getItems().get(i).getAllJobs()

 def job =new ArrayList(allJob).get(0)
 if (job != null && job .getLastBuild() != null && job.getLastBuild().getTimeInMillis() < cutOfDate) {
    println job.getFullName()
  oldJobsNumber++
  }
}
println oldJobsNumber