February 16, 2013

deploying with mina & current git revision

At the time of writing mina does not support writing a REVISION file on deployment, like capistrano does by default. Mina also deletes the .git directory at the end of each deployment, leaving you no option to access your git log to retrieve the currently deployed revision (note that this might change in the future).

Luckily, most software that depends on your app running out of a git revisioned directory, like for example squash, support fallback modes which rely on your REVISION file existing somewhere.

So here’s a simple work around to create a REVISION file on deployment:

# inside your deploy.rb, right after set :branch
set :deployed_revision, %x[git rev-parse #{branch}].strip

# snip …

task :deploy => :environment do
  deploy do
    invoke :'git:clone'
    invoke :'deploy:link_shared_paths'
    invoke :'bundle:install'
    invoke :'rails:assets_precompile'

    to :launch do
      queue "echo '#{deployed_revision}' > #{deploy_to}/#{current_path}/REVISION"
      queue "touch tmp/restart.txt"
    end
  end
end

This should work great for regular deployments.

Using Jenkins for your staging deployments?

The above example won’t work out of the box if you are using Jenkins to deploy your application. Luckily, jenkins exports the current revision to your environment as GIT_REVISION.

Add the following right after set :deployed_revision:

if ENV['GIT_COMMIT'] != nil && ENV['GIT_COMMIT'] != ''
  set :deployed_revision, ENV['GIT_COMMIT']
end

That’s it. Now you have a REVISION file inside your current directory. Easy!

© Raphael Randschau 2010 - 2022 | Impressum