您的位置:首页 > 编程语言 > Python开发

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

2016-07-11 09:41 501 查看
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[/code], not 
url = http://jenkins_ip:jenkins_port/job/job_name[/code] 
2. use the
data
parameter, not the
files
parameter. Reason is listed here.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: