Tuesday 18 March 2014

Rails: Staging environment on Heroku

A note to myself for everytime I have to do this

Consider you already have a rails app on Heroku but you now want to incorporate a staging environment with your workflow

Local changes first:

Copy the config/environment/production.rb file to config/environment/staging.rb Change the action mailer setup for Exception Notification so you know the email is coming from your staging app.

Add a staging group to your gemfile (this can normally be a copy of the production group)

Change the config/newrelic.yml file to uncomment the app_name under staging

Heroku changes:

From your console in you project directory on your development machine:

heroku apps:create staging-appname

This creates a new heroku app that is also linked to this project.

git remote add staging git@heroku.com:staging-appname

This adds the git handle of staging to this app. You can see the different git remotes with:

git remote -v

So when you want to push your development branch to your staging environment you do:

git push staging development:master

You also have to run your migrations (from now on you have to specify the app name):

heroku run rake db:migrate --app staging-appname

Your staging app will be set to production by default so you have to set it to staging environment:

heroku config:set RACK_ENV=staging RAILS_ENV=staging --remote staging

You can now check if it done by doing:

heroku config --app staging-appname

Dont forget to change the default password on Active Admin!

You can now do new feature testing, automated cross browser testing, automated db population and load testing to your hearts content without affecting your real app, its data or its users.

Your staging environment is now complete

When you are happy that the staging app is behaving as intended you can first merge the development branch into master and then delete the development branch:

git checkout master
git merge development
git branch -d development

Now you can push master to heroku (production app)

git push heroku master

Your production app is now updated!