devroom.io/drafts/2006-12-13-show-the-current-svn-revision-in-your-rails-app.md
Ariejan de Vroom dbae98c4c0 Moar updates
2013-03-24 14:27:51 +01:00

60 lines
2.3 KiB
Markdown

---
title: "Show the current SVN revision in your Rails app"
kind: article
slug: show-the-current-svn-revision-in-your-rails-app
created_at: 2006-12-13
tags:
- General
- Everything
- RubyOnRails
- Features
- Subversion
- Capistrano
---
I'm current developing a Rails application. I deploy this application to a demonstration server using <a href="http://manuals.rubyonrails.com/read/book/17">capistrano</a>.
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.
<!--more-->
First of all, I created a partial that contains the revision number and render this in my layout.
app/views/layouts/_revision.rhtml:
<pre lang="bash">
CURRENT
</pre>
This shows CURRENT always when I work with my working copy.
app/views/layouts/mylayout.rhtml
<pre lang="ruby">
< %= render :partial => 'layout/revision' %>
</pre>
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:
<pre lang="ruby">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"
end</pre>
Next, I added the following hook 'after_update_code'. This will automatically be run after update_code which is called in 'deploy'.
<pre lang="ruby">desc "Run this after update_code"
task :after_update_code do
publish_revision
end</pre>
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.
<h3>Bonus</h3>
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.