2
votes

I am using the github-organization plugin to manage jenkins jobs from github but I discovered that Jenkins API does not report these builds.

In fact the API list the entire organization as a single job.

How can I build a specific repository and branch using the API?

To be clear, I am looking for some groovy code to add inside the Jenkinsfile

#!groovy

stage 'test-downstream'
node {
    def job = build job: 'some-job'
}

Now, the problem is that Jenkins is seeing the entire organization as a single job!

If I use Jenkins API to retrieve the jobs, it will return only the organization, and not all the repositories and jobs inside it.

I suspect that's because the way this plugin was implemented and I suppose that I need to give some extra parameters in order to specify which repository and branch I want to build inside the organization.... building an organization does not make much sense.

2
Can you clarify, are you using this github-organization-folder-plugin and want to manually trigger one of those jobs created by a Jenkinsfile? Also, have you tried if it works to trigger it via url with the jobname from an already created job? Then youll just need to find out about the job-naming convention with that Jenkinsfile Pipeline stuff. - Dominik Gebhart
Sadly I tried using acme/job/foo/job/master as the job name but got No parameterized job named acme/job/foo/job/master found which tells me that probably it does not expose these as real jobs. - sorin
Check what is exposed on http://<jenkinsserver>:<its port>/api/xml - Dominik Gebhart
But yeah, its probably similar to how matrix-jobs work. One job and all combinations are just builds. - Dominik Gebhart

2 Answers

4
votes

The question is vague, but I am guessing “API” in this context means the REST API to trigger builds. You can use for example

curl -X POST -u user:apitoken http://jenkins/job/yourorg/job/yourrepo/job/master/build
0
votes

The following code trigger job via System Groovy build step. Please note that system groovy always run on master so passing info from previous build steps might be tricky.

import jenkins.model.*
import hudson.model.*
import java.util.concurrent.*

def run_job(job_name) {
  def currentBuild = Thread.currentThread().executable
  def jenkins = jenkins.model.Jenkins.getInstance();

  def job = jenkins.getItemByFullName(job_name);
  if (job == null)
      throw new hudson.AbortException("Cannot find job:" + job_name);

  def params =[
    new StringParameterValue('PARAMETER1', "invoke 1 param1"), 
    new StringParameterValue('PARAMETER2', ",invoke 1 param2")
  ]

  def paramsAction = new ParametersAction(params) 
  def cause = new hudson.model.Cause.UpstreamCause(currentBuild)
  def causeAction = new hudson.model.CauseAction(cause)     

  def future_build = job.scheduleBuild2(0,causeAction,paramsAction);
  def running_build = future_build.waitForStart()
  return running_build
}

run_job("runner1")