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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
chaimp
  • 16,897
  • 16
  • 53
  • 86
  • 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 '12 at 00: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 '12 at 00:23
  • how do i pass the parameter such as mvn command line arugments.. – Sam Oct 23 '12 at 00:25
  • You make parameters in your job, and then you should be able name them in the URL string – chaimp Oct 23 '12 at 01:46
  • You could always integrate with another tool, BuildMaster has a [Queue a Jenkins Build](http://inedo.com/den/buildmaster/jenkins) operation you can use as part of a deployment plan. – Karl Harnagy Feb 23 '17 at 23:00

10 Answers10

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mikel
  • 759
  • 5
  • 3
  • 1
    I think it should be a POST rather than a GET to submit a build – Laurent Bristiel Nov 13 '13 at 08: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] https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Build#ParameterizedBuild-Launchingabuildwithparameters – Elijah Lofgren Sep 08 '15 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.

Jacob Hacker
  • 1,501
  • 1
  • 13
  • 16
Apurv
  • 17,116
  • 8
  • 51
  • 67
  • 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 08 '14 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 '15 at 21:20
8

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

http://YOURHOST/jenkins/job/PROJECTNAME/build
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
chaimp
  • 16,897
  • 16
  • 53
  • 86
  • 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 '12 at 00:24
  • In this case, you do not even need to set a token (but it is recommended for security). – chaimp Oct 23 '12 at 00:25
  • 1
    Simple yes, if *[CSRF (cross site request forgery) Protection](https://wiki.jenkins.io/display/JENKINS/CSRF+Protection)* is off. Otherwise: not so simple. – Peter Mortensen Jul 23 '18 at 18:08
7

Use:

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

You can take a look at this documentation: Parameterized Build

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kazerm
  • 499
  • 1
  • 4
  • 11
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'
Samuel Garratt
  • 301
  • 3
  • 5
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.

Amr Lotfy
  • 2,937
  • 5
  • 36
  • 56
  • What do you mean by *"Set token at job configuration /build triggers/Trigger builds remotely"* (it seems incomprehensible)? – Peter Mortensen Jul 23 '18 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 '20 at 03: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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
vladisld
  • 528
  • 3
  • 10
3

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

bmargulies
  • 97,814
  • 39
  • 186
  • 310
  • Thanks for the help. I was looking specifically for this: http://YOURHOST/jenkins/job/PROJECTNAME/build – chaimp Dec 14 '11 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 '11 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"
stack_lech
  • 990
  • 2
  • 10
  • 28
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

Pengyy
  • 37,383
  • 15
  • 83
  • 73
  • 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 '20 at 03:52