Show SVN revision number in a Rails application

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”.

version 1.0.<%=render :partial => 'layouts/revision'%>

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.

desc "Write current revision to app/layouts/_revision.rhtml"
task :publish_revision do
  run "svnversion #{deploy_to}/current/app > #{deploy_to}/current/app/views/layouts/_revision.html.erb"

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

before 'deploy:restart', 'publish_revision'

Leave a Reply

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