Show the current SVN revision in your Rails app
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:
1
2CURRENT
This shows CURRENT always when I work with my working copy.
app/views/layouts/mylayout.rhtml
1
2 < %= 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:
1desc "Write current revision to app/layouts/_revision.rhtml"
2task :publish_revision do
3 run "svn info #{release_path} | grep ^Revision > #{release_path}/app/views/layouts/_revision.rhtml"
4end
Next, I added the following hook ‘after_update_code’. This will automatically be run after update_code which is called in ‘deploy’.
1desc "Run this after update_code"
2task :after_update_code do
3 publish_revision
4end
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.
Bonus
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.