Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you access rails_env from the Cap deploy command?

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.

like image 710
jearlu Avatar asked Apr 11 '14 17:04

jearlu


1 Answers

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.

like image 122
jearlu Avatar answered Nov 18 '22 00:11

jearlu