Using Python with Jenkins
In this article, we will explain how to use Python with Jenkins. We will look at the following aspects:
- What Jenkins is?
- Jenkins API in Python
- Install Python Package
- Create a Jenkins client
You can also check this tutorial in the following video:
1. What is Jenkins?
Jenkins is a CI/CD tool developed in Java. It was developed in 2004 by a Sun systems developer named Kohsuke Kawaguchi. His goal was to create a method to perform continuous integration. The idea was to test the code before committing to avoid breaking builds. Although Jenkins started as a continuous integration tool, the current use covers the whole software delivery pipeline, including deployment. Jenkins encompasses various DevOps terminology throughout different pipeline creation and management options. Below is a list of some of the common terms and what they mean.
1.1 Jenkins Pipeline
The Jenkins Pipeline is a user-made model for the continuous delivery pipeline. All the software is subjected to a 3-step process
- Automated Builds
- Automated Testing
- Automated Deployment
In Jenkins, there are two ways to create a pipeline:
- Define the Pipeline through the user interface directly.
- Use the Pipeline as Code methodology and create a Jenkinsfile. The text file uses Groovy-compatible syntax to define the pipeline process.
pipeline { agent any stages { stage('Build') { steps { echo 'Building…' } } stage('Integ Test') { steps { echo 'Testing…' } } stage('Deploy') { steps { echo 'Deploying...' } } } }
- The mandatory
pipeline { }
block invokes the Jenkins Pipeline plugin. - The keyword
agent
defines where the Pipeline runs, whereany
indicates the Pipeline runs on any available agent. - The
stages { }
block represents the sequence in which the Pipeline runs. - The code contains three stages:
Build
,Test
andDeploy
, each with its respectivesteps { }
. - The
steps { }
tell Jenkins what to do at a particular point.
The scripted equivalent of the Jenkinsfile looks like the following:
node { stage('Build') { echo 'Building…' } stage('Integ Test') { echo 'Testing…' } stage('Deploy') { echo 'Deploying…' } }
Checking a Jenkinsfile into a source control tool such as git allows the whole team to edit, review, and adapt the steps in the delivery pipeline.
1.2 Continuous integration
Continuous integration is a software development procedure where each applied change invokes an automated build test. The process ensures the code integrates into a working executable form without bugs.
Continuous integration is a critical aspect in environments with multiple developers. Each developer makes changes to code, and each change has potential issues.
A continuous integration tool such as Jenkins helps test, identify, and address problems before applying changes to production.
1.3 Automated Testing
Automated testing for Jenkins presets test execution and stores the results. The idea is to ensure code works in various scenarios. Creating automated tests for distinct environments, such as several Java versions or operating systems, helps foresee and prevent problems in later releases.
The automated testing phases embed into the CI pipeline in Jenkins seamlessly. Various plugins help run unit, integration, functional, and regression tests and store the results for later viewing and analysis
2. Jenkins API in Python
For the purpose of this discussion, we will be using the Python Jenkins library. This is a python wrapper for the Jenkins REST API. It provides a higher-level API containing a number of convenience functions.
Python-Jenkins can be used to automate the deployed Jenkins servers. Some of the things we can use it for:
- Create new jobs
- Copy existing jobs
- Delete jobs
- Update jobs
- Get a job’s build information
- Get Jenkins master version information
- Get Jenkins plugin information
- Start a build on a job
- Create nodes
- Enable/Disable nodes
- Get information on nodes
- Put server in shutdown mode (quiet down)
- List running builds
- Delete builds etc
2.1 Installation of Python-Jenkins
sudo pip install python-jenkins
The above command installs the library on your machine. Alternatively, you can use PyCharm IDE for the installation too.
3. Create Jenkins Client
The below code snippet shows how to connect to Jenkins and get the server version
import jenkins server = jenkins.Jenkins('http://jenkins-hostname:port', username='user_id', password='password') user = server.get_whoami() version = server.get_version() print('Hi %s, Welcome to Jenkins version %s' % (user['fullName'], version))
Line 1 above imports the Jenkins module/package which gives us access to the Jenkins API
Line 2 above connects to the Jenkins server running on some server say localhost and port say 8080 and provides the username and password
Line 3 captures the name of the user running the code
Line 4 gets the server version
Line 5 prints the user name and the server version
4. Get All Jenkins Jobs
We can use the python package’s inbuilt method to access all configured Jenkins jobs. The following code is used to retrieve all of the jobs that are configured on your CI system
import jenkins jenkins_client = jenkins.Jenkins('http://jenkins-hostname:port/', username='user', password='password') jobs = jenkins_client.get_all_jobs(folder_depth=None) for job in jobs: print(job['fullname'])
Line 1 above imports the Jenkins module/package which gives us access to the Jenkins API
Line 2 above connects to the Jenkins server running on some server say localhost and port say 8080 and provides the username and password
Line 3 captures all the jobs running on the server (just the top-level names) in a python iterable
Line 4 and 5 loops through the list of jobs and print them to the console
5. Summary
In this article, we tried to understand what Jenkins is, what is used for and the various important terms related to CI/CD process.
We also learned how to install the Python Jenkins library and use the Jenkins API to talk to the Jenkins server hosted on some server, e.g localhost.
More details about the Python Jenkins library can be found here.