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.

4
  • 1
    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. Commented Mar 30, 2016 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. Commented Mar 31, 2016 at 11:45
  • 1
    Check what is exposed on http://<jenkinsserver>:<its port>/api/xml Commented Mar 31, 2016 at 11:58
  • But yeah, its probably similar to how matrix-jobs work. One job and all combinations are just builds. Commented Apr 1, 2016 at 3:09

2 Answers 2

6

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
Sign up to request clarification or add additional context in comments.

2 Comments

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.
Then perhaps you are asking something totally different, and this is simply a duplicate of: stackoverflow.com/questions/36306883/…
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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.