13
votes

I am working with a Jenkins build server to run synthesis/simulation for FPGAs. Right now I have nightly builds and can start the build manually in Jenkins browser interface.

My question is:

Is there any possibility to start a job build with a batch script without using browser interface?

(I am running Jenkins on Windows 7 64bit.)

6

6 Answers

33
votes

Here is an example with a curl command (for a job with parameters):

curl -X POST -u YOUR_USER:YOUR_USER_PASSWORD http://YOUR_JENKINS_URL/job/YOUR_JOB/buildWithParameters?PARAM1=value1&PARAM2=value

And a job without parameters:

curl -X POST -u YOUR_USER:YOUR_USER_PASSWORD http://YOUR_JENKINS_URL/job/YOUR_JOB/build

If you don't want to use your user/password, you can generate an API token for your Jenkins user:

enter image description here

And use this token in your curl command:

curl -X POST http://YOUR_JENKINS_URL/job/YOUR_JOB/build?TOKEN=YOUR_API_TOKEN
14
votes

You can trigger a Jenkins job with a configured token instead of your username/password, which would allow you to share a trigger script without exposing your own credentials.

  1. Go to your job's configuration.
  2. Scroll down to Build Triggers, and check the box for Trigger build remotely (e.g., from scripts), and enter an authentication token (e.g., "MY_TOKEN").

enter image description here

  1. Copy one of the URLs below the Authentication Token field based on whether your build has parameters.

Then use that URL in a curl command to trigger a build. For example:

curl -I https://${JENKINS_URL}/job/tmp/job/dummy-test/build?token=MY_TOKEN

The -I parameter tells curl to print the head of the response, which you could use to determine the result status. Jenkins replies with HTTP 201 if successful:

$ curl -I https://<JENKINS_URL>/job/tmp/job/dummy-test/build\?token\=MY_TOKEN
HTTP/1.1 201 Created
Cache-Control: public
Content-Length: 0
Date: Mon, 11 Apr 2016 12:47:26 GMT
Location: https://<JENKINS_URL>/queue/item/1707/
Pragma: public
Server: Apache-Coyote/1.1
X-Content-Type-Options: nosniff
Connection: keep-alive
2
votes

As I tried to trigger my job via curl I ended up always getting "Not authorized" errors.

Later I found out that this was because I completely disabled anonymous access on the server. The solution was to install the following plugin: https://wiki.jenkins-ci.org/display/JENKINS/Build+Token+Root+Plugin

Source: https://issues.jenkins-ci.org/browse/JENKINS-17764

1
votes

you can do this using curl command with -I Option. create an API token for the jenkins Job and use it to trigger the job. you can use jenkins user password for this as well.

enter image description here

command would be

curl -I -u auto:<user_api_token> http://<jenkins_Server>/job/test/build?token=wefiytgwiefiweihfqweiodf

results would be enter image description here

for more information https://serverfault.com/questions/888176/how-to-trigger-jenkins-job-via-curl-command-remotely/888248#888248

0
votes

In the new Jenkins Pipeline, under Build Triggers, select the checkbox Trigger builds remotely (e.g., from scripts). Then give Jenkins a token that will be required when triggering the build.

Not authorized errors

A problem with triggering the builds remotely is, if you've set up Jenkins right and disabled anonymous user access, you will get Not authorized errors when you try to trigger the build from a script (as @keocra pointed out). You now have two options:

  1. Pass a username and password along when you trigger the build. This means that your script will need to include the username and password, which means everyone who can read your script will have the username and password, which is almost as bad as anonymous access.
  2. Use the Build Token Root Plugin. This plugin allows you to use the Trigger builds remotely feature without requiring the username and password. All you need is the token you generated before.

Triggering the build

To trigger the build remotely, run

curl JENKINS_URL/buildByToken/build?job=JobFoo&token=MyToken

Where JENKINS_URL is the URL to your Jenkins instance, JobFoo is the name of your job, and MyToken is the token you entered under Trigger bulids remotely.

Of course, you don't need to use curl; you can also use wget or any other program that can make HTTP requests.

0
votes

I've googled across many addresses and the working result can be found here:

#!/bin/bash
TOKEN='jenkins-user-token'
USER='my-username'
SERVER="http://your.server.address"

#jenkins job parameters
PARAMF=$1
SECONDPARAM=$2

# retrieve the crumb that we need to pass in the header
CRUMBS=$(curl -s -X GET -u $USER:$TOKEN ${SERVER}/crumbIssuer/api/json  | jq -c '. | .crumb ')
curl --user $USER:$TOKEN  -H "Jenkins-Crumb:${CRUMBS}" -X POST  "${SERVER}/view/MyView/job/JobName/buildWithParameters?TOKEN=${TOKEN}&PARAMETERONE=${PARAMF}&PARAMETERTWO=${SECONDPARAM}"

The steps script does:

  1. get the breadcrumb
  2. call jenkins to execute a job with multiple parameters

you can save this script as jenkins-job-cli.sh and call it

chmod +x jenkins-job-cli.sh
./jenkins-job-cli.sh first-parameter second-parameter

Hope this help.

Cheers,

Leslie