How to Build an Authenticated Staging Environment with Heroku + Rails

Tadashi Shigeoka ·  Sun, October 16, 2011

Here’s how to build an authenticated staging environment with Heroku + Rails.

Heroku

Heroku allows you to operate web applications for free if the database is under 5MB, so it’s convenient to build a staging environment for testing before deploying to production.

Even if there are no problems in the local environment, sometimes things don’t work properly in Heroku’s production environment, so it’s better to test in a staging environment before deploying to production.

Add Staging Heroku App

$ heroku create myapp-staging --stack cedar --remote staging

With —remote staging, we’re setting the remote repository name to “staging”.

Create and Edit Configuration File staging.rb

Copy the production configuration file for the staging environment.

$ cp config/environments/production.rb config/environments/staging.rb

For the staging environment, change for development as follows:

■ config/environments/staging.rb

# Full error reports are disabled and caching is turned on
config.consider_all_requests_local       = true
config.action_controller.perform_caching = false

Change Heroku Environment Variables RACK_ENV/RAILS_ENV

Heroku’s environment variable RACK_ENV defaults to “production”, so change it to “staging”.

heroku config:add RACK_ENV=staging --app myapp-staging

If using Cedar stack to use Rails 3.1 series, change RAILS_ENV.

heroku config:add RAILS_ENV=staging --app myapp-staging

Deploy Staging App to Heroku

$ git commit -am 'create staging enviroment'
$ git push staging master

Database Migration

$ heroku rake db:migrate --app myapp-staging

For production environment migrations, we didn’t specify the application name, but when there are 2 or more remote repositories, you need to specify it.

So when executing Heroku commands, you need to specify “—app myapp” for production environment and “—app myapp-staging” for staging environment.

Implement Basic Authentication

app/controllers/application_controller.rb

class ApplicationController < ActionController::Base

  before_filter :password_protected if Rails.env.staging?

  protected

  def password_protected
    authenticate_or_request_with_http_basic do |username, password|
      username == "spam" && password == "spam_pw"
    end
  end

end

Authentication will only be required when the execution environment is “staging”. (Username: spam, Password: spam_pw)

That’s all.

Reference Information

Herokuで認証付きのステージング環境を構築する - exdesign (Building an Authenticated Staging Environment with Heroku - exdesign)

Herokuでステージング環境を作る - アインシュタインの電話番号☎ (Creating a Staging Environment with Heroku - Einstein’s Phone Number☎)

Herokuで作るFacebookアプリ:第7回 Herokuをもっと活用しよう!|gihyo.jp … 技術評論社 (Facebook Apps with Heroku: Chapter 7 Let’s Make More Use of Heroku! | gihyo.jp … Gijutsu-Hyoron Co.)

Heroku で複数の環境( production, staging など)を使い分けたいときー - LazyLoadLife (When You Want to Use Multiple Environments (production, staging, etc.) with Heroku - LazyLoadLife)

That’s all from the Gemba.