but ideally I would like to run a groovy script via the Script Console.
I tried to query the name and properties of a project object in hopes that I
can figure out what to call, but i didn't see any "copy" or "clone" command
(or something similar), so it must not be a function of a FreeStyleProject
object.
Does anyone know how to copy a job via the Script Console?
BTW, here is the code I used to query the project object:
for(item in hudson.model.Hudson.instance.items) {
if (item.name == "The Job I Want To Copy") {
// print out everything about a project object:
item.metaClass.properties.each { p ->
println('======================');
println p.name;
println p.type;
}
}
}
--
View this message in context: http://jenkins.361315.n4.nabble.com/How-to-Copy-Clone-a-Job-via-Groovy-tp4097412p4097412.html
Sent from the Jenkins users mailing list archive at Nabble.com.
But it would be nice via a *Groovy Script* too...
--
View this message in context: http://jenkins.361315.n4.nabble.com/How-to-Copy-Clone-a-Job-via-Groovy-tp4097412p4097457.html
Can't your groovy script import the cli.jar and use it directly - or
chat over the rest interface like any other language?
--
Les Mikesell
lesmi...@gmail.com
I ended up doing it by brute force:
java -jar jenkins-cli.jar -s http://localhost:8080/jenkins/ groovy
foo.groovy | xargs -I {} java -jar jenkins-cli.jar -s
http://localhost:8080/jenkins/ copy-job {} {}_MyClonedJob
Where foo.groovy is simply the following (which can be executed via the
"Script Console" too:
partialJobName = "JobIWantToCopy";
for(item in hudson.model.Hudson.instance.items) {
loc = item.name.indexOf(partialJobName);
if (loc >= 0) {
println(item.name);
}
}
I was able to clone all jobs that contain "partialJobName".
--
View this message in context: http://jenkins.361315.n4.nabble.com/How-to-Copy-Clone-a-Job-via-Groovy-tp4097412p4097809.html
you may try this (it's exactly what the CLI command does internally):
inst = jenkins.model.Jenkins.instance;
inst.copy(inst.getItem("MyFooBarProject"), "CopyOfMyFooBarProject");
BTW, a nice way to get some inspiration on how to use the Jenkins API in
your Groovy scripts, is to look at the actual source code of the CLI
commands, e.g. at
https://github.com/jenkinsci/jenkins/blob/master/core/src/main/java/hudson/cli/CopyJobCommand.java.
Groovy and Java are very close cousins by design, so adaption should not
be that tricky ;O).
Cheers,
Simon.
--
grayaii (22.11.2011 22:21):
--
View this message in context: http://jenkins.361315.n4.nabble.com/How-to-Copy-Clone-a-Job-via-Groovy-tp4097412p4099475.html
Manuel
[1] https://wiki.jenkins-ci.org/display/JENKINS/Scriptler+Plugin