Monday, March 31, 2014

Use Jenkins REST API to Update Job Configurations Automatically

I manage several Jenkins jobs for a single git repository that has several modules (i.e. directories). Each of the modules has the same structure and the job for each of the modules is essentially the same, differentiated only by the name of the directory.

I recently needed to make a change to each of the job configurations and rather than do so by hand, I thought I'd investigate the possibility of doing so automatically using the Jenkins REST API.

I had originally created all the jobs using the API by posting to the generic the http://jenkins.example.com/createItem URI. I had since moved the jobs into folders and hitting createItem only created new jobs and did not update the proper jobs.

I did some searching on the internet and couldn't find any help. I eventually found my answer on the api page for the folder. I'm posting my code below for anyone else with similar questions.

# First, get the http://jenkins.example.com/job/folder-name/job/sample-job--template/configure looking like you want

read -s token
# type token from http://jenkins.example.com/user/$userName/configure

# Download the configuration XML for the template job (which will be our model template)
curl -v -u "bvanevery:$token" http://jenkins.example.com/job/folder-name/job/sample-job--template/config.xml > generic-config.xml

# My modules
declare modules=('module1' 'module2' 'module3')

# POST the updated configuration XML to Jenkins
for m in ${modules[@]}; do 
   echo "module $m";
   sed "s/MODULE/$m/g" generic-config.xml > $m-config.xml; 
   curl -v -X POST --data-binary @$m-config.xml -u "bvanevery:$token" \
        -H 'Content-Type: application/xml' \
        "http://jenkins.example.com/job/folder-name/job/$m/config.xml ;
done


13 comments:

  1. Do you know how to update the job without restarting the jenkins using any API ?

    ReplyDelete
  2. The reason the average job seeker presently gets 1 telephone interview per 20 job applications, and spends four months job searching is poor technique.guarantor

    ReplyDelete
  3. I really appreciate the kind of topics you post here. Thanks for sharing us a great information that is actually helpful. Good day! Career advancement blog

    ReplyDelete
  4. The blog delineates how hard the author has taken a shot at this.
    Resumeyard

    ReplyDelete
  5. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free.
    resume template college student

    ReplyDelete
  6. I have seen some great stuff here. Worth bookmarking for revisiting. I surprise how much effort you put to create such a great informative website. Your work is truly appreciated around the clock and the globe. tìm việc làm partime 365

    ReplyDelete
  7. Re-define your job hunt in terms of alternative possibilities. Successful job hunters always have alternative plans ready in the background and implement them at the first sign of difficulty.Top 10 Hr recruitment Companies in India

    ReplyDelete
  8. I am impressed by the information that you have on this blog. It shows how well you understand this subject. job board

    ReplyDelete
  9. Your blog provided us with valuable information to work with. Each & every tips of your post are awesome. Thanks a lot for sharing. Keep blogging, job posting

    ReplyDelete
  10. Thanks for an interesting blog. What else may I get that sort of info written in such a perfect approach? I have an undertaking that I am just now operating on, and I have been on the lookout for such info. job posting site

    ReplyDelete
  11. I visit your blog regularly and recommend it to all of those who wanted to enhance their knowledge with ease. The style of writing is excellent and also the content is top-notch. Thanks for that shrewdness you provide the readers! job posting

    ReplyDelete
  12. Thanks for the blog loaded with so many information. Stopping by your blog helped me to get what I was looking for. job posting site

    ReplyDelete
  13. I learn some new stuff from it too, thanks for sharing your information.
    Jobs in Dubai

    ReplyDelete