Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger jenkins hudson.model.Job programmatically from jenkins plugin

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
    }
}
like image 482
mogli Avatar asked Apr 30 '17 21:04

mogli


People also ask

How do I trigger a remote job in Jenkins?

Remote Job Name or full URL. The name or URL of the job on the remote Jenkins host which you would like to trigger. If the full job URL is specified the URL of the remote Jenkins host configured above will be ignored. The max concurrent connections to the remote host, default is 1, max is 5.

What is a job in Jenkins?

Key Takeaways: 1 A job is a runnable task that Jenkins controls to achieve a required objective. 2 Also, we can create a new job by clicking on “New Item” in the Jenkins dashboard. 3 We need to configure our jobs according to our requirements for making it fully runnable. More items...

How to trigger a Jenkins build on a different Jenkins master?

There is also a Parameterized Remote Trigger Plugin in case you want to trigger a build on a different/remote Jenkins Master. The parameters section can contain a combination of one or more of the following: Subversion revision: makes sure the triggered projects are built with the same revision (s) of the triggering build.

How to trust all certificates for a Jenkins job?

It is possible to override/rewrite the 'Trust all certificate'-setting for each Job separately. Setting this checkbox to 'true' will result in accepting all certificates for the given Job. If your remote Jenkins host has a self-signed certificate or its certificate is not trusted, you may want to enable this option.


1 Answers

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.

like image 73
lax1089 Avatar answered Oct 21 '22 12:10

lax1089