Goutham Nithyananda
Goutham Nithyananda

Reputation: 939

How to disable multiple Hudson/Jenkins job at once

I using groovy script to fetch all Hudson jobs older than 30 days. using below script. I also want to disable all the old jobs as part of this script, can someone suggest how to do this.

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

Upvotes: 0

Views: 1054

Answers (1)

Its not blank
Its not blank

Reputation: 3095

import com.cloudbees.hudson.plugins.folder.Folder

def  allJob= hudson.model.Hudson.getInstance().getItems()

for(int i=0; i<allJob.size(); i++){
    def job = allJob[i]
    if(job instanceof hudson.model.Project && job .getLastBuild() != null ){
        processJob(job)
    }else if(job instanceof Folder){
        processFolder(job)
    }
}

void processFolder(Item folder){
println "Processing Folder -"+folder.getFullName()
folder.getItems().each{
    if(it instanceof com.cloudbees.hudson.plugins.folder.AbstractFolder){
        processFolder(it)
    }else{
        processJob(it)
    }
}
}

void processJob(Item job){
   if(job instanceof hudson.model.Project && job .getLastBuild() != null ){
    println  job.getFullName() +" -- "+job.getLastBuild().getTimeInMillis()
// Now you can chek time here And if the condition is met then 
    // job.disable()
  }
}

The reason I have written the complete code here is because it was on my pending list since long. I am sure you could have figured it out if you had invested some more time on this.

Upvotes: 2

Related Questions