python有jenkins获取配置文件的api,也有修改配置文件的api,
下面介绍下 如果修改jenkins job的配置文件内容:
import re import time import jenkins jenkins_url="http://xxx.com/jenkins" username="zhangsan" token="1.......de" jenkins = jenkins.Jenkins(jenkins_url,username,token) #添加 旧构建记录默认配置 daysToKeep = "20" numToKeep = "10" str = '<properties>\n' + \ '<jenkins.model.BuildDiscarderProperty>\n' + \ '<strategy class="hudson.tasks.LogRotator">\n' + \ '<daysToKeep>'+daysToKeep+'</daysToKeep>\n' + \ '<numToKeep>'+numToKeep+'</numToKeep>\n' + \ '<artifactDaysToKeep>-1</artifactDaysToKeep>\n' + \ '<artifactNumToKeep>-1</artifactNumToKeep>\n' + \ '</strategy>\n' + \ '</jenkins.model.BuildDiscarderProperty>' # 获取jenkins中所有job的名字 all_jobs=jenkins.get_jobs() for job in all_jobs: job_name = job["name"] job_confogure_data = jenkins.get_job_config(job_name) # 获取job congifure info if job_confogure_data.find("jenkins.model.BuildDiscarderProperty") == -1: # 如果这个job没有勾选 构建旧记录的配置策略 job_confogure_data = re.sub("<properties>", str, job_confogure_data, 1) # 需修改第一个<properties> else: job_confogure_data = re.sub("<daysToKeep>-?\d+</daysToKeep>", "<daysToKeep>" + daysToKeep + "</daysToKeep>", job_confogure_data) job_confogure_data = re.sub("<numToKeep>-?\d+</numToKeep>", "<numToKeep>" + numToKeep + "</numToKeep>", job_confogure_data) print(job_confogure_data) jenkins.reconfig_job(job_name, job_confogure_data) # 修改job configure信息 time.sleep(3)
因为jenkins的特性是:如果构建记录的保存几天 没有配置,那么值就是-1,具体可以通过jenkins job的config.xml查看:
http://xxx.com/jenkins/job/【jobName】/config.xml
所以需要用到一些正则表达式:
<daysToKeep>-?\d+</daysToKeep>标签:name,配置文件,python,confogure,job,jenkins,data From: https://www.cnblogs.com/pingguomang/p/18146536
解释:
?:表示?前面的字符可以出现0到1次
-?\d+ :表示可以是负数,也可以为负数
\d+: 这样 就只能匹配到正数