Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using capistrano to deploy from different git branches

Tags:

git

capistrano

People also ask

How do I deploy to another branch?

To where you wish to deploy. Run git checkout -b deployment origin/master . Make your changes (push them if you like). Whenever your master (or whatever branch you made the deployment from) has changes you want to deploy, simply git pull --rebase .

How does git work with multiple branches?

Git offers a feature referred to as a worktree, and what it does is allow you to have multiple branches running at the same time. It does this by creating a new directory for you with a copy of your git repository that is synced between the two directories where they are stored.


This works with Capistrano >= 3.1:

add this line to config/deploy.rb:

set :branch, ENV['BRANCH'] if ENV['BRANCH']

and then call capistrano with:

cap production deploy BRANCH=master

This solution works with Capistrano < 3.1:

# call with cap -s env="<env>" branch="<branchname>" deploy

set :branch, fetch(:branch, "master")
set :env, fetch(:env, "production")

Using Capistrano 3.1.0+, none of these were working for me anymore. Instead, according to their commented instructions:

   ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }

But, you don't want to use ask or it will prompt you. Instead you should use set. HEAD is the top most branch; 'edge' as it's called. If you want a different branch, replace HEAD with your branch name, eg: master, staging, etc.

To conclude with examples, in /config/deploy/production.rb, you might include this line:

   set :branch, proc { `git rev-parse --abbrev-ref master`.chomp }

...or

   set :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }

btw, HEAD is the default setting, so no need to really state that in file. Might be used better in a /config/deploy/edge.rb.

In /config/deploy/staging.rb, you might include this line:

   set :branch, proc { `git rev-parse --abbrev-ref staging`.chomp }

...or

   set :branch, proc { `git rev-parse --abbrev-ref test`.chomp }

You get the idea!

I hope these examples help future users of capistrano (^_^)


I can confirm that the below still works in Cap 3.11.0 13/10/18 as well as Cap 2:

In deploy.rb / stage.rb:

set :branch, ENV['BRANCH'] || 'develop'

On the command line:

cap deploy BRANCH=featurex

This gives you a default branch (which could be different for different environments), and the ability to change branches when you want.


With multistage, it's actually now:

cap production deploy -s branch=my-branch

The previous post syntax does not work in my environment


Alternatively you could structure it from the command line where you have a default branch and environment and also you are able to pass parameters to the cap call which could include the environment and the branch to use. This could be a branch that is explicitly passed or you could have a parameter which would indicate current branch as described in the link you listed.

#call with cap -S env="<env>" branch="<branchname>" deploy
...

# Prevents error if not parameter passed, assumes that default 'cap deploy' command
# and should deploy the master branch to the production server
set(:env, ‘production’) unless exists?(:env)
set(:branch, ‘master’) unless exists?(:branch)

if !env.nil? && env == "production"
   role :web, "production_ip_address"
else   # add more as needed 
   role :web, "development_ip_address"
end

if !branch.nil? && branch == "current"
   set :branch, $1 if `git branch` =~ /\* (\S+)\s/m
elsif !branch.nil?
   set :branch, branch
else   # add more as needed 
   set :branch, "master"
end
...

Code example borrowed heavily from here