当前位置:网站首页>Jenkins 导出、导入 Job Pipeline
Jenkins 导出、导入 Job Pipeline
2022-08-04 04:01:00 【LeoHsiao1】
Jenkins 网页上可以手动修改 Job Pipeline 配置 ,但是大批量修改时比较麻烦。有几种批量修改的方法:
- 到 jenkins_home 安装目录下,执行 zip -r jobs.zip jobs/*/config.xml ,可以导出 Job 配置文件。不过修改了之后,需要重启 Jenkins 才会生效。
- 通过 Jenkins API 导出、导入 Job 配置文件。
本文给出第二种方法的 Python 示例代码:
""" - 该脚本用于导出、导入 Jenkins 的 Pipeline 脚本,保存为 XML 文件 - 需要安装 pip install jenkinsapi - 导入的配置文件包含非 ASCII 码时,需要修改 update_config() 的定义代码,注释 config = str(config) 一行 """
import os
import re
from jenkinsapi.jenkins import Jenkins
# 连接 Jenkins
jk = Jenkins('https://jenkins.test.com/', username='***', password='***', timeout=10, useCrumb=True)
config_suffix = '.xml'
def export_job(job_pattern='.*', work_dir='.'):
for job_name in jk.keys():
if not re.findall(job_pattern, job_name):
continue
config = jk.get_job(job_name).get_config()
config_file = os.path.normpath(os.path.join(work_dir, job_name + config_suffix))
os.makedirs(os.path.dirname(config_file), exist_ok=True)
with open(config_file, 'w', encoding='utf-8') as f:
f.write(config)
def import_job(job_pattern='.*', work_dir='.'):
for line in os.walk(work_dir, onerror=print):
sub_dir,dir_list,file_list = line
for file in file_list:
if file[-len(config_suffix):] != config_suffix:
continue
# 获取 job name
path_fileds = list(os.path.split(sub_dir)) + [file]
job_name = '/'.join(path_fileds).removeprefix(work_dir.replace('\\', '/')).removeprefix('/').removesuffix(config_suffix)
# 读取 job 的配置文件
if not re.findall(job_pattern, job_name):
continue
config_file = os.path.join(sub_dir, file)
with open(config_file, 'r', encoding='utf-8') as f:
config = f.read()
# 导入 job 配置
if jk.has_job(job_name):
jk.get_job(job_name).update_config(config.encode('utf-8'))
print('已导入Job:', job_name)
else:
print('Jenkins不存在该Job:', job_name)
# 自动创建 Job 会失败,目前只能导入 Jenkins 上已创建的 Job
# jk.create_job(job_name, config)
# 执行导出、导入
export_job('test.*')
import_job('test.*')
边栏推荐
- 6-port full Gigabit Layer 2 network managed industrial Ethernet switch Gigabit 2 optical 4 electrical fiber self-healing ERPS ring network switch
- RSS订阅微信公众号初探-feed43
- 系统设计.如何设计一个秒杀系统(完整版 转)
- Polygon zkEVM网络节点
- Embedded database development programming MySQL (full)
- SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropri
- Senior PHP development case (1) : use MYSQL statement across the table query cannot export all records of the solution
- Postgresql source code (66) insert on conflict grammar introduction and kernel execution process analysis
- 哎,又跟HR在小群吵了一架!
- SQL query String field less than 10 how to check
猜你喜欢
随机推荐
企业直播风起:目睹聚焦产品,微赞拥抱生态
马尔可夫链
如果禁用了安全启动,GNOME 就会发出警告
Oracle与Postgresql在PLSQL内事务回滚的重大差异
Senior PHP development case (1) : use MYSQL statement across the table query cannot export all records of the solution
DIY电工维修如何拆卸和安装开关面板插座
[Ryerson emotional speaking/singing audiovisual dataset (RAVDESS)]
函数,递归以及dom简单操作
拿捏JVM性能优化(自己笔记版本)
技术解析|如何将 Pulsar 数据快速且无缝接入 Apache Doris
[Medical Insurance Science] To maintain the safety of medical insurance funds, we can do this
Significant differences between Oracle and Postgresql in PLSQL transaction rollback
docker+bridge+redis master-slave+sentry mode
2千兆光+6千兆电导轨式网管型工业级以太网交换机支持X-Ring冗余环网一键环网交换机
MRS: Alluxio的使用介绍
Hey, I had another fight with HR in the small group!
基于Qt的目录统计QDirStat
Postgresql源码(66)insert on conflict语法介绍与内核执行流程解析
The video of machine learning to learn [update]
JVM的内存模型简介




![出现504怎么办?由于服务器更新导致的博客报504错误[详细记录]](/img/e0/32d78fac04dc2deb1cb1f847a7bab5.png)



![The video of machine learning to learn [update]](/img/e7/c9a17b4816ce8d4b0787c451520ac3.png)
