-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathJenkinsJobBuilderCopy.groovy
127 lines (121 loc) · 3.7 KB
/
JenkinsJobBuilderCopy.groovy
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*** BEGIN META {
"name" : "JenkinsJobBuilderCopy",
"comment" : "Copies the source project builders to target project",
"parameters" : [ 'vSourceProject', 'vTargetProject', 'vMode'],
"core": "2.222.1",
"authors" : [
{ name : "Ioannis K. Moutsatsos" }
]
} END META**/
import org.jenkinsci.plugins.scriptler.config.Parameter;
import org.jenkinsci.plugins.scriptler.config.Script;
import org.jenkinsci.plugins.scriptler.config.ScriptlerConfiguration;
import hudson.model.*
import jenkins.model.*
sourceJob=jenkins.model.Jenkins.instance.getJob(vSourceProject)
newJobName=vTargetProject
println 'Copying builders from:\n\t'+sourceJob.name+"\tto:\t$newJobName\n"
jenkins=jenkins.model.Jenkins.instance
cfg=ScriptlerConfiguration.getConfiguration()
try{
jenkins.createProject(FreeStyleProject,newJobName)
} catch (IllegalArgumentException e) {
println "$newJobName already exists-Reusing existing job"
reusing=true
}
targetJob=jenkins.model.Jenkins.instance.getJob(newJobName)
sBuilders=sourceJob.getBuildersList()
tBuilders=targetJob.getBuildersList()
k=0
/* vMode: review, append, overwrite
In 'review' mode we just list vSourceProject builder details
In 'append' mode we append source builders to those of target
In 'overwrite' mode we overwrite target builders with those of source
*/
switch(vMode){
case"review":
println "Reviewing builders of $vSourceProject"
sBuilders.each{
k++
builderClass=(it.class as String).tokenize('.')[-1] as String
println "$builderClass\tBuilder-$k"
printBuilderReport(it)
}
break
case"append":
println "Appending builders to $vSourceProject"
sBuilders.each{
k++
tBuilders.add(it)
builderClass=(it.class as String).tokenize('.')[-1] as String
println "$builderClass\tAppended-$k"
printBuilderReport(it)
}
break
case"overwrite":
println "Overwriting builders to $vSourceProject"
//remove all target builders
tBuilders.each{
tBuilders.remove(it)
}
//now append source builders
sBuilders.each{
k++
tBuilders.add(it)
builderClass=(it.class as String).tokenize('.')[-1] as String
println "$builderClass\tAppended-$k"
printBuilderReport(it)
}
break
default:
println "Unknown Mode. Defaulting to reviewing builders of $vSourceProject"
}
/* A method to print simple report
for a particular class of Jenkins Builder
*/
def printBuilderReport(hudson.tasks.Builder builder) {
builderClass=(builder.class as String).tokenize('.')[-1] as String
switch(builderClass){
case"ScriptlerBuilder":
println '\tScript:\n\t\t'+builder.getScriptId()
Script s = ScriptlerConfiguration.getConfiguration().getScriptById(builder.getScriptId());
println '\tAbout:\n\t\t'+s.comment
println '\tParameters:'
builder.getParameters().each{pm->
println '\t\t'+ pm.getName()+':\n\t\t\t'+pm.getValue()
}
break
case"EnvInjectBuilder":
propertiesFile=builder.getInfo().getPropertiesFilePath()
println '\t'+propertiesFile
println '\tMore Env Props:'+builder.getInfo().getPropertiesContent().replace('\n','\n\t')
break
case"Groovy":
scriptFile=builder.getScriptSource().getScriptFile()
println '\t'+scriptFile
println '\tScriptParameters:'+builder.getScriptParameters()
break
case"SystemGroovy":
scriptFile=builder.getSource().getScriptFile()
println '\t'+scriptFile
println '\tScriptBindings:'+builder.getBindings()
break
case"R":
println '\t Embedded R Command'
break
case"SSHBuilder":
println '\t'+builder.getSiteName()
break
case"ConditionalBuilder":
println '\t'+(builder.getRunCondition().class as String).tokenize('.')[-1] as String
builder.getConditionalbuilders().each{
println '\t'+it.class
printBuilderReport(it)
}
break
default:
//do default actions
println 'Uknown case'
}
print '\n'
}