Set default stage with Capistrano 3

20,158

Solution 1

Capistrano v3 is somewhat of a wrapper around Rake, so you need to realize that what's really happening is that a production task is getting run first, followed by a deploy task.

If you debug it a little, you'll find that deploy.rb doesn't get loaded when you don't type in a stage. This is because the stage's task is where deploy.rb gets loaded: Looking at lib/setup.rb, a task is defined for each stage. When run, the stage's task sets :stage, loads up the capistrano defaults, and then finally loads your deploy.rb file.

So, an easy trick would be to tell Capistrano to invoke the stage task every time you run cap by adding this to the end of your Capfile (not your deploy.rb):

Rake::Task[:production].invoke

or, using the invoke method from Capistrano's DSL:

invoke :production

This may have some unintended consequences if you actually do use multiple stages, but if you only ever use the production stage, it should work fine.

Another easy solution could be a simple shell alias, such as alias cap='cap production', but it might not work great if you have multiple projects with different stage names.

Solution 2

After I cd into the RAILS Root directory, issuing the command:

cap development deploy

seems to work. Earlier I was in the app/models folder and issuing the command came back with this error:

Stage not set, please call something such as cap production deploy, where production is a stage you have defined.

Solution 3

The old solution works for me in Capistrano 3:

cap --version
#=> Capistrano Version: 3.3.5 (Rake Version: 10.4.2)

At the very top of the Capfile after these lines

# Load DSL and Setup Up Stages
require 'capistrano/setup'

add:

set :stage, :production

and then run you task as usual without the stage specified:

cap foo:bar

Solution 4

New answer for capistrano 3.6+: It's better to use invoke :production unless Rake.application.options.show_tasks to avoid the warning which you would otherwise get with cap -T

Share:
20,158
Brad Dwyer
Author by

Brad Dwyer

Full stack developer working on democratizing Computer Vision. Previously founder of Hatchlings, a social games company that got over 10 million users and augmented realit mobile apps (including Magic Sudoku, Product Hunt's AR App of the Year).

Updated on August 13, 2020

Comments

  • Brad Dwyer
    Brad Dwyer almost 4 years

    Is there a way to set a default stage in Capistrano 3?

    I've tried putting set :stage, :production inside deploy.rb but that didn't work, it gives the error:

    Stage not set, please call something such as `cap production deploy`,
    where production is a stage you have defined
    

    I only have one stage right now so I want to be able to just run cap deploy and have it execute on the default.

  • Brad Dwyer
    Brad Dwyer over 10 years
    Thanks, this is very helpful. I've never used Rake before but thinking it may be helpful to read the Rake docs to better understand capistrano 3.
  • Gupta
    Gupta about 9 years
    what did you mean by cap foo:bar ?
  • Peter
    Peter about 9 years
    It is just an example name of a task. For example of example (!) it could be cap deploy:migrate or cap cache:clear.
  • Mark Robinson
    Mark Robinson over 5 years
    Had to add this to the end of the Capfile not near the start.