[SOLVED] How to Trigger a Jenkins Build Externally: A Simple Guide
In this chapter, we will look at different ways to start Jenkins builds from outside the Jenkins system. Jenkins is a popular automation server. It helps developers to automate build tasks. But sometimes, we need to start these builds from a distance. This guide will give you clear solutions to call Jenkins builds using different methods. Whether we want to connect with other systems or automate tasks, this article will cover all we need to know about triggering Jenkins builds from external sources.
Solutions We Will Discuss:
- Using the Jenkins REST API: We will learn how to use the REST API to start builds automatically.
- Triggering a Build with cURL: We will see how to use cURL commands to easily start builds.
- Using Jenkins CLI for Remote Builds: We will understand how to use the Jenkins Command Line Interface to run jobs from a distance.
- Integrating with Webhooks: We will explore how webhooks can help us start builds automatically from different platforms.
- Calling Jenkins Jobs via Python Scripts: We will find out how to write Python scripts that work with Jenkins to start builds.
- Configuring Jenkins for Remote Access: We will make sure that our Jenkins setup allows external build triggers.
- Frequently Asked Questions: We will answer common questions about calling Jenkins builds from outside.
By following this guide, we will be ready to manage and start Jenkins builds remotely. This will improve our continuous integration and deployment processes. For more information on Jenkins setup and fixing problems, check our articles on how to authenticate Jenkins CI and how to schedule jobs in Jenkins.
Part 1 - Using the Jenkins REST API
We can call a Jenkins build from outside of Jenkins using the Jenkins REST API. This API lets us start jobs and manage builds using simple HTTP requests.
Triggering a Build
We can start a build with a basic HTTP POST request to the Jenkins
server. Here is an example using curl
.
curl -X POST "http://<jenkins-url>/job/<job-name>/build" --user <username>:<api-token>
- Change
<jenkins-url>
to your Jenkins instance URL. - Change
<job-name>
to the job name you want to start. - Change
<username>
to your Jenkins username. - Change
<api-token>
to your API token. You can find or create this token in your user settings in Jenkins.
Passing Parameters
If your job needs some parameters, we can send them in the request like this:
curl -X POST "http://<jenkins-url>/job/<job-name>/buildWithParameters?PARAM1=value1&PARAM2=value2" --user <username>:<api-token>
- Replace
PARAM1
andPARAM2
with the names of the parameters you set in your Jenkins job.
Example
For example, if your job is called “TestJob” and you want to trigger
it with parameters ENV=prod
and VERSION=1.0
,
the command will look like this:
curl -X POST "http://your-jenkins-server/job/TestJob/buildWithParameters?ENV=prod&VERSION=1.0" --user admin:your-api-token
Configuring Jenkins for API Access
We must make sure that our Jenkins instance allows API access. We can set permissions for users in Jenkins. This way, we control who can start builds using the API. For more information on setting up Jenkins for remote access, you can check this guide on Jenkins configuration.
Using the Jenkins REST API gives us a great way to automate our CI/CD processes and start builds from outside. For more details on how to trigger Jenkins builds, please look at this article on triggering Jenkins builds.
Part 2 - Triggering a Build with cURL
We can trigger a Jenkins build using cURL by using the Jenkins REST API. This way, we can work with Jenkins jobs from far away. Here are the steps to start a Jenkins build from another system.
Ensure Jenkins is Accessible: First, we need to check that our Jenkins server is reachable from the command line where we will run cURL.
Setup API Token: If we have not done this yet, we need to create an API token for authentication. We can make one in our Jenkins user settings.
Construct the cURL Command: We use this syntax to start a build:
curl -X POST "http://<JENKINS_URL>/job/<JOB_NAME>/build" \ --user "<USERNAME>:<API_TOKEN>"
We should replace:
<JENKINS_URL>
with our Jenkins server URL (likehttp://localhost:8080
).<JOB_NAME>
with the name of our Jenkins job.<USERNAME>
with our Jenkins username.<API_TOKEN>
with our Jenkins API token.
Trigger a Build with Parameters: If our job needs parameters, we add
/buildWithParameters
to the URL and put in the parameters like this:curl -X POST "http://<JENKINS_URL>/job/<JOB_NAME>/buildWithParameters" \ --user "<USERNAME>:<API_TOKEN>" \ --data-urlencode "PARAM1=value1" \ --data-urlencode "PARAM2=value2"
Example Command: Here is an example of a complete cURL command to trigger a build:
curl -X POST "http://localhost:8080/job/MyJob/buildWithParameters" \ --user "admin:my_api_token" \ --data-urlencode "branch=main" \ --data-urlencode "deploy=true"
Check Build Status: We can check the build status using this command:
curl -s "http://<JENKINS_URL>/job/<JOB_NAME>/lastBuild/api/json" \ --user "<USERNAME>:<API_TOKEN>"
For more details, we can look at this guide on triggering Jenkins builds with cURL.
Part 3 - Using Jenkins CLI for Remote Builds
We can trigger a Jenkins build from outside using the Jenkins Command Line Interface (CLI). Here are the steps to do it:
Install Jenkins CLI: First, we need to download the
jenkins-cli.jar
file from the Jenkins server. We can find it athttp://<your-jenkins-url>/jnlpJars/jenkins-cli.jar
.Authenticate: We must have the right credentials like username and API token. We can authenticate Jenkins CI so we can use CLI.
Command to Trigger Build: We use this command to start a build:
java -jar jenkins-cli.jar -s http://<your-jenkins-url>/ build <job-name> -u <username> -p <api-token>
We should replace
<your-jenkins-url>
,<job-name>
,<username>
, and<api-token>
with our Jenkins server info, job name, username, and API token.Additional Options: If we want to pass parameters to the job, we can use the
-p
option like this:java -jar jenkins-cli.jar -s http://<your-jenkins-url>/ build <job-name> -p <parameter-name>=<parameter-value> -u <username> -p <api-token>
Check Build Status: To see if the build is running or finished, we can use this command:
java -jar jenkins-cli.jar -s http://<your-jenkins-url>/ get-job <job-name> -u <username> -p <api-token>
Using Jenkins CLI lets us start builds from outside Jenkins. It gives us more flexibility and control over our CI/CD processes. For more information, we can check triggering Jenkins builds.
Part 4 - Integrating with Webhooks
We can call a Jenkins build from outside Jenkins using webhooks. We need to set up our Jenkins server to respond to HTTP requests from a webhook. Many people use this with services like GitHub, GitLab, or Bitbucket. This helps us trigger builds when we commit code or make pull requests.
Steps to Integrate Webhooks with Jenkins:
Configure Jenkins Job for Webhook:
- First, we open the configuration for our Jenkins job.
- Then, in the “Build Triggers” section, we check the box for “Trigger builds remotely (e.g., from scripts)” and add an authentication token.
Setup Webhook in Your Version Control System:
- Next, we go to our repository settings, like in GitHub.
- We add a new webhook with these details:
- Payload URL:
http://<jenkins-server>/job/<job-name>/build?token=<your-token>
- Content type:
application/json
- Triggers: We choose events like “push” or “pull request” based on when we want to start the build.
- Payload URL:
Example cURL Command: To test the webhook, we can start the Jenkins build using cURL:
curl -X POST http://<jenkins-server>/job/<job-name>/build?token=<your-token>
Setting Up Security:
- We must make sure our Jenkins server is safe, preferably using HTTPS.
- We should use a secret token in the webhook URL to stop unauthorized access.
Check Build Status:
- We can watch the build status in Jenkins. We can also set up notifications to tell us when it succeeds or fails.
For more help on triggering Jenkins builds, we can check the article on how to trigger Jenkins builds. This integration helps us automate and continuously integrate. It makes our development work more smooth and efficient.
Part 5 - Calling Jenkins Jobs via Python Scripts
We can trigger Jenkins jobs straight from Python scripts. We use the
requests
library for this. It helps us interact with the
Jenkins REST API. Here is a simple guide on how to do this.
Prerequisites
First, we need to install the requests
library if we
don’t have it yet:
pip install requests
Triggering a Jenkins Job
We can use the following Python code to start a Jenkins job:
import requests
from requests.auth import HTTPBasicAuth
# Jenkins server details
= 'http://your-jenkins-url'
jenkins_url = 'your-job-name'
job_name = 'your-username'
user = 'your-api-token' # Generate this in Jenkins
api_token
# Trigger the job
= requests.post(
response f'{jenkins_url}/job/{job_name}/build',
=HTTPBasicAuth(user, api_token)
auth
)
if response.status_code == 201:
print("Job triggered successfully!")
else:
print(f"Failed to trigger job: {response.status_code} - {response.text}")
Configuring Jenkins
We have to make sure that our Jenkins is set to allow remote API access. We can look at the Jenkins configuration guide for more info.
Handling Job Parameters
If our job needs some parameters, we can pass them like this:
# Trigger a job with parameters
= {'param1': 'value1', 'param2': 'value2'}
params
= requests.post(
response f'{jenkins_url}/job/{job_name}/buildWithParameters',
=params,
params=HTTPBasicAuth(user, api_token)
auth )
So, this is how we can easily call Jenkins jobs using Python scripts. We use the Jenkins REST API for automation and connecting with other systems. For more details on how to trigger builds, we can check this Jenkins build trigger guide.
Part 6 - Configuring Jenkins for Remote Access
To call a Jenkins build from outside, we need to set up Jenkins for remote access. This means we should adjust the security settings. We also must make sure that our Jenkins can accept remote requests.
Enable Remote Access:
- Go to
Manage Jenkins
and thenConfigure Global Security
. - Under
Access Control
, check the box for “Enable security”. - Choose the right security realm and authorization method.
- Go to
Allow Anonymous Access (Optional):
- If we want to let some users access without logging in, we can check
the
Allow anonymous read access
. But we should remember that this is not safe for production environments.
- If we want to let some users access without logging in, we can check
the
Set up API Token:
- Each user can create their own API token. This token helps to authenticate API requests.
- Go to
User
, thenConfigure
, and look for theAPI Token
section to create a token.
Configure CSRF Protection:
- We must make sure that
Prevent Cross Site Request Forgery exploits
is on for extra security. If we have trouble with API calls, we might need to turn this off for a short time. But we should not do this in production.
- We must make sure that
Firewall and Network Configuration:
- Check if the firewall on our Jenkins server allows requests on the port Jenkins is using (default is 8080).
- We also need to make sure that the server can be reached over the network from the machine that makes the API calls.
Testing Remote Access:
We can test if Jenkins is available remotely using this cURL command:
curl -u username:api_token http://your-jenkins-url/jenkins/api/json
Remember to replace
username
,api_token
, andyour-jenkins-url
with your real Jenkins username, your API token, and your Jenkins URL.
For more details on how to trigger builds, we can check the guides on how to trigger a Jenkins build and how to authenticate Jenkins CI.
Frequently Asked Questions
1. How can we trigger a Jenkins build remotely?
To trigger a Jenkins build from outside, we can use the Jenkins REST API or cURL commands. We send an HTTP POST request to the correct build URL. This will start a build from outside Jenkins. For more details, we can check our guide on how to trigger a Jenkins build.
2. What authentication methods does Jenkins support for remote builds?
Jenkins has several authentication methods for remote builds. These include basic authentication, API tokens, and OAuth. We need to set up authentication correctly to keep our Jenkins safe while allowing remote triggers. For more details on how to authenticate Jenkins CI, we can read our full guide.
3. Can we use Python to trigger Jenkins jobs?
Yes, we can use Python scripts to trigger Jenkins jobs. The
requests
library in Python helps us send HTTP requests to
the Jenkins API. This allows us to start builds and even send
parameters. For a step-by-step example, we can check our section on calling
Jenkins jobs via Python scripts.
4. What are webhooks and how do they work with Jenkins?
Webhooks are HTTP callbacks. They help us trigger Jenkins jobs automatically when certain events happen. For example, when we have code commits in a version control system. By using webhooks with Jenkins, we can make a smooth CI/CD pipeline. To learn more about setting this up, we can read our guide on integrating with webhooks.
5. How do we configure Jenkins for remote access?
To configure Jenkins for remote access, we need to make sure our Jenkins server is reachable on the network. We also need to set up security measures. We will adjust settings in the Jenkins configuration to allow API access and enable the right authentication. For detailed help, we can check our article on configuring Jenkins for remote access.
Comments
Post a Comment