Jenkins-按视图批量复制项目

Jenkins-按视图批量复制项目

新建一个视图

jenkins–Manage Jenkins–Script Console

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import hudson.model.*
//源view
def str_view = "2dev前端"
//目标view
def str_new_view = "test"
//源job名称(模糊匹配)
def str_search = "-dev"
//目标job名称(模糊匹配后替换)
def str_replace = "-dev-copy"

def view = Hudson.instance.getView(str_view)

Hudson.instance.addView(new ListView(str_new_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 {
job = Hudson.instance.copy(item, newName)
} catch(IllegalArgumentException e) {
println("$newName job is exists")
continue
} catch(Exception e) {
println(e.toString())
continue
}
// job.disabled = true
job.save()

println(" $item.name copied as $newName is success")
}