Rails Application Versioning

Back in the days of sub version forks used to use the revision number of the deployed branch or the truck to version the app. I remember stackoverflow.com used this strategy for sometime. Anyways getting back to topic, the QA folks at work wanted a version number for this web application that i am working on right now. Reason been that will allow them to keep track of the application and generally be useful with issue tracking and such.

I looked around for a standard ruby way of doing this. However i didn’t find any. There were some gems that try to handle things for you. Mostly by providing you some rake tasks to increment the major/minor/build…etc versions. But i wanted something that i can just setup and forget about. Like for example using the subversion revision number.

Right now i am using Git. And some folks were using the last commit’s hash as the version number. This is all cool but its not something thats incremental. This can be a problem for humans.

So at first i thought of setting up a git pre commit hook to automatically run a rake task to increment application version number. Pre commit because then i select the version file it self and include it in the commit (there for if i am displaying the version on my site, it will reflect the latest version whenever i commit and deploy my changes). This approach was very straight forward and i soon found my self battling rvm.

This particular application is hosted on heroku. And it turned out that heroku sets a version number internally for each deploy of the application. You can access this as shown bellow. I ended up using a combination of the last commit’s hash and the heroku deploy ‘name’/version for this rail’s application’s version.

[production.rb]
1
2
3
4
5
6
# Heroku API key is in the source!@@#! might not be such a good idea
  heroku_client = Heroku::Client.new '', 'your_api_key_here'
  last_deploy = heroku_client.releases('APP_NAME').last

  # Get the app version using a combination of the deploy version from heroku and the commit hash.
  config.app_version = "#{last_deploy["name"]} #{last_deploy['commit']}"
[development.rb]
1
config.app_version = "development version"

And i can now display the application version anywhere in my views.

[application.html.erb]
1
2
3
4
5
<div id="footer">
  <div class="container">
    <p class="muted credit"><%= Rails.configuration.app_version %></p>
  </div>
</div>

You can do something like the above if you are on heroku. The end result is something like this

v235 82ee972

It can look a bit weird for a normal user, specially the commit hash part. But the heroku deploy version look humane enough.

Comments