In deploy.rb
, I believe that :rails_env
is typically set by require 'capistrano/rails'
found in your Capfile
. I am using rails, but not using the built in asset pipeline and therefore, don't use capistrano/rails and was in the predicament of having to manually set :rails_env
in deploy.rb
when switching between staging and production deploys. This seemed rather silly as the environment is always included when executing cap staging deploy
or cap production deploy
. I am posting this question, as it seems like it could be a fairly common pain point with a trivial solution, but after much googling, I came up empty-handed. I therefore dug into the Capistrano 3.1.0
source code to figure out how to access the stage variable entered on the command line and will follow up with a brief answer that does a bit of explaining.
The simple answer is to add the following line near the top of deploy.rb
:
set :rails_env, fetch(:stage)
The variable :stage
gets set in capistrano/setup
which is required in your Capfile
. This script creates rake tasks for each stage that is defined in config/deploy
. Inside the definition for the rake task you will find the following: set(:stage, stage.to_sym)
which is the stage/rails_env variable from your cap staging deploy
or cap production deploy
command.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With