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
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
errorsA 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:
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.
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
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:
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
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
results would be
for more information https://serverfault.com/questions/888176/how-to-trigger-jenkins-job-via-curl-command-remotely/888248#888248
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
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.
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