jenkins批量复制视图项目到新的视图
1、当前视图为 测试2分支,创建了新的视图为国际化预生产
2、进入系统设置的脚本管理
import hudson.model.*
//源view
def str_view = "测试2分支"
//目标view
def str_new_view = "国际化预生产"
//源job名称(模糊匹配)
def str_search = "branch-test2"
//目标job名称(模糊匹配后替换)
def str_replace = "branch-preview"
def view = Hudson.instance.getView(str_view)
//copy all projects of a view
for(item in view.getItems())
{
//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
}
//是否禁用任务,false不禁用,true禁用
job.disabled = false
job.save()
Hudson.instance.getView(str_new_view).add(job)
println(" $item.name copied as $newName")
}
3、拷贝完成