Start Jenkins build using batch script

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

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


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


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