8

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?

Andy Shinn
  • 26,561
  • 8
  • 75
  • 93

1 Answers1

1

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
Shyam Habarakada
  • 15,367
  • 3
  • 36
  • 47
  • Thanks for taking the time to respond to my old request. Unfortunately, I no longer do stuff with Ruby and Capistrano so I can't really test this. Do you think it makes sense to close my original question or just leave it open? I feel bad leaving it open indefinitely, especially since I hate seeing the same types of questions on stack overflow that linger forever. – Andy Shinn Jul 17 '15 at 15:44
  • No problem, and either closing or leaving it open works—but I am not 100% sure what works best for SO. – Shyam Habarakada Jul 31 '15 at 20:30
  • @pjammer might be able to verify the answer since he asked last month. – Shyam Habarakada Jul 31 '15 at 20:32
  • Minor correction - To use the variable `should_start_my_application`, we will have to fetch it in the rake task using `if fetch(:should_start_my_application) then...` – Deepak Sharma Sep 13 '21 at 14:22