本番と同一の環境で色々試したいので作ることに。というか最初からやっておくべきだった。
Staging用にHerokuAppを作る
$ heroku create yourapp-staging --remote staging
環境設定ファイルをコピー
$ cp config/environments/production.rb config/environments/staging.rb
適宜設定を変えるのも良いと思います。
Herokuの環境変数を設定
$ heroku config:add RACK_ENV=staging RAILS_ENV=staging --remote staging
git pushする際のキーワードを変更
git push heroku master
とかしていると自分がどこにpushしてるのか分かりにくく感じたので、.git/configを編集しました。
.git/config
# [remote "heroku"]
# を下に置換
[remote "production"]
これで本番環境はgit push production master
、staging環境はgit push staging master
で出来るようになった。
Staging環境に鍵をかける
あんまり人に見られる物でもないので、Basic認証をかけます。
app/controller/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 == "name" && password == "password"
end
end
end
追記
コメントで頂いた記法(Rails3.1以降)では簡潔に書けるようになってるみたいです
app/controller/application_controller.rb
class ApplicationController < ActionController::Base
http_basic_authenticate_with :name => "name", :password => "password" if Rails.env.staging?
end
綺麗でいいですね。
その他
AmazonS3等を利用する際は同一のサーバを使うので、違うKeyを使うなどして対応します。
$ heroku config:add S3_KEY=XXX --remote staging
$ heroku config:add S3_SECRET=YYY --remote staging