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


18 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
  14. I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. In fact your creative writing abilities has inspired me to start my own BlogEngine blog now. Really the blogging is spreading its wings rapidly. Your write up is a fine example of it. Buscar ofertas

    ReplyDelete
  15. Although you now know what you want to do, the jobs market may at that point in time not want those exact skills, in that search geography, for the pay level which makes economic sense to you.Cover letter

    ReplyDelete
  16. The second explanation involves a person accepting a job they know is not a good match, and hoping it will become something else in time. For example, kuala lumpur vs Singapore they have more experience than the job requires but the employer only matches them to an entry-level position.

    ReplyDelete
  17. I recently found many useful information in your website especially this blog page. Among the lots of comments on your articles. Thanks for sharing. https://europa-road.eu/hu/gepszallitas-budapest.php

    ReplyDelete
  18. I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it. romani in uk

    ReplyDelete