Use current Git branch for deploy in Capistrano

 Feb 25, 2013

I use Git Flow and Capistrano Multistage to manage deployment of my web apps, which means I can be deploying lots of different branches to our test server through out the day if we are doing patches and feature development intermingled with each other.

Originally I was just changing the branch name in the test.rb file each time I needed to deploy a different branch, but eventually I got fed up with it.

This little method will help deploy the currently checked out branch each time. Just add it to your deploy script

def git_branch
  `git symbolic-ref HEAD`.gsub(/^refs\/heads\//, '').chomp
end

Then just change the deploy recipe to use the method instead of a branch name

set :branch git_branch