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:

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

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.

  • Twitter
  • Digg
  • del.icio.us
  • DZone
  • Reddit
  • email

9 Responses to “Show the current SVN revision in your Rails app”

  1. Bitbutter says:

    Thanks for this write-up! i was thinking about the best way to do this very thing.

  2. That seems… complicated. I use svnversion to get that info. It’s a bit slow though, so I cache the result. This is from my application.rb:

    def build_number
    if RAILS_ENV == ‘production’
    if !defined?(@@build_number)
    svnversion = `svnversion #{RAILS_ROOT}`
    svnversion =~ /:?(\d )[MS]?$/
    @@build_number = $1
    end
    @@build_number
    else
    ‘BUILD’
    end
    end
    helper_method :build_number

  3. Ariejan says:

    Your version has to run subversion every time you request the active revision.

    My script only does that once so there would be a performance advantage to my version.

  4. Alexander says:

    Hi,

    since ’svn info’ output is pretty much YAML, I simply do the following:

    deploy.rb:
    task :publish_revision do
    run “svn info #{release_path} > #{release_path}/config/appversion.yml”

    and then in the app: read this file as a hash:

    environment.rb:
    APP_VERSION = File.file?(‘config/appversion.yml’) ? YAML.load_file(‘config/appversion.yml’) : “”

    in the view:

    < %= APP_VERSION %>

    and so on :)

  5. jc says:

    Capistrano now seems to give you a “revision” variable for free.

    This is what I’m doing:
    run “echo \”REVISION:#{revision}\” > #{release_path}/app/views/shared/_version.rhtml”

  6. [...] Ariejan.net » Show the current SVN revision in your Rails app Shared with shareomatic.com (tags: rails svn) [...]

  7. Elwyn says:

    Great article and comments, really helpful. However, what you actually need is the following if your using capistrano’s “free” revision number.

    run “echo \”REVISION:#{real_revision}\” > #{release_path}/app/views/shared/_version.rhtml”

    revision will just spit out a useless “HEAD”

    :-)

  8. dpbmcvtk says:

    oATFeL qxwqlyzphvln, [url=http://lilmpgqnpxad.com/]lilmpgqnpxad[/url], [link=http://jzsiydleorsg.com/]jzsiydleorsg[/link], http://qrvrnmmuqdfw.com/

  9. Guys, try my Rack middleware for showing revision info on demo/staging servers. It allows to completely remove revision-displaying code from your app’s layout and/or partials (which shouldn’t be there at all). Check it out at http://sickill.net/blog/2009/06/01/rack-middleware-showing-git-or-svn-revision.html

Leave a Reply