Jenkins is one of the most popular tools in DevOps, enabling Continuous Integration and Continuous Deployment (CI/CD) pipelines. In this blog post, we’ll explore how to trigger a Jenkins job remotely using the cURL command. By automating job execution through an API call, teams can seamlessly integrate Jenkins jobs with other tools, scripts, or workflows.
If you’re looking to automate Jenkins job triggers with cURL, you’re in the right place!
You can use “sh” command in the Jenkins declarative Pipeline Stage and get the jobs triggered from that stage
Why Use cURL to Trigger Jenkins Jobs?
Triggering Jenkins jobs via cURL provides several benefits:
Automation: Allows you to integrate Jenkins job execution into other scripts or workflows.
Simplicity: A single HTTP POST request is all it takes to trigger a build.
Flexibility: You can pass parameters (or skip them) depending on your needs.
Remote Control: Enables running Jenkins jobs without manually accessing the Jenkins UI.
This approach is especially useful for integrating Jenkins jobs with other tools like Git, CI/CD pipelines, or custom dashboards.
Prerequisites
Before you start, ensure you have the following:
Jenkins API Token:
Go to your Jenkins profile.
Navigate to Configure > API Token.
Generate and copy the API token.
User Permissions: Ensure the Jenkins user has permissions to trigger jobs.
Jenkins URL: The URL where Jenkins is hosted (e.g., http://localhost:8080).
Job Token: The token configured for the Jenkins job (used for security).
cURL Installed: Verify that cURL is installed by running curl --version.
The cURL Command to Trigger a Jenkins Job
Here’s the cURL command to trigger a Jenkins job without parameters:
sh "curl -v -k --user admin:${JENKINS_API_TOKEN} -X POST \
-H 'cache-control: no-cache' -H 'content-type: application/x-www-form-urlencoded' \
'http://localhost:8080/job/DevOpsstatus/buildWithParameters?token=gitops-token'"
Breakdown of the Command:
curl -v -k:
-v for verbose output to debug.
-k allows insecure SSL connections (useful for self-signed certificates).
--user admin:${JENKINS_API_TOKEN}:
Authenticate using the Jenkins username (admin) and the API token.