
Trigger Jenkins Job Remotely using Jenkins API
Jenkins is the master of flexibility. It supports a wide range of configurations through its plugins. There are various triggering options supported by Jenkins job such as Build periodically, GitHub hook trigger, trigger build remotely, etc.
In this story, we are going to learn how to trigger Jenkins job remotely using Jenkins API.
Why do we need to trigger builds remotely?
Trigger builds Remotely feature provides flexibility that allows triggering a job from the script, command line, or GitHub hook when somebody has committed a change or triggers a job from one server to another
For Example, Trigger an integration tests job when a developer creates a Pull Request to make sure the new changes are not breaking the existing functionality
Let us Get Started!
There are three main steps required to configure.
Create an authentication token.
Configure a job to trigger from a remote.
Trigger the job from a remote resource.
Create an Authentication Token
The authentication token is nothing but token-based credentials that grant authorization to trigger the job. The token can be generated by Jenkins user. Generating an authentication token is simple.
- Click on the username drop-down icon from the top right corner of the Jenkins server page and click on the configuration options.
Tips: Enable Security options from Manage Jenkins -> Configure global security if the user login feature is not enabled in your Jenkins server.
Find the
Add new Tokenbutton and click on it.Add the token name and click on
Generate.Copy the token name (We will configure this token in the job).
Configure a job to trigger from remote
Create a new FreeStyle job or use the existing one, Go to the configuration -> Build Triggers sections and check the "Trigger builds remotely(e.g., from scripts)" option and type the token name in the Authentication Token text box.
Trigger the job from a remote resource
All the settings have been done successfully, Now its time to trigger the job using CURL, and WGET commands. You can also use Postman or GitHub webhook to trigger the job.
Replace the api_token with the real token string in the below command.
// Run when the anonymous user has the build permission
curl http://127.0.0.1:8080/job/Trigger_Remote_Demo/build?token=My-token
//Send a request with credentials
curl -u username:api_token http://127.0.0.1:8080/job/Trigger_Remote_Demo/build?token=My-token
// Build with Parameter
curl -u username:api_token http://127.0.0.1:8080/job/Trigger_Remote_Demo/buildWithParameters?token=My-token¶1=val1¶2=val2
Trigger the Job using the WGET command
wget http://127.0.0.1:8080/job/Trigger_Remote_Demo/build?token=My-token
wget --auth-no-challenge --http-user=username --http-password=api_token http://127.0.0.1:8080/job/Trigger_Remote_Demo/build?token=My-token
Troubleshoot
The username is not displayed in the top right corner and also no option for login
Solution:
Enable Securityoptions from Manage Jenkins -> Configure global security if the user login feature is not enabled in your Jenkins server
No valid crumb was included in the request
Solution:
Disable the
Prevent Cross-Site Request Forgery exploitsoption from Configure Global Security --> CSRF ProtectionAdd the crumb in the request header
Get the Crum (HTTP://127.0.0.1:8080/crumbIssuer/api/json)
curl -H "Jenkins-Crumb:64857********" -u admin:11d******** http://127.0.0.1:8080/job/Trigger_Remote_Demo/build?token=My-token