Recently, to build multiple test environments, it is necessary to batch copy all tasks under the dev view in Jenkins to views such as sit.
Explanation
Jenkins task name [测试环境标识]-[工程名称],如:dev-daodaotest,sit-daodaotest
rules: .
Show Tasks view in regular expressions: [测试环境标识]-.* ,如:dev-.*,sit-.*
.
The first type: bulk copy under the directory
Jenkins tasks are based on xml
documents stored all can be copied xml
to create a batch mode.
# 进入 jobs 目录下
$ cd ~/.jenkins/jobs
# 创建批量复制 shell 脚本
$ vi copyViewJobs.sh
#!/bin/bash
# 视图名称
viewName=$1
# 新视图名称
newViewName=$2
# 循环复制任务
for jobName in `ls /home/jenkins/.jenkins/jobs/`
do
# 判断文件存在并且是目录
if test -d $jobName
then
# 目录为 $viewName 开头,则进行复制
if [[ $jobName == *$viewName* ]]; then
# 截取工程名称
name=`echo $jobName|awk 'BEGIN{FS="'$viewName'-"} {print $2}'`
newJobName=$newViewName-$name
echo $newJobName
# 复制 config.xml
mkdir $newJobName && cp $jobName/config.xml $newJobName/
fi
fi
done
# 执行批量复制脚本,dev 视图下的任务负责到 sit 视图下
$ sh copyViewJobs.sh dev sit
Note : After copying, Jenkins needs to reload the configuration to take effect. Operation menu path: Manage Jenkins
-》Reload Configuration from Disk
.
The second kind: jenkins-cli
The implementation steps are similar to the first one, you can implement it according to your own scripting language. The key commands are briefly described below.
jenkins-cli
For usage, see: http: // localhost: 8080 / cli
# 下载 jenkins-cli.jar
$ wget http://localhost:8080/jnlpJars/jenkins-cli.jar
# 获取视图下的所有任务
$ java -jar jenkins-cli.jar -s http://localhost:8080/ -auth daodaotest:daodaotest list-jobs dev
# 复制任务
$ java -jar jenkins-cli.jar -s http://localhost:8080/ -auth daodaotest:daodaotest copy-job dev-daodaotest sit-daodaotest
Third: REST API
Like the second, only key commands are introduced. Take python-jenkins
api as an example here .
python-jenkins
Official website address: https://opendev.org/jjb/python-jenkins
Install Python Jenkins
# 安装 pip
$ sudo yum install epel-release && sudo yum install python-pip
# 安装 python-jenkins
$ pip install python-jenkins
Get the task name in the view
import jenkins
server = jenkins.Jenkins('http://localhost:8080', username='daodaotest', password='daodaotest')
# 查询 dev 视图下的所有任务
jobs = server.get_jobs(folder_depth=0, view_name='dev')
# 循环打印任务名称
for job in jobs:
print(job['fullname'])
Copy task
import jenkins
server = jenkins.Jenkins('http://localhost:8080', username='daodaotest', password='daodaotest')
# 任务是否存在,True 为存在,Fasle 为不存在
print(server.job_exists('dev-daodaotest'))
# 复制任务
server.copy_job('dev-daodaotest','sit-daodaotest')
# 打印任务信息
jobinfo = server.get_job_info('sit-daodaotest')
print(jobinfo)
请求报错 "Error 403 No valid crumb was included in the request"
Cause of error : jenkins placed a token named .crumb in the http request header. After using a reverse proxy and checking "Prevent Cross Site Request Forgery exploits" in the jenkins settings, this token will be removed by the forwarding server nginx as an illegal header, resulting in a jump failure.
Solution : Cancel "Prevent Cross Site Request Forgery exploits" in Jenkins' security settings.