Show the current SVN revision in your Rails app

| Posted: - Last updated:
| Tagged general everything rubyonrails features subversion capistrano
| ~300wrds (~2min)

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.



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


  < %= 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"
task :publish_revision do
  run "svn info #{release_path} | grep ^Revision > #{release_path}/app/views/layouts/_revision.rhtml"

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"
task :after_update_code do

That’s it. When I deploy the application, the current code is checked out and and the layouts/_revision.rhtml file is overwritten with the current revision information.


You could also leave the layouts/_revision.rhtml files empty and update it for your demonstration server, but not for your production box. This way there won’t be a revision added.

Of course, you could also create a deploy_demonstration method in deploy.rb and call publish_revision manually from there.

My site is free of ads and trackers. I record privacy-respecting usage statistics with Fathom.

Was this post helpful to you? Why not ☕ Buy me a coffee or use Brave Browser to send a tip.