需要将jenkins中某一个view中的所有job都复制到另外一个view中,一个一个复制有点蛋疼,所以查询了一下资料,使用groovy scripts 来实现这个功能
- 新建view
- 打开系统管理 -> 脚本命令行
import hudson.model.* //源view def str_view = "AOSIT_Frontend_SONAR" //目标view def str_new_view = "PTTEST_Frontend_SONAR" //源job名称(模糊匹配) def str_search = "-aosit-sonar" //目标job名称(模糊匹配后替换) def str_replace = "-pttest-sonar" 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 } job.disabled = true job.save()
// update the workspace to avoid having two projects point to the same location //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") }