embedding the git/hg/svn’s revision number inside a grails application

When your application goes to production, you should be prepared for handling bugs. Users and customers will find them and hopefully report them. I’ve been multiple times in a situation where a well written bug report including a stacktrace doesn’t help me much because the stacktrace doesn’t match the current development state of the code. Therefore it simplifies life if the bug report contains a build number or a SCM revision number. With this small recipe, you can easily add this information to your application.

Find out your current SCM revision

Every version control system does this differently, so here’s a short summary of what command might be used for what SCM:

SCM command to get revision information
Mercurial hg id -i -n -b -t
Git git rev-parse HEAD
Subversion svnversion

If your SCM of choice is not listed and you know the command, please let me know by posting a comment.

Create a template that contains the revision information

For the following, I’ll use mercurial, it should be easy to adopt this to the SCM of choice. Using a Gant-Script scripts/_Events.groovy you could hook into the build process and easily generate a Grails template that just contains the output of one of the above commands.

This recreated grails-app/views/_version.gsp upon each build, so we’re sure to have always the most recent revision ids there. It is crucial that the file containing the revision information is itself not under version control. To exclude this file you have to list it in .hgignore, .gitignore or use svn propedit svn:ignore.

Using the _version.gsp template

The final step is to embed the previously created _version.gsp somewhere in your code. For my usecase, I want to have the revision ids available on each and every page, so I’ll put it into the layout template grails-app/views/layout/main.gsp:

Be sure not to omit the leading “/” in the g:render tag, otherwise _version.gsp gets not found in case of a deeper nested view path.

4 thoughts on “embedding the git/hg/svn’s revision number inside a grails application

  1. GrailsUser

    Thanks for the info. Included it into a project using a slightly modified version:

  2. OverZealous

    Great idea! A cleaner version might look like:

    new File("grails-app/views/_version.gsp").text = "hg id -i -b -t".execute().text

    There’s no reason to keep the Process object around (since we don’t care about the output), and the getText() method on input streams automatically waits for the process to complete. Also, using File.setText automatically closes the file, and overwrites the previous contents.

    Finally, I removed the local revision number (-n) from the mercurial output, since that number is effectively meaningless (it can change for each local repository or clone). That’s a matter of personal preference, of course.

    For a complete version, I’d be tempted to put the name of the version file and the revision command string in a configuration option, so they could be referenced in a generic manner.

  3. Pingback: An Army of Solipsists » Blog Archive » This Week in Grails (2011-29)

Leave a Reply

Your email address will not be published. Required fields are marked *