Automating hudson/jenkins via REST and curl – a very small cookbook

As you know, Hudson supports a RESTful-API. So here a small cookbook using curl (running in cygwin). It is very easy, when you are accustomed to it.

#Get the current configuration and save it locally
curl -X GET http://user:password@hudson.server.org/job/myjobname/config.xml -o mylocalconfig.xml

#Update the configuration via posting a local configuration file
curl -X POST http://user:password@hudson.server.org/job/myjobname/config.xml --data-binary "@mymodifiedlocalconfig.xml"

#Creating a new job via posting a local configuration file
curl -X POST "http://user:password@hudson.server.org/createItem?name=newjobname" --data-binary "@newconfig.xml" -H "Content-Type: text/xml"

Update: Use --data-binary to post the data, else the line-endings won’t be transfered correctly.

10 thoughts on “Automating hudson/jenkins via REST and curl – a very small cookbook

  1. Nice, thank you. I’ve looked around and was unable to find how to use curl to enable a job. Do you happen to know?

    Reply
  2. hello !
    I try to use PHP curl for sending new config xml file, have you got an idea ?

    Thanks in advance !

    Reply
  3. If you wanna post with PHP and Jenkins API there is a code work fine for me:

    $xml = file_get_contents(‘http://user:apitoken@yourjenkins.com:8080/job/job2update/config.xml’);
    $url = ‘http://user:apitoken@yourjenkins.com:8080/job/jobupdated/config.xml’;
    $ch = curl_init($url);

    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);
    curl_close($ch);

    Reply
  4. I recently tried this approach to “transfer” some config files from one Hudson instance, to another, but they did not see to “take”. The jobs did (already) have the same name. There were no errors (I could find) but am wondering if you happen to know if there is “machine specific” info in the config files that matter? Or, perhaps “creation time” or similar?

    Reply
  5. Pingback: How to update a Jenkins job posting config.xml | Anthony Dahanne's blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s