最近,要搭建多套测试环境,需要把 Jenkins 中 dev 视图下的所有任务批量复制到 sit 等视图下。
说明
Jenkins 任务名称规则为:[测试环境标识]-[工程名称],如:dev-daodaotest,sit-daodaotest。
视图中显示任务的正则表达式:[测试环境标识]-.* ,如:dev-.*,sit-.*。
第一种:目录下批量复制
Jenkins 的任务都是以xml文件方式存储的,所有可以通过复制xml的方式来批量创建。
1#?进入?jobs?目录下
2$?cd?~/.jenkins/jobs
3
4#?创建批量复制?shell?脚本
5$?vi?copyViewJobs.sh
6#!/bin/bash
7
8#?视图名称
9viewName=$1
10#?新视图名称
11newViewName=$2
12
13#?循环复制任务
14for?jobName?in?`ls?/home/jenkins/.jenkins/jobs/`
15?do
16????#?判断文件存在并且是目录
17????if?test?-d?$jobName
18????then
19????????#?目录为 $viewName?开头,则进行复制
20????????if?[[?$jobName?==?*$viewName*?]];?then
21???????????#?截取工程名称
22???????????name=`echo?$jobName|awk?'BEGIN{FS="'$viewName'-"}?{print?$2}'`
23???????????newJobName=$newViewName-$name
24???????????echo?$newJobName
25???????????#?复制?config.xml
26???????????mkdir?$newJobName?&&?cp?$jobName/config.xml?$newJobName/
27????????fi
28?????fi
29?done
30
31?#?执行批量复制脚本,dev?视图下的任务负责到?sit?视图下
32?$?sh?copyViewJobs.sh?dev?sit
注意:复制完成后,Jenkins 需要重新加载配置才可以生效。操作菜单路径:Manage Jenkins--》Reload Configuration from Disk。
第二种:jenkins-cli
实现步骤与第一种类似,大家可以根据自己擅长的脚本语言来实现即可。下面简单介绍下关键命令。
jenkins-cli使用方法见:http://localhost:8080/cli
1#?下载?jenkins-cli.jar
2$?wget?http://localhost:8080/jnlpJars/jenkins-cli.jar
3
4#?获取视图下的所有任务
5$??java?-jar?jenkins-cli.jar?-s?http://localhost:8080/?-auth?daodaotest:daodaotest?list-jobs?dev
6
7#?复制任务
8$?java?-jar?jenkins-cli.jar?-s?http://localhost:8080/?-auth?daodaotest:daodaotest?copy-job?dev-daodaotest?sit-daodaotest
第三种:REST API
同第二种,仅介绍关键命令。这里以python-jenkinsapi 为例。
python-jenkins官网地址:https://opendev.org/jjb/python-jenkins
安装 Python Jenkins
1#?安装?pip
2$?sudo?yum?install?epel-release?&&?sudo?yum?install?python-pip
3
4#?安装?python-jenkins
5$?pip?install?python-jenkins
获取视图下任务名称
1import?jenkins
2
3server?=?jenkins.Jenkins('http://localhost:8080',?username='daodaotest',?password='daodaotest')
4
5#?查询?dev?视图下的所有任务
6jobs?=?server.get_jobs(folder_depth=0,?view_name='dev')
7
8#?循环打印任务名称
9for?job?in?jobs:
10????print(job['fullname'])
复制任务
1import?jenkins
2
3server?=?jenkins.Jenkins('http://localhost:8080',?username='daodaotest',?password='daodaotest')
4
5#?任务是否存在,True?为存在,Fasle?为不存在
6print(server.job_exists('dev-daodaotest'))
7
8#?复制任务
9server.copy_job('dev-daodaotest','sit-daodaotest')
10
11#?打印任务信息
12jobinfo?=?server.get_job_info('sit-daodaotest')
13print(jobinfo)
请求报错 "Error 403 No valid crumb was included in the request"
错误原因:jenkins 在 http 请求头部中放置了一个名为 .crumb 的 token。在使用了反向代理,并且在 jenkins 设置中勾选了“防止跨站点请求伪造(Prevent Cross Site Request Forgery exploits)”之后此 token 会被转发服务器 nginx 认为是不合法头部而去掉,导致跳转失败。
解决办法:在 Jenkins 的安全设置中取消“防止跨站点请求伪造(Prevent Cross Site Request Forgery exploits)”。
领取专属 10元无门槛券
私享最新 技术干货