How to Copy/Clone a Job via Groovy

3,074 views
Skip to first unread message

grayaii

unread,
Nov 22, 2011, 4:21:31 PM11/22/11
to jenkins...@googlegroups.com
The jenkins-cli has a really cool function to copy jobs:
java -jar jenkins-cli.jar -s http://localhost:8080/jenkins/ copy-job SRC
DEST

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.

grayaii

unread,
Nov 22, 2011, 4:34:42 PM11/22/11
to jenkins...@googlegroups.com
Oh, I forgot to mention, along with the jenkins-cli.jar, you can copy a job
via :
http://localhost/jenkins/api/
or by copying job folders and then restarting or reload the configuration
from disc.
I got this information from:
http://stackoverflow.com/questions/3886892/configure-or-create-hudson-job-automatically
http://stackoverflow.com/questions/3886892/configure-or-create-hudson-job-automatically

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

Les Mikesell

unread,
Nov 22, 2011, 5:49:33 PM11/22/11
to jenkins...@googlegroups.com
On Tue, Nov 22, 2011 at 3:34 PM, grayaii <georg...@rsa.com> wrote:
> Oh, I forgot to mention, along with the jenkins-cli.jar, you can copy a job
> via :
> http://localhost/jenkins/api/
> or by copying job folders and then restarting or reload the configuration
> from disc.
> I got this information from:
> http://stackoverflow.com/questions/3886892/configure-or-create-hudson-job-automatically
> http://stackoverflow.com/questions/3886892/configure-or-create-hudson-job-automatically
>
> But it would be nice via a *Groovy Script* too...

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

grayaii

unread,
Nov 22, 2011, 6:09:10 PM11/22/11
to jenkins...@googlegroups.com
thanks for reply! I don't know enough about groovy (yet) to easily do that
from Jenkin's "Script Console", but it definitely sounds do-able. By any
chance, do you have a working example, or a web page that does?

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

Simon Wiest

unread,
Nov 23, 2011, 2:49:13 AM11/23/11
to jenkins...@googlegroups.com
Hi grayaii,

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):

grayaii

unread,
Nov 23, 2011, 7:59:48 AM11/23/11
to jenkins...@googlegroups.com
Hi Simon. That is fricken awesome. Way cleaner than the way I was doing it.
I'm still on the Jenkins learning curve, but it's not as hard as I thought
it would be and your link will definitely help out.
Thanks!

--
View this message in context: http://jenkins.361315.n4.nabble.com/How-to-Copy-Clone-a-Job-via-Groovy-tp4097412p4099475.html

Manuel Doninger

unread,
Nov 23, 2011, 10:17:51 AM11/23/11
to jenkins...@googlegroups.com
I wrote a small blog post about writing Groovy scripts for Jenkins:
http://blog.doninger.net/2011/11/write-groovy-scripts-for-jenkins-with.html
This way you can use Eclipse with all code completion and Javadoc
features. For me this is a great help with writing scripts.
Executing the scripts of course won't work. However i thought about
writing an Eclipse plugin, which sends the script to the Jenkins
instance through the api. Don't know if that would work.

Manuel

domi

unread,
Nov 23, 2011, 11:59:14 AM11/23/11
to jenkins...@googlegroups.com
Manuel, thats a cool idea - I would not have thought about this! :)
btw. If you use the scriptler plugin [1], you get even around the copy paste of groovy scripts
/Domi

[1] https://wiki.jenkins-ci.org/display/JENKINS/Scriptler+Plugin

ZillaYT

unread,
Feb 14, 2019, 4:26:52 PM2/14/19
to Jenkins Users
How do I use this to copy a job in a folder to another job in the same folder?
Reply all
Reply to author
Forward
0 new messages