Recently, we need to trigger in the back-end code in our work Jenkins Task building , So I thought of Jenkins Are there any packaged API The class library provides , Used to deal with Jenkins Related operations .
Because our backend project is based on python Developed , So we need to python Callable class libraries . After some research, I locked "python-jenkins" This library .
In this article ,"python-jenkins" Some basic operations of , These operations can basically meet the vast use scenarios in the work .
python-jenkins yes Jenkins REST API Of Python Wrappers , It aims to provide a more traditional Python To control Jenkins The server . It provides a higher level of API, There are many convenience features .
In everyday use ,python-jenkins It can satisfy all calls jenkins Operational requirements for , Only for API How to use... Skillfully , Basically 10 You can use it in minutes .
Recommend several practical documents , You can query and use .
Code warehouse : https://github.com/openstack-archive/python-jenkins
Official website : https://pypi.python.org/pypi/python-jenkins/
Doc: http://python-jenkins.readthedocs.io/en/latest/index.html
Recommended pip Command line installation , The following figure shows that the installation is successful .
sudo pip install python-jenkins
You need to define a remote jenkins master server Of url as well as port. jenkins Account and password of , Used to connect to jenkins The server .
The code is as follows :
# Define remote jenkins master server Of url, as well as port
jenkins_server_url = 'http://106.75.xxx.xxx:8080/'
# Define the user's User Id and API Token, The acquisition method is the same as above
user_id = 'admin'
api_token = 'xxxxx'
# Instantiation jenkins object , Connect remote jenkins master server
server = jenkins.Jenkins(jenkins_server_url, username=user_id, password=api_token)
print(server.get_whoami())
establish jenkins after , return jenkins example , adopt server You can call any API Method .
Without build parameters job, In itself, this job When configuring , No build parameters are required , Just click to build .
The code is as follows :
server.build_job(name='android_dev')
With build parameters , This job When configuring , Configure the required build parameters , Here's the picture :
The code is as follows :
param_dict = {"name": "mike"}
server.build_job(name='android_dev_parameters', parameters=param_dict)
If the build is successful , View console output , You can see the parameters passed in the code "mike".
jenkins More commonly used in job It's free style job, Characteristic is job Configuration can use shell The script executes some parameters .
Need only job name , If there is already job Will report a mistake .
You also need a profile template , Provide string templates , Here's the picture :
The code is as follows :
print(server.create_job(name='android_dev_temp', config_xml=Jenkins_TEMP))
stay jenkins2.x Come into fashion pipeline Writing , In the form of code job The construction of .
In fact, it is common in use job To configure , There's no big difference .
pipeline The configuration template of is shown in the following figure :
The code is as follows :
print(server.create_job(name='android_dev_temp_pipeline', config_xml=pipelineBuildTemp))
Inquire about job The name of the message must exist , If there is no error .
The code is as follows :
print(server.get_job_info(name='android_dev_parameters'))
The information you find out , Contains a description 、 Task status 、 Build nodes and other information .
After the build completes the task , The build number will not be returned , So you need to execute the acquisition job Method of the last build number of .
adopt get_job_info The last build number can be obtained from the return data structure of the method .
The code is as follows :
# obtain job be known as job_name Of job The last build of
print(server.get_job_info(name='android_dev_parameters')['lastBuild']['number'])
Output : 3
The code is as follows :
# obtain job be known as job_name Of job The execution result state of a build of
build_number = server.get_job_info(name='android_dev_parameters')['lastBuild']['number']
print(server.get_build_info(name='android_dev_parameters', number=build_number)['result'])
The result state has several states :
The code is as follows :
print(server.get_build_info(name='android_dev_parameters',number=build_number)['building'])
There are several build States :
The code is as follows :
print(server.get_build_console_output(name="auto_blog", number=14))
In previous projects , We will test the interface automatically 、 The performance pressure test script is configured in jenkins Server , The business logic of the back-end service will dynamically create job、 structure job、 View the build log, etc .
python-jenkins The appearance of has greatly improved the operation jenkins The convenience of , I hope you can use it in your work ~