Jenkins 使用脚本批量复制任务

import hudson.model.*
//源view
def str_view = "Test-Front"
//目标view
def str_new_view = "Test02-Front"
//源job名称(模糊匹配)
def str_search = "Test-Front"
//目标job名称(模糊匹配后替换)
def str_replace = "Test02-Front"
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")
}

posted @ 2022-04-24 11:13  蒲公英PGY  阅读(243)  评论(0编辑  收藏  举报