Jenkins provided a very nice REST api to interact. In Python, we can use requests package. For simplicity, we are going to use password based authentication. For details on requests package, please see its official documentation.
Get the job configuration
Get the configuration is real easy. Please replace jenkins_ip,jenkins_port,job_name,username,password with your customized value.
import requests
url = http://jenkins_ip:jenkins_port/job/job_name/config.xml
r = requests.get(url, auth=('username', 'password'))
config = r.text
The config is an unicode xml string of the job configuration.
Set the job configuration
To modify the job configuration, you can post the modified config to the same url.
p = requests.post(url, data=config, auth=('username', 'password'))
Not there are two things you might need to pay attention to:
1. the url is url = http://jenkins_ip:jenkins_port/job/job_name/config.xml, not url = http://jenkins_ip:jenkins_port/job/job_name
2. use the data parameter, not the files parameter. Reason is listed here.
本文介绍如何利用Python的requests库与Jenkins REST API进行交互。通过示例代码展示了如何获取和设置Jenkins任务配置。
1万+

被折叠的 条评论
为什么被折叠?



