文章目录

  • ​​一、初级版:修改一个参数的默认值​​
  • ​​`需求:把t1项目的DB_TYPE的默认值从mysql改为oracle`​​
  • ​​`代码`​​
  • ​​`效果`​​
  • ​​二、中级版:同时修改多个参数的默认值​​
  • ​​`需求:把t1项目的DB_TYPE改为oracle,OS_TYPE改为windows`​​
  • ​​`代码`​​
  • ​​`效果`​​
  • ​​三、参考文章​​

一、初级版:修改一个参数的默认值

需求:把t1项目的DB_TYPE的默认值从mysql改为oracle

python通过API接口修改Jenkins job的参数默认值_jenkins

代码

import jenkins
from xml.etree import ElementTree as ET
from xml.dom.minidom import parse

jenkins_server_url = "http://192.168.1.2:8080/"
username = "admin"
token = "111111"

server = jenkins.Jenkins(jenkins_server_url, username=username, password=token)
print(server.get_version()) # Jenkins版本

# 获取Jenkins job t1项目的配置文件
pipelineBuildConf = server.get_job_config('t1')
xml_root = ET.XML(pipelineBuildConf) # 获取根标签
tree = ET.ElementTree(xml_root)
tree.write("config.xml", encoding="utf-8",short_empty_elements=True) # 将配置文件保存到config.xml

# 处理config.xml
doc = parse("./config.xml")

# 删除字符串参数的某个元素
str_param_define=doc.getElementsByTagName("hudson.model.StringParameterDefinition")[0]
element_delete = doc.getElementsByTagName("defaultValue").item(0)
str_param_define.removeChild(element_delete)
# 创建该元素
str_param_define=doc.getElementsByTagName("hudson.model.StringParameterDefinition")[0]
new_element=doc.createElement("defaultValue")
new_element.appendChild(doc.createTextNode("oracle")) # 给元素增加文本
str_param_define.appendChild(new_element)

with open("config.xml", "w", encoding="utf-8") as f:
doc.writexml(f, indent='\n', addindent='\t', newl='\n', encoding="utf-8")

# 删除xml文件里面的空行
with open("config.xml","r",encoding="utf-8") as xmlfile:
lines = [line for line in xmlfile if line.strip() != ""]
with open("config.xml", "w",encoding="utf-8") as xmlfile:
xmlfile.writelines(lines)

f = open('config.xml', 'r',encoding="utf-8") # 打开文件
xml = f.read() # 读取文件内容
f.close()
server.reconfig_job("t1",xml) # 更新Job

效果

python通过API接口修改Jenkins job的参数默认值_运维_02

二、中级版:同时修改多个参数的默认值

需求:把t1项目的DB_TYPE改为oracle,OS_TYPE改为windows

python通过API接口修改Jenkins job的参数默认值_Jenkins_03

代码

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName :modify_jenkins_job.py
# @Time :2023/1/3
# @Author :运维@小兵
# @Function :通过API接口,修改Jenkins job的配置
# @Excute :python modify_jenkins_job.py

######################导入模块######################
import jenkins,os
from xml.etree import ElementTree as ET

######################定义变量######################
workdir = os.path.dirname(os.path.abspath(__file__)) # 获取当前脚本绝对路径
config_path = os.path.join(workdir,"config.xml")
jenkins_server_url = "http://192.168.1.2:6060/"
username = "admin"
token = "113383aa84ffa8315ec032f6a465f"
jenkins_server = jenkins.Jenkins(jenkins_server_url, username=username, password=token)
job_name = "t1"
job_str_dict = {"DB_TYPE":"oracle","OS_TYPE":"windows"}
"""
Check Jenkins Connection

:param param1: jenkins实例化server

:returns: null
"""
def check_jenkins_connect(jenkins_server):
try:
print('jenkins版本:%s' % jenkins_server.get_version()) # Jenkins版本
except Exception as e:
print('ERROR:%s' % e)

"""
Check Jenkins Connection

:param param1: 配置文件绝对路径
:param param1: Jenkins job名字
:param param1: 参数字典
:param param1: 需要修改的xml文件中的元素名

:returns: null
"""
def modify_job_str_defaultValue(config_path,job_name,str_dict,element_name):
pipelineBuildConf = jenkins_server.get_job_config(job_name) # 获取Jenkins job的配置文件
xml_root = ET.XML(pipelineBuildConf) # 获取文件根标签
str_param_object = xml_root.find("properties").find("hudson.model.ParametersDefinitionProperty").find(
"parameterDefinitions").iter("hudson.model.StringParameterDefinition") # 获取字符串参数标签
for str_param_cell in str_param_object:
element_value = str_param_cell.find("name").text # name元素的值,如DB_TYPE
str_param_cell.find(element_name).text = str_dict.get(element_value) # 修改元素的值
tree = ET.ElementTree(xml_root)
tree.write(config_path, encoding="utf-8", short_empty_elements=True) # 如果文件不存在,则创建文件,如果文件已存在则修改相应内容

# 更新job
f = open(config_path, 'r',encoding="utf-8") # 打开文件
xml = f.read() # 读取文件内容
f.close()
jenkins_server.reconfig_job(job_name, xml) # 更新job
print(f"[INFO] Jenkins Job:{job_name} Update Success")

if __name__ == '__main__':
try:
check_jenkins_connect(jenkins_server)
modify_job_str_defaultValue(config_path,job_name,job_str_dict,"defaultValue")
except Exception as e:
print('ERROR:%s' % e)

效果

python通过API接口修改Jenkins job的参数默认值_jenkins_04

三、参考文章

​​python处理Jenkins Job配置文件config.xml​​