当前位置:网站首页>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.*')
边栏推荐
- Take care of JVM performance optimization (own note version)
- 【医保科普】维护医保基金安全,我们可以这样做
- How class only static allocation and dynamic allocation
- Enterprise live broadcast is on the rise: Witnessing focused products, micro-like embracing ecology
- 数据治理平台项目总结和分析
- RSS订阅微信公众号初探-feed43
- Learn iframes and use them to solve cross-domain problems
- 软件测试如何系统规划学习呢?
- MRS: Introduction to the use of Alluxio
- 复制带随机指针的链表
猜你喜欢

Shell 函数

拿捏JVM性能优化(自己笔记版本)
The general SQL injection flow (sample attached)

哎,又跟HR在小群吵了一架!

MySQL query optimization and tuning

y86.第四章 Prometheus大厂监控体系及实战 -- prometheus存储(十七)

2千兆光+6千兆电导轨式网管型工业级以太网交换机支持X-Ring冗余环网一键环网交换机

mq应用场景介绍

【源码】使用深度学习训练一个游戏

2 Gigabit Optical + 6 Gigabit Electric Rail Type Managed Industrial Ethernet Switch Supports X-Ring Redundant Ring One-key Ring Switch
随机推荐
元宇宙“吹鼓手”Unity:疯狂扩局,悬念犹存
SVM介绍以及实战
Based on the statistical QDirStat Qt directory
2022 Hangzhou Electric Power Multi-School League Game 5 Solution
千兆2光8电管理型工业以太网交换机WEB管理X-Ring一键环网交换机
2 Gigabit Optical + 6 Gigabit Electric Rail Type Managed Industrial Ethernet Switch Supports X-Ring Redundant Ring One-key Ring Switch
机器学习模型的“可解释性”
打造一份优雅的简历
案例 | 重庆银行流动数据安全挑战及应对实践
初识Numpy
目标检测-中篇
十一种概率分布
The video of machine learning to learn [update]
How to systematically plan and learn software testing?
42. 接雨水
FPGA parsing B code----serial 3
FFmpeg —— 通过修改yuv,将视频转为黑白并输出(附源码)
Use serve to build a local server
sql注入一般流程(附例题)
数组相关 内容 解析