17

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

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250

7 Answers7

12

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
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 3
    I think it's nicer to read the api from the Webpage. Just visit http://:/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 Oct 08 '10 at 15:46
  • 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 Oct 08 '10 at 18:06
  • @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? –  Oct 08 '10 at 19:43
  • @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 Oct 08 '10 at 19:52
  • @VonC you are saying no way to invoke from batch command in hudson job? –  Oct 08 '10 at 20:25
  • @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 Oct 08 '10 at 20:27
  • 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. –  Oct 08 '10 at 20:56
  • @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 Oct 08 '10 at 21:01
  • @VonC Yes now working,what I did was cd C:\\Users\\praveen\\.hudson\\war\\WEB-INF java -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 -:) –  Oct 08 '10 at 21:51
  • @sharp: that is where [parameterized Hudson builds](http://wiki.hudson-ci.org/display/HUDSON/Parameterized+Build) can come in handy. – VonC Oct 08 '10 at 21:55
  • @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, –  Oct 08 '10 at 22:11
  • @VonC I learnt to give parameter for two jobs -:) Thanks again for letting me know about this api. 25% done. –  Oct 09 '10 at 00:31
  • @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? –  Oct 09 '10 at 00:38
  • @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 Oct 09 '10 at 08:18
  • @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.(FullDuplexHttpStream.java:61) at hudson.cli.CLI.(CLI.java:90) at hudson.cli.CLI.(CLI.java:62) at hudson.cli.CLI.main(CLI.java:175) can you please advice how I can make it work in Unix/Linux? –  Oct 11 '10 at 17:20
  • is there possibility with the copy job argrument to specify multiple jobs with regex ? – klerk Oct 20 '14 at 21:28
8

Kind of already covered in the other answers, but for an easy way to copy the config.xml over:

curl --user USER:PASS -H "Content-Type: text/xml" -s  
--data-binary "@config.xml" "http://hudsonserver:8080/createItem?name=newjobname"
chrisbunney
  • 5,819
  • 7
  • 48
  • 67
Paul Andrew
  • 525
  • 2
  • 15
  • for whatever reason the '--data "@config.xml"' option didn't work for me. I used --data "$XML" instead and that worked wonderfully. YMMV. – Dave Dopson Nov 07 '11 at 20:20
  • 1
    You must use `--data-binary` to avoid http://issues.jenkins-ci.org/browse/JENKINS-7501 issue with newlines. – Julien Carsique Oct 31 '13 at 14:59
  • if you get `Error 403 No valid crumb was included in the request`, try to create API TOKEN for user instead of PASSWORD [ login, click user icon/name on top right -> configure -> add api token ] – Robert Ranjan Jan 27 '22 at 20:38
  • similar to this answer but using env vars. `curl -XPOST --user ${JENKINS_USER_ID}:${JENKINS_API_TOKEN} \ -H "Content-Type: text/xml" \ -s --data-binary "@config.xml" \ 'http://jenkins.robert.net/createItem?name=createdFromCLI3'` Note: it's uses `${JENKINS_API_TOKEN} ` – Robert Ranjan Jan 27 '22 at 20:42
8

You could use groovy system script like this :

def jenkins = hudson.model.Hudson.instance
def template = jenkins.getItem("MyTemplate")
def job = jenkins.copy(template,"MyNewJob")
job.scm = new hudson.scm.SubversionSCM("http://base/branches/mybranche")
job.save()
vhardion
  • 199
  • 2
  • 5
2

There seems to be a plugin for jenkins.

https://wiki.jenkins-ci.org/display/JENKINS/Job+DSL+Plugin

I have not tested the plug-in yet. But if the plugin works, it should alleviate some of human errors from straight copying a job and modifying variables/values.

Gerald
  • 31
  • 2
  • I created a [Docker image](https://github.com/thomasleveil/docker-jenkins-dsl-ready) for running a Jenkins instance ready to go for DSL scripts. Hope this helps – Thomasleveil Aug 22 '15 at 14:33
1
def jenkins = hudson.model.Hudson.instance
def template = jenkins.getItem("MyTemplate")
def job = jenkins.copy(template,"MyNewJob")
job.save()

I used this now I have to change the parameter values of MyNewJob using Groovy how will I do that?

ex I have a parameter called "Build_BranchName" and the default is //perforce/mybranch I have to change it to //perforce/mynewbranch

0

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
  • 16,185
  • 4
  • 44
  • 58
  • Thanks guys for valuable suggestion but restarting Hudson is not at all possible as it will break my other build -:( –  Oct 08 '10 at 17:05
  • @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 Oct 08 '10 at 18:08
  • @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.(FullDuplexHttpStream.java:61) at hudson.cli.CLI.(CLI.java:90) at hudson.cli.CLI.(CLI.java:62) at hudson.cli.CLI.main(CLI.java:175) Any advice or suggestions? –  Oct 11 '10 at 17:59
  • I would submit a issue report, may be someone there can help you. http://issues.hudson-ci.org/ – Peter Schuetze Oct 12 '10 at 13:44
  • **To get you going.** Have a look at `http:///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:///job//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 Oct 12 '10 at 13:53
  • I guess I am using older version of hudson I am trying to update new version and try it in new machine. –  Oct 12 '10 at 16:38
0

In case you're willing to use GIT (like I do, mirroring the main SVN repo, onto the Hudson/Jenkins server, and it works great).... ..you could try Stephen Haberman's post-receive-hudson:

This hook creates new jobs for each branch in the Hudson continuous integration tool. Besides creating the job if needed, the user who pushed is added to the job's email list if they were not already there.

In any case, that script can give you new hints on how to remote control Jenkins(Hudson).

inger
  • 19,574
  • 9
  • 49
  • 54