1、新增view
两种方式添加都可以:
方法一:
方法二:
2、使用正则表达式在视图中显示任务,然后点击保存视图。
3、点击manage jenkins-script console
4、修改复制的脚本,点击【运行】按钮
import hudson.model.*
//源view
def str_view = "istom-dr-239"
//目标view
def str_new_view = "istom-dr-211"
//源job名称(模糊匹配)
def str_search = "-DR-239"
//目标job名称(模糊匹配后替换)
def str_replace = "-DR-211"
def view = Hudson.instance.getView(str_view)
//copy all projects of a view
for(item in view.getItems())
{
//跳过未模糊匹配到的构建 任务
if (!item.getName().contains(str_search)) {
// 说明文字,表示跳过未匹配到的job,可加可不加
// println("but $item.name ignore ")
continue
}
//create the new project name
newName = item.getName().replace(str_search, str_replace)
// copy the job, disable and save it
def job
try {
//因为第一次导入后报错,所以添加了try-catch 跳过已存在的job
job = Hudson.instance.copy(item, newName)
} catch(IllegalArgumentException e) {
// println(e.toString())
println("$newName job is exists")
continue
} catch(Exception e) {
println(e.toString())
continue
}
job.disabled = true
job.save()
// update the workspace to avoid having two projects point to the same location
//下面开启后会报错,原因是groovy不用重新创建对象,注释后正常
//AbstractProject project = job
//def new_workspace = project.getCustomWorkspace().replace(str_search, str_replace)
//project.setCustomWorkspace(new_workspace)
//project.save()
//add job to view
Hudson.instance.getView(str_new_view).add(job)
println(" $item.name copied as $newName is success")
}
注意:修改上面的四处信息
5、查看视图
6、需要手工修改视图下面的每个任务的启动-配置相关信息,就完成视图的复制