Show SVN revision number in a Rails application

June 7, 2012

This is a hack to display the Subversion revision in a Rails application using Capistrano to insert the current revision number on each deploy.

Create a partial that will contain the revision number. For example, _~/app/views/layouts/revision.html.erb. You can put anything in that file, as it will be overwritten by Capistrano on each deploy. The word “Development” or “Current” seems a good choice.

In the page where you want to display the revision number (probably in the application layout), render the partial. In development mode, you’ll see something like “version 1.0.Development”. In your deployment environment, you’ll see the SVN revision number, like so: “version 1.0.504”.

To set up Capistrano, edit the ~/config/deploy.rb file (assuming you are already using Capistrano for deployments). Add a task to get the SVN version and write that to your revision partial. For some reason, pointing to the “/current” directory doesn’t work – probably because of the way Capistrano links directories. Therefore, just point SVN to any subdirectory or file to get the version.

And tell Capistrano to run this task just before restarting the application: