Recently I blogged about our approach to using executable jar/war files for deployment. As part of this we came up with an interesting method of handling build numbers. We did not want to be restricted by things like incremental numbers since they can get weird if you branch (which for this app we do not). What we decided to do was to use a combination of the git SHA and the date. The SHA is sufficient for telling us the head of the repository for a specific build. The date portion just makes it more human readable with the additional ability of being able to group builds by day. Below is the ruby code we use within buildr to calculate our version number:

require 'date'

def calculate_version_name
  date_string = Date.today.strftime("%G.%m.%d")
  sha = `git log -n 1 --pretty=format:'%h'`
  "#{date_string}-#{sha}"
end

This ends up producing a build number that looks something like this – 2011.10.25-836ab79

The only drawback we’ve seen from this approach is that the build numbers cannot be easily sorted chronologically, but so far we haven’t needed that.

What do people think?