Recipe: Accessing Your Subversion Revision in Rails

Have you ever looked at a Rails web site and wanted to know precisely which code revisions were deployed? If you know that a certain fix was implemented in Revision 436, how can you verify whether that fix has been deployed to production? This recipe shows how to quickly and easily solve this problem.

Problem: You want to access the master revision number for your Subversion repository and then make that number available to your Rails application.

Assumptions: Clearly, this recipe assumes that the source code for your Rails site is being stored in a Subversion repository. If you're using GitHub, or a similar service, then you're going to have to solve this problem in a different way, i.e. - that would be a different recipe.

Solution

The first thing that you might think of when solving this problem is Subversion's keyword substitution support. Subversion provides the same kind of keyword substitutions as RCS, an earlier source code management system (and one largely supplanted by Subversion).

In Subversion, a keyword such as "Revision" can be placed in a file, delimited by "$" characters, e.g. — "$Revision$". If keyword substitution is turned on for the file, then the keyword will be replaced with updated content each time a user checks out a new working copy that includes that file. Keyword substitution for the "Revision" keyword can be turned on by:

   $ svn propset svn:keywords "Revision" myfile.rb

Unfortunately, this doesn't quite produce the intended result. The Revision number substituted into the file is the highest revision number in which that particular file was last changed. It's not the latest revision for the codebase, the master revision number, which is what's needed.

And no, Subversion doesn't provide a keyword to get that master revision number.

We're going to have to go outside of Subversion to get that master revision number. The following shell script shows how to access the master revision number and make it available to a Rails application:

Listing 1: A Simple Shell Script

   #!/bin/ksh

   WEBAPPS_DIR="/home/dkeener/webapps"
   WEBAPP="keenertech"
   DEST="${WEBAPPS_DIR}/${WEBAPP}"
   REPO="http://svn.keenertech.com/code"

   echo "-- Getting the master revision number..."
   REV=`svn info ${REPO} | grep "^Revision" | awk '{print $2}'`
   echo "-- Placing (Rev = ${REV}) in subversion.rb file..."
   echo "SVN_REVISION=${REV}" > ${DEST}/config/initializers/subversion.rb

The shell script uses the "svn info" command to retrieve summary information about the repository, which is then parsed to extract the revision number. The shell script writes the revision number to the subversion.rb file in the initializers directory, essentially defining a constant called SVN_REVISION. This constant can then be referenced anywhere in the Rails application, e.g. - a view could display the revision number in a page footer or within an administrative area of the site.

This solution carries with it an implicit assumption that the shell script will be called during the production deployment process. This shell script should be executed after the source code has been exported from Subversion and before the Rails site is started.

While this solution was implemented as a UNIX shell script, it should be obvious that the solution could easily be morphed into Ruby code, such as a rake task or a Capistrano recipe.

To support other environments, such as development and test, the Subversion repository should include a default version of the subversion.rb file, which will be overwritten with real information during a production deployment.



Comments

No comments yet. Be the first.



Leave a Comment

Comments are moderated and will not appear on the site until reviewed.

(not displayed)