当前位置:网站首页>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.*')
边栏推荐
- new Date converts strings into date formats Compatible with IE, how ie8 converts strings into date formats through new Date, how to replace strings in js, and explain the replace() method in detail
- 从图文展示到以云为核,第五代验证码独有的策略情报能力
- Postgresql源码(66)insert on conflict语法介绍与内核执行流程解析
- 如何动态添加script依赖的脚本
- FFmpeg —— 通过修改yuv,将视频转为黑白并输出(附源码)
- unity框架之缓存池
- 企业直播风起:目睹聚焦产品,微赞拥抱生态
- DIY电工维修如何拆卸和安装开关面板插座
- Take care of JVM performance optimization (own note version)
- FPGA parsing B code----serial 3
猜你喜欢

Reproduce 20-character short domain name bypass

Learn iframes and use them to solve cross-domain problems

SVM介绍以及实战

A Preliminary Study of RSS Subscription to WeChat Official Account-feed43

目标检测-中篇

Functions, recursion and simple dom operations

42. 接雨水

全网没有之一的JMeter 接口测试流程详解

深度学习——以CNN服装图像分类为例,探讨怎样评价神经网络模型

机器学习之视频学习【更新】
随机推荐
【 observe 】 super fusion: the first mention of "calculate net nine order" evaluation model, build open prosperity of power network
Embedded database development programming MySQL (full)
使用serve搭建本地服务器
怎样提高网络数据安全性
移动支付线上线下支付场景
XSS相关知识点
Eight guiding principles to help businesses achieve digital transformation success
八年软件测试工程师带你了解-测试岗进阶之路
2022年软件测试——精选金融银行面试真题
本周四晚19:00知识赋能第4期直播丨OpenHarmony智能家居项目之设备控制实现
出现504怎么办?由于服务器更新导致的博客报504错误[详细记录]
2003. 每棵子树内缺失的最小基因值 DFS
图像处理之Bolb分析(一)
【机器学习】21天挑战赛学习笔记(一)
一文详解DHCP原理及配置
【MD5】采用MD5+盐的加密方式完成注册用户和登录账号
帮助企业实现数字化转型成功的八项指导原则
力扣(LeetCode)215. 数组中的第K个最大元素(2022.08.03)
if,case,for,while
tkmapper的crud示例: