Pattern: Conditional Configuration
Posted by Jamis on Monday, July 14
Here’s a pattern that I’ve been using and seeing more and more often. The problem it solves is this: how do I configure Capistrano differently for some set of predetermined scenarios? The most common usage of it is when configuring Capistrano to deploy to different staging locations, and you want to configure each stage differently.
The cleanest solution simply involves creating a different task for each scenario. Within each task, you set variables, declare roles, and load files:
1 2 3 4 5 6 7 8 9 10 11 |
task :scenario_a do set :deploy_to, "/path/to/scenario_a" role :web, "scenario_a.web" role :app, "scenario_a.app" end task :scenario_b do set :deploy_to, "/path/to/scenario_b" role :web, "scenario_b.web" role :app, "scenario_b.app" end |
When you then go to invoke a task (like deploy), you simply invoke the scenario task you want first:
$ cap scenario_a deploy
Capistrano simply invokes the task in the same order they were specified, so in this case, it will first call scenario_a (which will set up the deploy_to variable and the roles) and then will call deploy, which will use the settings you gave.
It’s really a quite powerful pattern for conditional configuration.
Comments