9

I am struggling to figure out some sample example to trigger hudson.model.Job from plugin:

private void triggerPipelineJobs(){

    for (Job<?,?> job : Jenkins.getInstance().getAllItems(Job.class)) {
        System.out.println("job is : " + job.getName());
        //how to trigger this jenkins pipeline job
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
mogli
  • 1,549
  • 4
  • 29
  • 57

1 Answers1

2

To run all Jenkins jobs (including pipelines), I use the following:

import hudson.model.*;

// get all jobs   
jobs = Hudson.instance.getAllItems(Job.class);

// iterate through the jobs
for (j in jobs) {
  // first check, if job is buildable
  if (j instanceof BuildableItem) {
     // run that job
     j.scheduleBuild();
  }
}

I think the part you are looking for is the scheduleBuild() method which you could call on your job variable in your for loop.

lax1089
  • 3,403
  • 3
  • 17
  • 37