views:

169

answers:

3

Hi All,

Is there any way to create new Hudson job by one more Hudson job based one previous Jobs?

For example if I need to create new bunch of jobs one by one, Automatically create 4 jobs with similar configuration with different parameter

Basically steps like this

  1. create SVN branch I can call svn cp command and make it parametrized using script
  2. Create some build based on new svnbranch name
  3. Later tag it

Or other word, I need to clone the previous job and give the new branch name where ever $ Branch comes in new job.

Thanks

+1  A: 

You can try the Hudson Remote API for this kind of task (setting up an Hudson project).

See this tutorial for instance, and remember you can display the help quite easily:

java -jar hudson-cli.jar -s http://your_Hudson_server/ help

alt text

So, to copy a job:

java -jar hudson-cli.jar -s http://your_Hudson_server/ copy-job myjob copy-myjob
VonC
I think it's nicer to read the api from the Webpage. Just visit http://<Hudson-server>:<Hudson-Port>/api and you will see some general explanation as well as the documentation for the copy api. If you are interested in other parts of the API, go to the page where you actually would perform the configuration and add `/api` to it. Use whatever method you like best. The browser version even works if you are not able to access a Hudson jar locally (whatever, the unlikely reason might be)
Peter Schuetze
Reading you post for the second time I leaned something new. I thought you can only talk to Hudson with the cli when you are on the same server. But it is just an easy way to use the remote API from the command line. I always used `wget` to talk to Hudson. Unfortunatly I can only vote you up once. :(
Peter Schuetze
@VonC I have able to copy a job now I need to run it from hudson itself I tried execute this batch and ran java -jar hudson-cli.jar -s http://your_Hudson_server/ copy-job myjob copy-myjob did not create any job just success,How I can run from hudson itself?
sharp
@sharp, I am not sure, except may be from within a dedicated Hudson job itself (which would then launch the `java -jar` command line)
VonC
@VonC you are saying no way to invoke from batch command in hudson job?
sharp
@sharp: I am saying I never experienced and tried that particular operation and that making a dedicated job for it was the first idea that came up. I am not right now with the proper environment to experiment that possible solution though.
VonC
oh okay,what happend is C:\Users\sharp\.hudson\jobs\clone\workspace>java -jar hudson-cli.jar -s http://localhost:8080/ copy-job batch batch12 Unable to access jarfile hudson-cli.jar but if I put jar file under C:\Users\sharp\.hudson\jobs\clone\workspace it created new jar file,any wrong with my environment?Please advice.
sharp
@sharp: interesting, in a job wrapper, you would have first to make sure `hudson-cli.jar` is available in the workspace of said wrapper job. That is what you did, but I don't understand the "it created new jar file" part. Did the clone job then managed to copy and create a new job as it is supposed to?
VonC
@VonC Yes now working,what I did was cd C:\\Users\\praveen\\.hudson\\war\\WEB-INFjava -jar hudson-cli.jar -s http://localhost:8080/ copy-job batch batch19 so it created new job exactly copy of batch now need to replace particular string -:)
sharp
@sharp: that is where [parameterized Hudson builds](http://wiki.hudson-ci.org/display/HUDSON/Parameterized+Build) can come in handy.
VonC
@VonC very intresting,Can you help me out how to use this parametrized build to create multiple jobs for example if I want to run C:\\Users\\praveen\\.hudson\\war\\WEB-INF java -jar hudson-cli.jar -s localhost:8080 copy-job batch batch22 using parametrized how I can do,?I just saw the link u have included but I felt tricky,
sharp
@VonC I learnt to give parameter for two jobs -:) Thanks again for letting me know about this api. 25% done.
sharp
@Vonc is this available only for windows? I don't find in my current Linux where my prod Hudson installed,can I copy under WEB-INF in Linux Hudson home? Will it work?
sharp
@sharp: you have some clue in http://wiki.hudson-ci.org/display/HUDSON/Installing+Hudson+as+a+Unix+daemon, but also mainly in https://wiki.nci.nih.gov/display/BuildandDeploymentAutomation/How+to+Install+and+Configure+Hudson (regarding the installation of Hudson on Linux)
VonC
@VonC I tried instaling hudson-cli.jar file in linux but when I try to invoke I got this errorException in thread "main" java.io.FileNotFoundException: http://localhost:9080/hudson/cli at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1288) at hudson.cli.FullDuplexHttpStream.<init>(FullDuplexHttpStream.java:61) at hudson.cli.CLI.<init>(CLI.java:90) at hudson.cli.CLI.<init>(CLI.java:62) at hudson.cli.CLI.main(CLI.java:175)can you please advice how I can make it work in Unix/Linux?
sharp
A: 

You have the option that VonC just gave you (which is probably the safest way but you can also go a different rout by just creating a new directory in {Hudson_Home}\jobs (the directory name will be the job name) and copy a modified config.xml in there. The modification will basically just be the SVN URL. You should check out the xml from the job that you are copying. You need to find out how you change the xml file via script, but this is a secondary problem.

Unfortunately, you have to either restart Hudson, or force a reload of the configuration. (visit the page http://:/reload to reload the config).

Peter Schuetze
Thanks guys for valuable suggestion but restarting Hudson is not at all possible as it will break my other build -:(
sharp
@praveen: **VonC's solution will need no restart and no reload of the config**. That is one reason why I like VonC's more than mine. I am not sure if reloading the config from disk will break the build though. Anyway, go with VonC's solution and you are fine. `wget` works wonders when working with the remote api. But you can proably use many scripting languages to talk to the remote api besides, wget and hudsons's cli.
Peter Schuetze
@Peter I was able to get cloned job in windows but while I try in linux java -jar hudson-cli.jar -s http://localhost:9080/hudson help Exception in thread "main" java.io.FileNotFoundException: http://localhost:9080/hudson/cli at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1288) at hudson.cli.FullDuplexHttpStream.<init>(FullDuplexHttpStream.java:61) at hudson.cli.CLI.<init>(CLI.java:90) at hudson.cli.CLI.<init>(CLI.java:62) at hudson.cli.CLI.main(CLI.java:175) Any advice or suggestions?
sharp
I would submit a issue report, may be someone there can help you. http://issues.hudson-ci.org/
Peter Schuetze
**To get you going.** Have a look at `http://<HudsonURL>/api`. This will show you some help on the api side. It will also tell you, how to copy a job. Then you go to `http://<HudsonURL>/job/<job_name>/api` which will explain how to change the job settings. Since you can do this from your browser, you can also use the `wget` (a linux/unix tool for accessing web servers) to do this.
Peter Schuetze
I guess I am using older version of hudson I am trying to update new version and try it in new machine.
sharp
A: 

I am able to create copy new job if I make security false in Hudson config.xml if I make it on I get below error,any help? and I use very latest hudson version. java -jar ./hudson-cli.jar -s http://10.10.4.60:9080/hudson/ copy-job bldtest2-bb bldtest2-b5ff hudson.security.AccessDeniedException2: anonymous is missing the Create permission at hudson.security.ACL.checkPermission(ACL.java:53) at hudson.model.Node.checkPermission(Node.java:316) at hudson.cli.CopyJobCommand.run(CopyJobCommand.java:53) at hudson.cli.CLICommand.main(CLICommand.java:184) at hudson.cli.CliManagerImpl.main(CliManagerImpl.java:68) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at hudson.remoting.RemoteInvocationHandler$RPCRequest.perform(RemoteInvocationHandler.java:274) at hudson.remoting.RemoteInvocationHandler$RPCRequest.call(RemoteInvocationHandler.java:255) at hudson.remoting.RemoteInvocationHandler$RPCRequest.call(RemoteInvocationHandler.java:215) at hudson.remoting.UserRequest.perform(UserRequest.java:114) at hudson.remoting.UserRequest.perform(UserRequest.java:48) at hudson.remoting.Request$2.run(Request.java:270) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) at java.util.concurrent.FutureTask.run(FutureTask.java:138) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:619)

sharp