Jenkins is a popular automation server that provides an API for interacting with its features programmatically. By making HTTP requests to Jenkins API endpoints, you can automate various tasks and integrate Jenkins with other systems or scripts. In this guide, we'll explore how to use the curl
command-line tool to interact with Jenkins API.
Before making API requests, you'll need to obtain an API token from Jenkins. The API token acts as a credential to authenticate your requests. Here's how you can generate an API token:
- Log in to Jenkins using your Jenkins credentials.
- Click on your username or the user icon in the top-right corner of the Jenkins interface to access your user profile.
- Locate the "Configure" or "Configure User" option and click on it.
- Look for the section related to API tokens, typically labeled as "API Token" or similar.
- Click on the "Add new Token" or similar button to generate a new API token.
- Provide any necessary authentication or password to proceed.
- Once the API token is generated, copy and securely store it as it may not be displayed again.
With the API token in hand, you can now use curl
to make API requests to Jenkins. Here's an example command to call a Jenkins API endpoint:
curl -s -X GET -u ${USERNAME}:${API_TOKEN} ${JENKINS_URL}/${API_ENDPOINT}
Let's break down the command:
-
-s
makescurl
silent, disabling the progress meter for script-friendly output. -
-X GET
specifies the HTTP request method as GET. You can replaceGET
with other HTTP methods like POST, PUT, or DELETE depending on the desired action. -
-u ${USERNAME}:${API_TOKEN}
provides the basic authentication credentials. Replace${USERNAME}
with your Jenkins username and${API_TOKEN}
with the API token you generated earlier. -
${JENKINS_URL}
should be replaced with the base URL of your Jenkins instance. -
${API_ENDPOINT}
represents the specific API endpoint you want to access. For example, to retrieve information about a job, you could use/job/{jobName}/api/json
.
By executing this curl
command, you can retrieve data or perform actions through Jenkins API. The response from Jenkins will be displayed in the command output.
Remember to consult the Jenkins API documentation or your Jenkins administrator for details about available endpoints, request parameters, and response formats specific to your Jenkins installation.
In summary, by leveraging curl
and the Jenkins API token, you can easily automate and integrate Jenkins with your scripts or external applications.
Top comments (0)