程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

A preliminary study on Python operating Jenkins

編輯:Python

Preface

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 .

Introduce

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 .

  • Create new job
  • Copy existing job
  • Delete job
  • to update job
  • obtain job Building information for
  • Start building at work
  • Create nodes

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

install

Recommended pip Command line installation , The following figure shows that the installation is successful .

sudo pip install python-jenkins

Common operations

structure job

establish jenkins Connect

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 .

structure job( Without build parameters )

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')

structure job( With build parameters )

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".

establish job

Create a freestyle job

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))

establish pipeline Of job

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

obtain job Information

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 .

obtain job The last build of

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

obtain job The execution result state of a build of

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 :

  • Building a successful :SUCCESS
  • Build failed :FAILURE
  • In the building :None

obtain job Whether a build of is still under construction

The code is as follows :

print(server.get_build_info(name='android_dev_parameters',number=build_number)['building'])

There are several build States :

  • In the building : True
  • Not under construction : False

obtain job Build logs for

The code is as follows :

print(server.get_build_console_output(name="auto_blog", number=14))

Conclusion

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 ~


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved