Thursday, October 08, 2009

Adding Hudson and SVN revision number into Grails application properties

Wow, it's been absolute ages since I last posted an entry here. Oh well, better late than never. In looking for an excuse to post something, I thought I'd add the strategy I came up with to incorporate both the Hudson build number and our SVN revision number into our Grails application.properties. From that line you can tell, obviously, we use Hudson for our build automation.

This build number insertion approach enables us to easily identify from both a test deployment or a customer deployment which Hudson build was used, and which SVN revision number was used simply by looking at the app.version that I show in the footer.gsp of our app.

To do this is fairly simple. It requires 3 simple steps including 2 changes to your build.xml file that Grails generates for you.
  1. In my project files, my application.properties looks as follows:

    app.version=3.3

    ... or whatever major minor version your app is on. Ours is currently at 3.3.

  2. In my build.xml file I added a task:

    <target name="buildnumber">
    <property environment="env"/>
    <replaceregexp file="${local.dir}/application.properties"
    match="app.version=(.*)"
    replace="app.version=\1.${env.BUILD_NUMBER}.${env.SVN_REVISION}"
    byline="true"/>
    </target>

    This task is run as a depends of prod.war. ${local.dir} is actually mapped to '.' in my build file properties, so you can always just make it "./application.properties" in the task above.

  3. prod.war task is changed as follows:

    <target name="prod.war" depends="buildnumber" ...
So what happens is that when Hudson triggers the build, the 'buildnumber' task in 2 above appends both the Hudson build number and the SVN revision number to the app.version number before Grails builds the WAR file.

This way, we know which Hudson build is being used, and which SVN revision was used in that build. This helps us in both QM and out in the wild when a customer reports a problem. Also, with SVN we never have to tag a build because the revision number is the tag. If we need to make bug fix changes we can branch from a revision and we're good to go.

No comments: