3

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.

sorin
  • 161,544
  • 178
  • 535
  • 806
  • 1
    Can you clarify, are you using this [github-organization-folder-plugin](https://github.com/jenkinsci/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](https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API) 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 Mar 30 '16 at 15:58
  • 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 Mar 31 '16 at 11:45
  • 1
    Check what is exposed on `http://:/api/xml` – Dominik Gebhart Mar 31 '16 at 11:58
  • But yeah, its probably similar to how matrix-jobs work. One job and all combinations are just builds. – Dominik Gebhart Apr 01 '16 at 03:09

2 Answers2

5

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
Jesse Glick
  • 24,539
  • 10
  • 90
  • 112
  • While this is not really ideal it may work if I am unable to find a groovy. Still I hate having to deal with the API token issue from here when the Jenkinsfile execution was supposed to already have access to Jenkins api. – sorin Mar 31 '16 at 11:41
  • Then perhaps you are asking something totally different, and this is simply a duplicate of: http://stackoverflow.com/questions/36306883/how-can-i-trigger-another-job-from-a-jenkins-pipeline-jenkinsfile – Jesse Glick Mar 31 '16 at 18:32
1

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")
Amir Hadar
  • 11
  • 2