Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set conditional variables in capistrano's deploy.rb

Tags:

capistrano

Snippet from deploy.rb

task :prod1 do
  set :deploy_to, "/home/project/src/prod1"
end

task :prod2 do
  set :deploy_to, "/home/project/src/prod2"
end

I have 2 tasks like the above. Now instead of manually running either "cap prod1 deploy" or "cap prod2 deploy", I want to create a task "prod" which sets the required "deploy_to" based on the existence of a file on the server.

something like:

task :prod do
  if (A_FILE_IN_SERVER_EXISTS)
    set :deploy_to, "/home/project/src/prod2"
  else 
    set :deploy_to, "/home/project/src/prod1"
end

How do I do that?

like image 345
Saurav Shah Avatar asked Apr 03 '12 15:04

Saurav Shah


1 Answers

You can do that like this:

task :set_deploy_to_location do
  if capture("[ -f /etc/passwd2 ] && echo '1' || echo '0'").strip == '1'
    set :deploy_to, "/home/project/src/prod2"
  else
    set :deploy_to, "/home/project/src/prod1"
  end
  logger.info "set deploy_to = #{deploy_to}"    
end

This will do what you need. You can hook this method using before and after hooks like this:

before :deploy, :set_deploy_to_location
like image 79
Szymon Jeż Avatar answered Sep 30 '22 12:09

Szymon Jeż