easily show current version number of your app, stackoverflow style

When your app is deployed in multiple environments (staging, production), knowing the version number of your deployed app helps a lot, in debugging. Stackoverflow does a great job of showing a meaningful version in it’s footer. Currently it shows that it’s version number as rev 2011.7.22.2. This tells us that the code running stackoverflow was last updated on 2011.7.22, and that it was updated twice on that same day.

You can set up a similar thing pretty easily if you are using git and rails (rails in not really needed, but my example shows it using rails). All you need to do is add the following line to your config/application.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#config/application.rb
module Khalid
class Application < Rails::Application
.
.
.
#cache the version as long as the app is alive
#2011.07.25.4c76f53
VERSION =`git --git-dir="#{Rails.root.join(".git")}" --work-tree="#{Rails.root}" log -1 --date=short --format="%ad-%h"|sed 's/-/./g'`
.
.
.
end
end

This gives you a constant called Khalid::Application::VERSION which will give you a nice version number containing the commit sha id and the date like this: 2011.07.25.4c76f53


I am currently working on Zammu which makes Automatic Deployment of static websites to Github Pages very easy. I would love to get your feedback on it, Use the invitation code KHAJA