tags:

views:

795

answers:

2

I am using capistrano to deploy a RoR application. The codebase is in a git repository, and branching is widely used in development. Capistrano uses deploy.rb file for it's settings, one of them being the branch to deploy from.

My problem is this: let's say I create a new branch A from master. The deploy file will reference master branch. I edit that, so A can be deployed to test environment. I finish working on the feature, and merge branch A into master. Since the deploy.rb file from A is fresher, it gets merged in and now the deploy.rb in master branch references A. Time to edit again.

That's a lot of seemingly unnecessary manual editing - the parameter should always match current branch name. On top of that, it is easy to forget to edit the settings each and every time.

What would be the best way to automate this process?

Edit:

Turns out someone already had done exactly what I needed.

A: 

General answer:

If you have a setting file with a content modified from environment to environment, you should make that line as a "template" (with a string representing variable name like @BRANCH_NAME@ or @ENV_NAME@).

Then you would have a (versioned) script able to read your config file, and replace the "@BRANCH_NAME@" variable by the appropriate value needed by your deployment process.

VonC
See also on the same kind of issue: http://stackoverflow.com/questions/1429256/how-to-track-config-files-of-submodules-in-git/1429288#1429288, http://stackoverflow.com/questions/487753/how-to-realise-a-deployment-branch-in-git/1214509#1214509, http://stackoverflow.com/questions/6009/how-do-you-deal-with-configuration-files-in-source-control
VonC
+2  A: 

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

naven87