This Rest API tutorial help to access jenkins using python.The jenkins are providing API interface to access all resources. You can do all thing as you do in jenkins ui using rest api. We ll use python third party API package to access jenkins rest api.
I have already shared tutorial How To Access Python API Using PHP. I ll demonstrate here how to access jenkins API using python.
There are two Python packages you can use for this task:
- The Python Jenkins package
- JenkinsAPI
The both python package will work with Jenkins and access JenkinsAPI. I will use jenkins package
to acccess rest api.
What is Jenkins
Jenkins is very popular self contained and opensource build tool.You can use jenkins for building, testing, and deploying software into server.You can get jenkins API information from bottom of jenkins server.The Jenkins provides hundreds of plugins to support building, deploying and automating any project.You can get more information from here.
Jenkins API Python
We ll create test.py
file that ll have all code to access jobs data.
Install Python Package
Let’s install python-jenkins
into your python application. You can use pip for that:
pip install python-jenkins
Create Jenkins Client
Let’s create jenkins object using username and password, that ll use further to access rest api:
import jenkins jenkins_client = jenkins.Jenkins('http://jenkins-hostname:port/', username='user', password='password')
The above client are returned are usually a Python dictionary.
Where is :
http://jenkins-hostname
: This is the Hostname of jenkins server.port
: This is the port number of jenkins server.user
: The jenkins server api username.password
: This is jenkins server api password.
How To Get All Jenkins Jobs
You can access all configured jenkins job using python package inbuilt method.The below code use to getting all 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'])
Above code ll access all configured jenkins jobs and loop through all the jobs, We ll print out their job names.
Conclusions:
You can access other operations as well using Python Jenkins package, You can start a build job, create a new job or delete an old job from Jenkins. You can get more information from documentation.
The post How to access Jenkins API Using Python appeared first on Rest Api Example.