jenkinshudsonhudson-api

How to programmatically build a jobs in a view in hudson


I have a 500+ Jenkins jobs in a particular view. I need to trigger those jobs programmatically using the Jenkins script console. I tried with the below code but it didn't work.

    import hudson.model.*;
    triggerbuild{
      def job = hudson.instance.getview("xxxx").listItem("Job")
      hudson.instance.build("job")
}

Can someone help me on this?


Solution

  • This is the bare minimal solution I can come up with, assuming you don't have any parameters to pass and you don't really care about the build cause, etc.

    def jen = Jenkins.instance;
    def viewName = 'TECHNICAL'
    
    def jobs = jen.getView(viewName).getJobNames()
    jobs.each { job -> 
       println "Running Job: " + job
       jen.getQueue().schedule(jen.getJob(job), 0, null, null)
    }
    

    Build on top of this as required. Probably you may have to identify different types of Jobs and execute them accordingly.