Show the current SVN revision in your Rails app
13 Dec, 2006

I’m current developing a Rails application. I deploy this application to a demonstration server using capistrano.

To streamline feedback and bug reporting I want to show the current revision number of the code that’s published on the demo server to show in the footer of every page.

First I looked into Subversion keyword expansion, but this is marked as ‘evil’ and it doesn’t meet my requirements. I want to show the latest revision number of the entire repository and not just that of the current file.

Luckily for me, I use capistrano. Here’s how I fixed the problem.

First of all, I created a partial that contains the revision number and render this in my layout.

app/views/layouts/_revision.rhtml:

CURRENT


This shows CURRENT always when I work with my working copy.

app/views/layouts/mylayout.rhtml

  < %= render :partial => ‘layout/revision’ %>


Now, I assume you have already setup capistrano for your project and that you have a config/deploy.rb file.

I’ve added the following helper to my config/deploy.rb file:

desc "Write current revision to app/layouts/_revision.rhtml"
run "svn info #{release_path} | grep ^Revision > #{release_path}/app/views/layouts/_revision.rhtml"
end

Next, I added the following hook ‘after_update_code’. This will automatically be run after update_code which is called in ‘deploy’.

desc "Run this after update_code"
end