PythonJenkins-Jenkins批量更新Job开启浅克隆⼀、Job更新测试阶段
【Alpha】所有CD流⽔线开启浅克隆为1,只获取最近⼀次commit
第⼀步:获取Jenkins的所有jobname
第⼆步: 遍历jobname,获取每个job的配置⽂件l
第三步:将获取到的xml类型字符串转化为document对象,然后修改机器节点的值,然后将修改的document对象写⼊⼀个新的xml⽂件第四步:将新的修改后的xml⽂件作为参数传给job
1、将标准和⾮标准的⽰例Job拷贝⾄Other视图中:
2、问题总结
1、shallow与depth关系:
# 只有开启shallow时才会相应的开启depth,默认的depth值为空,如下l
<extensions>
<hudson.sions.impl.CloneOption>
<shallow>true</shallow>
<noTags>true</noTags>
<reference></reference>
<depth></depth>
<honorRefspec>false</honorRefspec>
</hudson.sions.impl.CloneOption>
</extensions>
2、期望开启shallow和depth的值
<extensions>
<hudson.sions.impl.CloneOption>
<shallow>true</shallow> # 开启浅克隆
<noTags>true</noTags>
<reference></reference>
<depth>1</depth> # 克隆级别为1
<honorRefspec>false</honorRefspec>
</hudson.sions.impl.CloneOption>
</extensions>
3、编写代码如下:
使⽤re正则模块过滤出CD Job,并在CD Job中过滤出测试Job进⾏测试。
将执⾏失败操作的Job 的l 记录到临时本机⽬录中。
为了保证不重启Jenkins server ,采⽤启动/禁⽤ Jenkins job⽅式
import jenkins
ElementTree as ET
import re
def add_config_job(username, password):
# server获取信息
server = jenkins.Jenkins('jenkins.JenkinsDemon.live', username=username, password=password)
view_info = _info()
print(view_info)
count = 0
list = []
#依次获取对应视图⾥⾯的jobs名称及配置信息
for num in range(0,len(view_info['jobs'])):
job_name = view_info['jobs'][num]['name'] # .endswitch("CD-job")
# print(job_name)
result = re.search('CD', job_name)
# 测试Job
test_name1 = 'standard_job_CD'
test_name2 = 'non-standard_job_CD'
if result:
# print(job_name)
# list.append(job_name)
# print(list)
if job_name == "non-standard_job_CD":
jobs_config = _job_config(job_name)
print(jobs_config)
# 检查是否开启浅拷贝
if '<shallow>true</shallow>' not in jobs_config:
print(job_name + "shallow is not true")
if '<depth>1</depth>' not in jobs_config:
print(job_name + '\tdepth is ')
else:
print(job_name + '\tshallow is true and depth is ')
# 将配置写⼊xml⽂件中
tmp_xml = '/tmp/l'
with open(tmp_xml, 'w') as f:
f.write(jobs_config)
f.write('\n')
try:
# 更新第⼀次
modify_xml1(tmp_xml)
python处理xml文件# # 配置xml⽂件,添加对应的标签
# # modify_xml(tmp_xml)
with open(tmp_xml, 'r') as f:
jobs_config_new = f.read()
# # 此处的server⽤于修改配置,配置的名称在上⾯定义好
server = jenkins.Jenkins('jenkins.pingcode.live', username=username, password=password) server.disable_job(job_name)
# 更新第⼆次
modify_xml2(tmp_xml)
with open(tmp_xml, 'r') as f:
jobs_config_new = f.read()
# # 此处的server⽤于修改配置,配置的名称在上⾯定义好
server = jenkins.Jenkins('jenkins.pingcode.live', username=username,
password=password)
server.disable_job(job_name)
except Exception as e:
# print(e)
# 将配置失败的job名称保存到/tmp/config_failed.list列表中
fail_list = '/tmp/config_failed.list'
if count == 0:
with open(fail_list, 'w') as f:
f.write(job_name + '\n')
else:
with open(fail_list, 'a') as f:
f.write(job_name + '\n')
count = count + 1
def modify_xml1(tmp_xml):
tree = ET.parse(tmp_xml)
root = t()
# 更新选项
for shallow in root.iter('shallow'):
< = 'true'
print('shallow ')
tree.write(tmp_xml)
def modify_xml2(tmp_xml):
tree = ET.parse(tmp_xml)
root = t()
# 深浅需要另⾏添加
for depth ('depth'):
< = '1'
tree.write(tmp_xml)
if __name__ == '__main__':
username = 'daizhe 账号'
password = 'XXXXXXXXX密码'
add_config_job(username, password)
⼆、Job更新实战阶段
1、备份Jenkins server端所有 Job的 l
# 临时备份⼀份l
/yc/data/jenkins/data/jobs# mkdir back_2020.12.24
/yc/data/jenkins/data/jobs# cp -ravf * back_2020.12.24/
2、编写代码如下:
正则匹配所有CD流⽔线增加浅克隆及克隆深度
import sys
import jenkins
ElementTree as ET
import re
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(filename)s %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename='/tmp/alpha_jenkins.log',
filemode='w')
def add_config_job(username, password):
# server获取信息
server = jenkins.Jenkins('jenkins.Jenkins Demon.live', username=username, password=password) view_info = _info()
print(view_info)
count = 0
list = []
#依次获取对应视图⾥⾯的jobs名称及配置信息
for num in range(0,len(view_info['jobs'])):
job_name = view_info['jobs'][num]['name'] # .endswitch("CD-job")
# print(job_name)
result = re.search('CD', job_name)
if result:
jobs_config = _job_config(job_name)
print(jobs_config)
# 检查是否开启浅拷贝
if '<shallow>true</shallow>' not in jobs_config:
print(job_name + "shallow is not true")
if '<depth>1</depth>' not in jobs_config:
logging.info('{} --- depth is '.format(job_name))
else:
logging.info('{} --- shallow is true and depth is '.format(job_name))
# 将配置写⼊xml⽂件中
tmp_xml = '/tmp/l'
with open(tmp_xml, 'w') as f:
f.write(jobs_config)
f.write('\n')
try:
# 更新第⼀次
modify_xml1(tmp_xml)
with open(tmp_xml, 'r') as f:
jobs_config_new = f.read()
# # 此处的server⽤于修改配置,配置的名称在上⾯定义好
server = jenkins.Jenkins('jenkins.Jenkins Demon.live', username=username, password=password) server.disable_job(job_name)
# 更新第⼆次
modify_xml2(tmp_xml)
with open(tmp_xml, 'r') as f:
jobs_config_new = f.read()
# # 此处的server⽤于修改配置,配置的名称在上⾯定义好
server = jenkins.Jenkins('jenkins.pingcode.live', username=username,
password=password)
server.disable_job(job_name)
except Exception as e:
# 将配置失败的job名称保存到/tmp/config_failed.list列表中
fail_list = '/tmp/config_failed.list'
if count == 0:
with open(fail_list, 'w') as f:
f.write(job_name + '\n')
else:
with open(fail_list, 'a') as f:
f.write(job_name + '\n')
count = count + 1
def modify_xml1(tmp_xml):
tree = ET.parse(tmp_xml)
root = t()
# 更新选项
for shallow in root.iter('shallow'):
< = 'true'
print('shallow ')
tree.write(tmp_xml)
def modify_xml2(tmp_xml):
tree = ET.parse(tmp_xml)
root = t()
# 深浅需要另⾏添加
for depth ('depth'):
< = '1'
tree.write(tmp_xml)
if __name__ == '__main__':
username = 'daizhe-账号'
password = 'XXXX密码'
add_config_job(username, password)
3、验证Jenkins Job CD流⽔线配置:⽆误
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论