Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Capistrano task based on environment or server properties?

I have a namespace and a couple tasks in the namespace that run after deploy:updated. Here is an example:

namespace :myservice do
  task :start do
    on roles(:app) do
      sudo :start, "my/application"
    end
  end
end

I'd love for one of these tasks to only run on a certain environment or host property. How can I accomplish this?

I'd love to be able to filter on environment such as:

namespace :myservice do
  task :start do
    on roles(:app), env(:vagrant) do
      sudo :start, "my/application"
    end
  end
end

What is the best way to accomplish this?

like image 867
Andy Shinn Avatar asked Apr 09 '14 21:04

Andy Shinn


1 Answers

It seems like capistrano multi-stage would help you. https://github.com/capistrano/capistrano/wiki/2.x-Multistage-Extension

Essentially, you would have a stage called vagrant where you might define configuration variables, which would then be referenced by your main deploy.rb script(s) and acted on.

Here is a conceptual example,

# config/deploy/production.rb
set :should_start_my_application, false


# config/deploy/vagrant.rb
set :should_start_my_application, true


# config/deploy.rb
namespace :myservice do
  task :start do
    on roles(:app) do
      if should_start_application then
        sudo :start, "my/application"
      end
    end
  end
end
like image 126
Shyam Habarakada Avatar answered Nov 02 '22 16:11

Shyam Habarakada