How to set conditional variables in capistrano deploy.rb

Excerpt 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 as above. Now, instead of manually starting "cap prod1 deploy" or "cap prod2 deploy", I want to create a task "prod" that sets the required "deploy_to" based on the existence of the file on the server.

sort of:

 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 to do it?

+6
source share
1 answer

You can do it 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 use this method using before and after such as:

 before :deploy, :set_deploy_to_location 
+10
source

Source: https://habr.com/ru/post/912325/


All Articles