How to make staging env on rails

2024-05-25 ruby rails

By adding config/environments/staging.rb, you can define custom environment. However, especially for staging env, I'd like to use the same setting as production(because it is a test site for production). So here's a solution to use production setting as much as possible.

# basically the same as production
load(Rails.root.join('config','environments',"production.rb"))

# override only server specific settings
Rails.application.configure do
  config.action_mailer.default_url_options = { host: 'myapp-stg.example.net', protocol: "https" }
end

When the new environment was defined, also need to define database setting. Here's how to share the common parameters among several environment.

pg: &pg
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000
  host: localhost
  username: 
  password: 

staging:
  <<: *pg
  database: myapp_staging

production:
  <<: *pg
  database: myapp_production