[Jenkins] Use python requests to programatically get and set Jenkins job configuration

来源:互联网 发布:瑜伽初学者 知乎 编辑:程序博客网 时间:2024/05/01 19:02

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 requestsurl = http://jenkins_ip:jenkins_port/job/job_name/config.xmlr = 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.

0 0
原创粉丝点击