Subscribe to RSS Feed

Elijah Miller

25 Apr 2009

Deploying Multiple Environments on Heroku

UPDATE: This post is outdated, check out Easy Deploys with Heroku San.

So you followed yesterday's guide on getting your application deployed to Heroku, and now you've fallen in love with it. Now you need to setup both a production and staging environment that you can easily deploy your application to.

So far Heroku only runs code in one branch, so we'll work around this by creating two remote repositories that we can push to.

For example simplicity, we'll be pushing all code out of the same git branch. You'll probably be pushing staging and production servers from different branches, adjust accordingly.

Create your servers and fix your remotes

Application names have to be unique on Heroku, so make sure to replace myapp with your application’s name. You can do that automatically by clicking here.

heroku create myapp-staging --remote staging
heroku create myapp-production --remote production

Since we'll be pushing to two applications we are using the --remote argument to make two sensibly named remotes.

Environment specific variables

Heroku has a nice interface for setting up application specific settings, but I will assume your application configures itself according to the RAILS_ENV variable. Check out Heroku's docs if you need more control.

If you run your staging server in the production environment, you can skip this step.

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

Deploy and migrate

You'll be doing this next command pretty often. We push the current branch to the staging server's master branch. Since this is the first deploy we'll need to do the same thing for production and run all the migrations.

git push staging master
git push production master

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

heroku open --app myapp-staging
heroku open --app myapp-production

Done

Celebrate! You now have two servers running on Heroku with different databases and accessible through domains. Seriously, how long did that take?

Caveats

The heroku command seems to detect the application name by looking through git's remotes for remotes that are located at heroku.com. This means it will get confused by the multiple entries we created. You will need to work around this by passing --app myapp-staging after the normal heroku command.

Troubleshooting

Make sure you followed my original guide on getting your application deployed to Heroku.