Start Jenkins build using batch script
Solution 1
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:
And use this token in your curl command:
curl -X POST http://YOUR_JENKINS_URL/job/YOUR_JOB/build?TOKEN=YOUR_API_TOKEN
Solution 2
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
Solution 3
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:
- 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.
- 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.
Solution 4
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.
command would be
curl -I -u auto:<user_api_token> http://<jenkins_Server>/job/test/build?token=wefiytgwiefiweihfqweiodf
for more information https://serverfault.com/questions/888176/how-to-trigger-jenkins-job-via-curl-command-remotely/888248#888248
michi.b
"A computer is like air conditioning – it becomes useless when you open Windows"
Updated on May 21, 2020Comments
-
michi.b almost 4 years
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.)
-
Bruno Lavit about 8 yearsAs proposed by @tony19 below, you can also generate an API token for your user (if you don't want to use the user/password). I've updated my answer. Happy to know it solved your problem :)
-
michi.b about 8 yearsIs it possible to pass SVN Url's with parameters, too? It would be nice to tell Jenkins via command line to run a specific job (in my case synthesis for FPGAs) and checkout all projects from SVN. My Python script in Jenkins is able to run every checkout in workspace folder for FPGA synthesis, but I want to keep it simple for my users to start the jobs with a single batch and their specific checkouts.
-
Bruno Lavit about 8 yearsIf you have one parameter per SVN url, you can pass them using the command line (maybe protected with double quotes).
-
michi.b about 8 yearsThis could be an opportunity, thank you! Do you know any other plugins or commands to pass them to Jenkins? Is passing parameters a good way to solve this?
-
Bruno Lavit about 8 yearsI've created a room to discuss of that if you want: chat.stackoverflow.com/rooms/108894/discussion-with-michi-b
-
michi.b about 8 yearsI'd like to discuss, but I need 20 reputation to talk in chats... :/
-
Bruno Lavit about 8 yearsHo sorry :( If I find another solution for your SVN url(s), I'll post a message here.
-
rush about 3 years
crumbIssuer/api
doesn't work for jenkins 2.176+ due to CSRF security improvements. More details available here: jenkins.io/doc/upgrade-guide/2.176/#SECURITY-626