I work with continuous integration software as a part of my job. I use both Hudson and Jenkins in my role and occasionally need to interact with them programmatically. There are two Python packages you can use for this task:
The Python Jenkins package will work with both Hudson and Jenkins which JenkinsAPI only works with Jenkins. I usually use Python Jenkins because of this, although I have recently started looking to see which one works better with artifacts and I discovered that JenkinsAPI is actually better for that sort of thing. So you will need to evaluate both of these packages depending on what you need to do.
Install Python Jenkins
To follow along with the code examples in this article, you will need to install Python Jenkins. You can use pip for that:
pip install python-jenkins
Now that it’s installed, let’s give Python Jenkins a whirl!
Getting All the Jobs from Jenkins
One common task is needing to get a listing of all the jobs that are configured in your build system.
To get started, you need to login to your Jenkins server:
import jenkins server = jenkins.Jenkins('http://server:port/', username='user', password='secret')
Now you have a Jenkins object that you can use to execute REST requests against your Jenkins CI server. The results that are returned are usually a Python dictionary or dictionary of dictionaries.
Here’s an example getting all the jobs that are configured on your CI system:
import jenkins server = jenkins.Jenkins('http://server:port/', username='user', password='secret') # Get all builds jobs = server.get_all_jobs(folder_depth=None) for job in jobs: print(job['fullname'])
This will loop through all the jobs that are configured in Jenkins and print out their job names.
Getting Job Information
Now that you know the names of the jobs on your Jenkins box, you can get more detailed information about each job.
import jenkins server = jenkins.Jenkins('http://server:port/', username='user', password='secret') # Get information on specific build job # This returns all the builds that are currently shown in # hudson for this job info = server.get_job_info('job-name') # Passed print(info['lastCompletedBuild']) # Unstable print(info['lastUnstableBuild']) # Failed print(info['lastFailedBuild'])
get_job_info() will give you a lot of information about the job, including all the currently saved builds. It is nice to be able to extract which builds have passed, failed or are unstable.
Getting Build Information
If you want to know how long a job takes to run, then you need to get down to the build level.
Let's find out how:
import jenkins server = jenkins.Jenkins('http://server:port/', username='user', password='secret') info = server.get_job_info('job-name') # Loop over builds builds = info['builds'] for build in builds: for build in builds: print(server.get_build_info('job-name', build['number']))
To get build metadata, you need to call
get_build_info(). This method takes in the job name and the build number and returns the metadata as a dictionary.
You can do a lot more with the Python Jenkins package. For example, you can use it to start a build job, create a new job or delete an old one as well as quite a few other things. Unfortunately, the documentation is pretty bare-bones, so you'll have to do a fair bit of experimentation to get it working the way you want.