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.