Tuesday, August 3, 2010

Hudson and CLI

I have been doing some very neat stuff using Hudson the past couple of weeks. This is something which is really awesome, and am amazed at Hudson every time I use it. Hudson has a built-in command line interface which I have been using recently. I didn't find good documentation for doing some simple things like creating a job, copying a job and so on.. Hence, decided to write a blog entry so would help me and many others as  well.

First and foremost is to download the cli library, and you can do so easily from the Hudson-> Manage Hudson ->Hudson CLI link as shown below.

Now, click on the link and you will be presented with another page, were you click the download hudson-cli.jar link.
 

Next, open a command line and browse to where the hudson-cli.jar was downloaded, or even better copy to your favorite location.

At this point you should be able to run commands to create a job, enable a job, disable, copy and also delete a job. So, lets look at some of the commands.

1. Display help
java -jar hudson-cli.jar -s http://localhost:7223/ help


2. Create a Job, use the following command. config.xml is a sample configuration file which hudson needs for creating a job. You can find one in the hudson home folder. You also need to provide the configuration file next to the job name. Double click on the image below to see the parameters for passing in the configuration file. For some reason, each time I tried entering the same, blogpost automatically reformatted the same.
java -jar hudson-cli.jar -s http://localhost:7223/ create-job meera



If you go to the hudson main page, you should be able to see a new job called meera created.


Lets see a few more commands which can be executed:

3. Copy Job
java -jar hudson-cli.jar -s http://localhost:7223/ copy-job meera copy-meera

4. Disable a Job
java -jar hudson-cli.jar -s http://localhost:7223/ disable-job meera

5. Enable a Job

java -jar hudson-cli.jar -s http://localhost:7223/ enable-job meera

6. Delete a Job
java -jar hudson-cli.jar -s http://localhost:7223/ delete-job meera

Once you get an idea of how to use the command line interface, it is very easy to automate many things by using this interface.

I am also working on creating  a JAVA API for using the Hudson Remote API, and also a few build scripts as well. Will keep this blog updated on each of those as soon as I have them working.

16 comments:

  1. Nice tutorial... I am also seeing documentation for many days to get idea about CLI but failed due to insufficient help. After reviewing your post i am able to use this.
    thanks a lot for that type of post...

    please guide me how to specify the parameters which we specify through interface...

    ReplyDelete
  2. Great timing. I just started playing with Hudson CLI and this was the best documentation I could find for the commands.

    ReplyDelete
  3. Well that's helpful. Thanx a lot.
    But I'm blocked like Abdul Basit: "how to specify the parameters which we specify through interface..."

    That would be awesome if there is a way to do it.

    Have an excellent day

    ReplyDelete
  4. great !!!!!!!!!!!!
    thank you!!!

    ReplyDelete
  5. I have got enabled ldap security and the following example:

    java -jar jenkins-cli.jar -s http://localhost:8080/ copy-job jobname copy-jobname --username user –password userPassword

    does not work for me. Any ideas?

    ReplyDelete
  6. i was looking for an easy way to delete some of the jobs, this was helpful. Thanks !

    ReplyDelete
  7. Thanks. The article helped me in my task.

    ReplyDelete
  8. hi 123,
    use build command with -p param=value

    ReplyDelete
    Replies
    1. Hi,
      When i use the below command I'm getting connection timed out error.Can anyone help pls?

      java -jar _jenkins-cli.jar -s http://husdonserverip:8080/ help

      Exception in thread "main" java.net.ConnectException: Connection timed out: connect
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.PlainSocketImpl.doConnect(Unknown Source)
      at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
      at java.net.PlainSocketImpl.connect(Unknown Source)
      at java.net.SocksSocketImpl.connect(Unknown Source)
      at java.net.Socket.connect(Unknown Source)
      at java.net.Socket.connect(Unknown Source)
      at java.net.Socket.(Unknown Source)
      at java.net.Socket.(Unknown Source)
      at hudson.cli.CLI.(CLI.java:78)
      at hudson.cli.CLI.(CLI.java:63)
      at hudson.cli.CLI.main(CLI.java:182)

      rgds
      Vel

      Delete
  9. Hi, very nice and useful tutorial. How about the one of Hudson Remote API?

    I haven't found it. Could you kindly post the link if it's already created?

    Anyway this one has been very helpful for me. Thank you!

    ReplyDelete
    Replies
    1. You can install jenkins-python , and use remote access API using python .
      The link http://packages.python.org/python-jenkins/ http://javacruft.wordpress.com/2011/09/21/introducing-python-jenkins/
      might help you ...

      Thank You ,

      Harish Kayarohanam
      harish.pmprk@gmail.com

      Delete
  10. Hi,

    I tried to execute a copy-job command and I got the following exception:

    hudson.security.AccessDeniedException2: anonymous is missing the Create permission
    at hudson.security.ACL.checkPermission(ACL.java:53)
    at hudson.model.Node.checkPermission(Node.java:310)
    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:592)
    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:417)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:269)
    at java.util.concurrent.FutureTask.run(FutureTask.java:123)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:651)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:676)
    at java.lang.Thread.run(Thread.java:595)

    How can I execute the copy command with permissions?

    Thank You,

    Bondar Ferenc
    bondarferenc@info.uvt.ro

    ReplyDelete
  11. Focusing on the most relevant APIs for client development and not containing all types and members that are represented will limit the size of the client libraries, reduce the amount of time needed to download, and provide added security.

    apis jobs

    ReplyDelete
  12. is it possible to get the build details of a particular job in some format like an XML or plain text file so that i can programatically parse it to generate reports for it?

    ReplyDelete
  13. Could you please provide some samples to create job,build job in hudson using remote apis (json apis)

    ReplyDelete
  14. Hi,

    Please provide some samples to create jbo and build job in hudson using remote api(json api)

    Thanks,
    HariPrasadh
    hariprasadh89@gmail.com

    ReplyDelete