1

I have a Rails app on production and I want to deploy my app to Staging using Capistrano before I roll out to production so my team has made a copy of the application on different host which is supposed to act as Staging. I have 2 separate Capistrano environment setups, one for production, the other one for staging. They actually differ by the IP address of the server, the rest is the same standard setup.

I have also added config/evironments/staging.rb file which is very similar to config.environments/production.rb with the only difference in

config.action_controller.asset_host

because on Staging I need to load assets from the staging host.

config/environments/production.rb

config.action_controller.asset_host = "my_production_host"

config/environments/staging.rb

config.action_controller.asset_host = "my_staging_host"

but after I do bundle exec cap staging deploy and inspect the browser console I can see an error 404 Failed to load resource and it points to the production host assets for some reason. 404 Failed to load resource

<link rel="stylesheet" media="all" href="https://my_prduction_host/assets/application-7d22d41de3a16146e566368364a8b2c769a9ebd68d1333e71d624250fa2fd187.css" />

so it seems it does not read my config/environments/staging.rb.

config/deploy/staging.rb

server "1.2.3.4", user: "my_user", roles: %w{app db web}, port: 50022

set :stage, :staging
set :rails_env, :staging

Capistrano logs shows current release: current (production)

config/environments/staging.rb is added to the repo, so why is it not reading my staging configuration? What am I missing? Any idea why it is running the app in production mode?

UPDATE

jedi
  • 2,003
  • 5
  • 28
  • 66
  • what server you are use? Unicorn? Puma? Can you add full capistrano deploy log? – igor_rb Nov 25 '18 at 20:06
  • I am using Passenger + Nginx. I'd rather not paste the deploy log as it contains sensitive information, such as IP addresses, etc. but they clearly say that the app is run in production mode for some reason. – jedi Nov 25 '18 at 20:16
  • check passenger_app_env value. More info here: https://www.phusionpassenger.com/library/config/nginx/reference/#passenger_app_env `Passenger sets the default value to production. ` – igor_rb Nov 26 '18 at 06:26
  • Nowhere in any nginx config file do I see `passenger_app_env` var – jedi Nov 26 '18 at 09:22
  • set it to staging in you nginx config file. – igor_rb Nov 26 '18 at 09:32

3 Answers3

3

Capistrano doesn't set the RAILS_ENV, you probably need to configure this at application-server level, e.g.:

 root /home/www/public/app-name/current/public;
 passenger_ruby /home/app-name/.rbenv/shims/ruby;
 passenger_app_env staging;
 passenger_enabled on;

(this here is a Passenger+nginx config, but similar settings will exist for other apps)

Here passenger_app_env tells Passenger to load the app in using the staging environment.

murb
  • 1,736
  • 21
  • 31
  • This didn't work. It broke the app completely, actually. I got 502 error code after accessing the page after deployment. – jedi Nov 25 '18 at 19:48
  • Sorry to hear that it broke the app. Not sure what you did wrong, maybe you copied the paths without changing them to match your situation, but the line that is important, `passenger_app_env staging;`, is equivalent to `rails_env staging;`, see: https://www.phusionpassenger.com/library/config/nginx/reference/#rails_env-rack_env – murb Nov 27 '18 at 12:52
  • Yeah, it didn't work. What did work was rails_env staging; This is not the first time I've seen wrong documentation on Passenger site. Their whole tutorial on how to install nginx with passenger is compeltely wrong and does not work at all. – jedi Nov 28 '18 at 00:14
2

I have figured it out. I had to add rails_env staging; to /etc/nginx/sites-available/my_site and restart Nginx.

jedi
  • 2,003
  • 5
  • 28
  • 66
0

You should add RAILS_ENV environment variable on you staging sever. See here for more info: https://askubuntu.com/a/58828/369247

To set variable only for current shell: VARNAME="my value" To set it for current shell and all processes started from current shell: export VARNAME="my value" # shorter, less portable version To set it permanently for all future bash sessions add such line to your .bashrc file in your $HOME directory.

igor_rb
  • 1,821
  • 1
  • 19
  • 34
  • That didn't work. Besides, Capistrano doesn't mention that RAILS_ENV should be set manually. They actually say to set it in deploy/#{environment}.rb file. – jedi Nov 25 '18 at 19:48