In this post, we will access Jenkins rest API using python-Jenkins, a third-party API package. The Jenkins REST API can be accessed using the python-jenkins
library, which provides a Python wrapper for the Jenkins REST API.
This library makes it easier to interact with Jenkins programmatically, allowing you to automate tasks, retrieve information about builds and jobs, and perform other actions.
Before we go any further, What exactly is Jenkins, and why is it significant?
What exactly is Jenkins?
What is Jenkins
Jenkins is an open-source platform that enables all developers to create, test, and distribute software. It is a tool for automation.
Jenkins is a Java-based open-source DevOps solution that enables continuous integration of projects (also known as jobs) or end-to-end automation. Mostly used for automating, developing, testing, and deploying software into servers.
Why is Jenkins So Popular
Continuous Integration
If you are working on a large application that requires a lot of integration and code quality checks, each developer must perform tests on their local environment before merging the code into the master branch, which takes time for each developer. However, if we have a centralized integration server where developers can test their code and build. It will be simpler for them because they will not have to wait until the build is completed in the local environment. In the event of a build failure, the CI server can notify the developer, who can then fix the problem.
Continuous Deployment / Continuous Delivery
Won't it be easier when you can code -> build-> test -> deploy in a single pipeline till the PROD environment? Well, that's one of Jenkins's jobs.
The developer commits the code to a feature branch, and the automated Jenkins CI build triggers that run the Integration test follow. The developer will now create a Pull Request (PR) for the reviewer to validate the changes, and once the PR is approved, the developer will be able to merge the changes into the Master Branch. Now, the master branch can be used to create a new build artifact to promote the build to the Dev, QA, or higher environments. Next, we can request that the QA team run the test in the QA environment. However, if we use automation to validate the build in a higher environment, we won't have to wait for the QA team to certify it.
Jenkins may therefore be utilized for continuous deployment, allowing us to deploy to production as soon as a developer submits changes to the master branch.
Task automation
Giving another scenario where you can use Jenkins in task automation.
If you are working across many environments, we will need to upgrade or install something on each one. What if your installation and update include more than 100 steps? If you conduct the task manually, there is a good chance that errors will occur. What if you documented every step that needs to be taken to complete the activity in Jenkins? That way, it would be accurate, quicker, and easier to complete the installation or upgrade.
Building with our Jenkins API using Python
Before we begin,
Note: This tutorial requires basic knowledge of python;
Let's begin building with Jenkins,
Requirements for our project
The Python-Jenkins package
Jenkins API
Pout
let's install our packages for our application, using pip;
pip install python-jenkins, pout
pout: prints an ordered and more understandable dictionary arrangement in Python.
Creating Jenkins Client
Let's create a basic Jenkins client structure that will be using the API credential to get the Jenkins rest API:
import jenkins
jenkins_client = jenkins.Jenkins('http://jenkins-hostname:port/', username='user',password='password')
http://jenkins-hostname
: The hostname of the currently active Jenkins server.port
: This is the port that the Jenkins server uses to communicate.user
: The username for the Jenkins server API.password
: This is the API password for the Jenkins server. (str)
The client is returned in the form of a Python dictionary.
Note that you need to replace
'
http://your_jenkins_server:8080
'
,'your_username'
, and'your_password'
with the correct values for your Jenkins server.Typically, your username and password are saved as an external environment.
How To Get All Jobs running on Jenkins Server
Using the python package's built-in method, you could get entry to all configured Jenkins jobs. The following code is used to retrieve all the jobs configured to your CI system:
import jenkins
import os
import pout
# Jenkins client initialization
client= jenkins.Jenkins('http://your_jenkins_server:8080', username='your_username', password='your_password')
# this gets all the running jobs
# on jenkins server
def get_all_jobs():
jobs = client.get_jobs()
pout.v(jobs)
get_all_jobs()
How To Get Details about a specific Job running on Jenkins Server
In this example, the jenkins
library is imported and a connection to the Jenkins server is established using the Jenkins
class. The get_job_info
method is then used to retrieve information about a job, which is stored in the job_info
variable. Finally, the name and description of the job are printed.
import jenkins
# Connect to the Jenkins server
client= jenkins.Jenkins('http://your_jenkins_server:8080', username='your_username', password='your_password')
# Get information about a job
job_info = client.get_job_info('your_job_name')
# Print the job's name and description
print(job_info['name'], job_info['description'])
The output of this code depends on the values of http://your_jenkins_server:8080
, your_username
, your_password
, and your_job_name
. If the connection to the Jenkins server is successful and the job exists, the code will print the job's name and description.
For example, if the job name your_job_name
has the name "My Job" and the description "This is a test job", the code will produce the following output:
My Job This is a test job
How to Create and delete a Node
The python-jenkins
library provides methods for managing nodes in Jenkins through the Jenkins REST API. You can use these methods to automate the management of Jenkins nodes, such as creating and deleting nodes, or changing the configuration of existing nodes.
Here is an example of how you can use python-jenkins
to create a new Jenkins node:
import jenkins
# Connect to the Jenkins server
client= jenkins.Jenkins('http://your_jenkins_server:8080', username='your_username', password='your_password')
# Create a new Jenkins node
node_config = """
<slave>
<name>new-node</name>
<description>This is a new node</description>
<remoteFS>/path/to/jenkins/home</remoteFS>
<numExecutors>2</numExecutors>
<mode>NORMAL</mode>
<label>new-node</label>
<nodeProperties/>
<userId>jenkins</userId>
</slave>
"""
client.create_node("new-node", node_config)
In this example, a connection to the Jenkins server is established using the Jenkins
class. The create_node
method is then used to create a new Jenkins node, passing in the name of the node and an XML configuration for the node.
If the connection to the Jenkins server is successful and the node is created successfully, the code will not produce any output. If there is an error connecting to the Jenkins server or creating the node, an exception will be raised, and the code will terminate.
Here is an example of how you can use python-jenkins
to delete a Jenkins node:
import jenkins
# Connect to the Jenkins server
client= jenkins.Jenkins('http://your_jenkins_server:8080', username='your_username', password='your_password')
# Delete a Jenkins node
client.delete_node("new-node")
How to Create New Jobs on Jenkins Pipeline
The python-jenkins
library provides methods for creating new jobs in Jenkins through the Jenkins REST API. You can use these methods to automate the creation of new jobs in Jenkins, such as creating pipeline jobs or freestyle jobs.
Here is an example of how you can use python-jenkins
to create a new Jenkins pipeline job:
import jenkins
# Connect to the Jenkins server
server= jenkins.Jenkins('http://your_jenkins_server:8080', username='your_username', password='your_password')
# Define the pipeline script
pipeline_script = """
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
stage('Test') {
steps {
echo 'Testing...'
}
}
stage('Deploy') {
steps {
echo 'Deploying...'
}
}
}
}
"""
# Create a new pipeline job
server.create_job("new-pipeline-job", jenkins.EMPTY_CONFIG_XML, jenkins.DEFAULT_PIPELINE_SCRIPT % pipeline_script)
In this example, a connection to the Jenkins server is established using the Jenkins
class. The pipeline script is defined in the pipeline_script
variable, and the create_job
method is then used to create a new pipeline job in Jenkins, passing in the name of the job, an XML configuration template, and the pipeline script.
Conclusion
In conclusion, Python and Jenkins are powerful tools for automating software development processes. By combining Python and Jenkins, developers can automate many of the repetitive tasks involved in software development, freeing up time for more important tasks. For example, Python scripts can be used to automate test cases, while Jenkins can be used to automate the build, test, and deployment process. This helps to ensure that software is delivered faster and with higher quality.
In short, the integration of Python and Jenkins is a valuable combination for software development teams who want to streamline their workflows and improve the quality of their software products.