jedi
jedi

Reputation: 2198

Capistrano - deployment Rails app to Staging

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

Upvotes: 1

Views: 1288

Answers (3)

murb
murb

Reputation: 1850

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.

Upvotes: 3

jedi
jedi

Reputation: 2198

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

Upvotes: 2

igor_rb
igor_rb

Reputation: 1891

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.

Upvotes: 0

Related Questions