Thursday, February 23, 2012

Automating Jenkins job creation

So, here at work we've got a lot (and I mean a LOT) of git repositories.
We also got an underused build server which Gary and myself put up way, way back. Now my mission in life is to get this thing into peoples lives. If it saves one guy (such as myself) from forgetting to git add a new file he added, it would be worth it for that reason alone.
So, in any case, there are so many repos that it would be impossible to add them all by hand. Luckily, jenkins comes with a nice command line tool. So I though, I'll just write a script to import all the repos!
But then I saw, even better! They've got a REST API for creating jobs. Just go to http://your_jenkins_server/api to see the docs for it.
So in any case, this endpoint expects an XML file of the same format as it stores internally. So to get this format, I first used the "get-job" command on the Jenkins CLI to get an example XML file.
Then, the process seemed simple.
  • Get a list of all repos
  • Run through each of the repos, and clone them
  • If theres not a pom.xml, go to the next repo (its not buildable, thus doesn't belong on a build server)
  • If there is a pom, grab the groupId,artifactId and populate the job xml
  • POST the resulting job XML file to the jenkins server API
Sounds simple right? Well it was, actually. Guess what took the longest? Getting the damn groupId and artifactId from the XML file! For some reason, python's libxml2 didn't want to do XPath queries on the pom.xml, because of the super-awesome over-engineered XML namespace abortion. So, I had to resort to a hack to get the data from the XML. See if you can spot the hack in the code :)
Anyway, heres the code if anyones interested (and if we maybe find out it broke everything and need to reload everything :) )

No comments:

Post a Comment