60

I am new to Jenkins, and I'm not sure if this is possible, but I would like to set up a web interface where somebody could click "Start Job" and this will tell Jenkins to start a particular build job.

Does Jenkins have a webservice that would allow such a thing? If so, what would be a simple example?

5
  • Did you able to resolve this??, i've to do the same thing, can you please let me know how you sorted this any sample code or links..
    – Sam
    Oct 23, 2012 at 0:00
  • 1
    Yes, it's actually straight-forward. Just like the selected answer says, you call a URL in the form JENKINS_URL/job/JOBNAME/build?token=TOKEN You set the token when setting up job.
    – chaimp
    Oct 23, 2012 at 0:23
  • how do i pass the parameter such as mvn command line arugments..
    – Sam
    Oct 23, 2012 at 0:25
  • You make parameters in your job, and then you should be able name them in the URL string
    – chaimp
    Oct 23, 2012 at 1:46
  • You could always integrate with another tool, BuildMaster has a Queue a Jenkins Build operation you can use as part of a deployment plan. Feb 23, 2017 at 23:00

10 Answers 10

66

Here is a link to the documentation: Jenkins Remote Access API.

Check out the Submitting jobs section.

In your job configuration you setup a token and then create a POST request to JENKINS_URL/job/JOBNAME/build?token=TOKEN. That's probably the most basic usage.

2
  • 1
    I think it should be a POST rather than a GET to submit a build Nov 13, 2013 at 8:45
  • 7
    This was hard to find (see [1]) but to set the TOKEN you need to check "Trigger builds remotely (e.g., from scripts)" under "Build Triggers" in your Jenkins Job. [1] wiki.jenkins-ci.org/display/JENKINS/… Sep 8, 2015 at 19:56
31

Jenkins has support for parameterized build as well.

So, if you want to pass parameters for configurable build generation, you can pass them by posting it while invoking Jenkins build request with http://YOURHOST/jenkins/job/PROJECTNAME/buildWithParameters.

2
  • 1
    This is exactly what I needed. The doc says to pass parameters you need to send JSON, but I kept getting an error saying it expected a form submission. Using buildWithParameters worked.
    – DSoa
    Apr 8, 2014 at 16:17
  • 3
    This is way overdue, but I was having the same problems, and it turned out that POST /build expects, as you say, content-type: x-www-form-urlencoded, and in the body there should be a single key named "json", with the actual json as the value. Trying to POST with content-type: application/json, and actual json in the body, will render a servlet exception. Jenkins seems to have its own way of dealing with JSON indeed...
    – JHH
    Jun 10, 2015 at 21:20
8

Aha, I found it in the documentation. So simple:

http://YOURHOST/jenkins/job/PROJECTNAME/build
3
  • In the link, replace "YOURHOST" with your hostname (i.e. localhost or www.mywebserver.com, etc.) and replace "PROJECTNAME" with the name of your project.
    – chaimp
    Oct 23, 2012 at 0:24
  • In this case, you do not even need to set a token (but it is recommended for security).
    – chaimp
    Oct 23, 2012 at 0:25
  • 1
    Simple yes, if CSRF (cross site request forgery) Protection is off. Otherwise: not so simple. Jul 23, 2018 at 18:08
7

I needed to add parameters and I wanted to do it over https. It took me a while but the following worked for me:

curl --request POST --url 'https://HOST_NAME/job/JOB_NAME/buildWithParameters?token=TOKEN'  --header 'cache-control: no-cache' --header 'content-type: application/x-www-form-urlencoded' --data 'name1=value1&name2=value2'
7

Use:

http://some server/job/myjob/buildWithParameters?token=TOKEN&PARAMETER=Value

You can take a look at this documentation: Parameterized Build

6
curl -H POST http://USERNAME:PASSWORD@JENKINS_HOST:PORT/job/JOB_NAME/build?token=YOUR_TOKEN

Set YOUR_TOKEN at job configuration -> build triggers -> Trigger builds remotely.

2
  • What do you mean by "Set token at job configuration /build triggers/Trigger builds remotely" (it seems incomprehensible)? Jul 23, 2018 at 18:05
  • Could the token use jenkins built-in variable like JOB_NAME in the web page? I tried env.JOB_NAME, ${JOB_NAME}, ${env.JOB_NAME}, all of them doesn't work.
    – shuiqiang
    Dec 18, 2020 at 3:56
4

There is a good sample of using the above API from Python. The project is called Python Jenkins, and you may find it here: link

3

Jenkins has a documented REST API. You can make your little web service invoke it.

2
  • Thanks for the help. I was looking specifically for this: YOURHOST/jenkins/job/PROJECTNAME/build
    – chaimp
    Dec 14, 2011 at 22:53
  • Found it in jenkins documentation, but I'll mark you're answer as correct, since it at least answers that it is possible.
    – chaimp
    Dec 14, 2011 at 22:54
2

With curl if you have multiple arguments to pass like a token and a parameter you might have to quote on Linux shell:

curl -H POST "http://USERNAME:PASSWORD@JENKINS_HOST:PORT/job/JOB_NAME/build?token=YOUR_TOKEN&PARAMETER=VALUE"
0

Install Generic Webhook Trigger plugin. Select generic webhook trigger in build trigger actions. Generate a random string and paste in token. Now your job can be triggered with a http request to the following url.

screenshot

http://JENKINS_URL/generic-webhook-trigger/invoke?token=TOKEN_VALUE

replace your jenkins url and token value

1
  • Could the token use jenkins built-in variable like JOB_NAME in the web page? I tried env.JOB_NAME, ${JOB_NAME}, ${env.JOB_NAME}, all of them doesn't work.
    – shuiqiang
    Dec 18, 2020 at 3:52

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.