3

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.

Goutham Nithyananda
  • 871
  • 2
  • 13
  • 25

2 Answers2

12

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.

Jon S
  • 15,846
  • 4
  • 44
  • 45
2

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
Goutham Nithyananda
  • 871
  • 2
  • 13
  • 25