Herokuをステージング環境として使うときのTIPS
http://devcenter.heroku.com/articles/multiple-environments
ここに書いてあるよ!
heroku create --stack cedar --remote staging
stackがcedarはrails3.1以上のナウい人向け。see:http://devcenter.heroku.com/articles/rails31_heroku_cedar
あとは
git push staging master
すれば別環境にアップできます。
※sshのconfigも見てみるといいよ!
ただ、これだけだと本番とあんま変わらない。たぶん、ステージング用意する人はベーシック認証かけたりとか本番と変化つけたいケースがほとんどと思う。
そのためにはまずconfig/environments/staging.rbを作る。production.rbをコピーしとけばいいと思うよ。
で、ベーシック認証部分実装
class ApplicationController < ActionController::Base protect_from_forgery before_filter :password_protected if Rails.env.staging? private def password_protected authenticate_or_request_with_http_basic do |username, password| username == "hoge" && password == "fuga" end end end
でHerokuの環境変数をstagingにする。
まず確認。
$ heroku config --app app_name DATABASE_URL => postgres://xxxxxxxxxxxxx GEM_PATH => vendor/bundle/ruby/1.9.1 LANG => en_US.UTF-8 PATH => bin:vendor/bundle/ruby/1.9.1/bin:/usr/local/bin:/usr/bin:/bin RACK_ENV => production RAILS_ENV => production SHARED_DATABASE_URL => postgres://xxxxxxxxxxxxx
RACK_ENVとRAILS_ENVがproductionになってる。
ここにAspenと Bamboo stacksではRACK_ENVは無視されるって書いてある(たぶんcedarも)からRAILS_ENVだけ変えときゃいいと思うけど、とりあえずRACK_ENVも変えとく。
$ heroku config:add RACK_ENV=staging --app app_name Adding config vars and restarting app... done, v6 RACK_ENV => staging $ heroku config:add RAILS_ENV=staging --app app_name Adding config vars and restarting app... done, v7 RAILS_ENV => staging
で、確認
$ heroku config --app app_name DATABASE_URL =>postgres://xxxxxxxxxxxxx GEM_PATH => vendor/bundle/ruby/1.9.1 LANG => en_US.UTF-8 PATH => bin:vendor/bundle/ruby/1.9.1/bin:/usr/local/bin:/usr/bin:/bin RACK_ENV => staging RAILS_ENV => staging SHARED_DATABASE_URL => postgres://xxxxxxxxxxxxx
あと、DBにPostgreSQL使うならその対応も。
Gemfileに
group :staging do gem 'pg' end
config/database.ymlに
staging: encoding: unicode adapter: postgresql username: hogehoge port: 5432 host: ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com database: hogehoge password: fugafugafuga
usernameとかその辺はさっきのheroku configの結果のSHARED_DATABASE_URLを参照して書く。
で、pushした後に
git push staging master
migrate
heroku run rake db:migrate
cedarだとrunが必要らしい。というか必要。
これで、できあがり!