Heroku: Managing multiple accounts and environments

Heroku is a fast and easy way to get your Rails app deployed to the web. No need to buy and configure your own Linux virtual machine to run Rails apps - Heroku takes care of that for you. Here I'm going to cover two problems that you will eventually face when working on web projects that are deployed to Heroku:

• How to use more than one Heroku account on the same computer

• How to create a staging site for seeing changes in a production-like environment

All of this information is available in more detail here and here. Hopefully this article will get you moving in the right direction. If not, follow the above links for a better overview.

Managing multiple Heroku accounts

Oddly enough, the ability to manage multiple Heroku accounts from the same machine is not baked directly in to the Heroku Toolbelt. However, a gentleman by the name of David Dollar has created a plugin that allows us to do just that.

I stumbled upon Heroku Accounts for the first time because another programmer added me as a contributor to an existing Heroku app using my work email and not my personal email. This is a common problem, and luckily it has an easy solution. Before getting started, go ahead and install the plugin:

$ heroku plugins:install git://github.com/ddollar/heroku-accounts.git

Since you've already been using Heroku, you should already have at least one Heroku account configured on your computer. In order to get your existing account into Heroku Accounts, run this command:

$ heroku accounts:add personal

Enter your Heroku credentials, and then add this to your ~/.ssh/config file:

Host heroku.personal

HostName heroku.com
IdentityFile ~/.ssh/[your heroku private key]
IdentitiesOnly yes

Now let's add your second Heroku account. Before proceeding, make sure you have your second account created on Heroku.

The Heroku Accounts plugin actually has the ability to generate a new SSH key for an existing Heroku account. If you have already generated a key, use the step above to add the second account. If not, use this command to add the account with an automatically generated key:

$ heroku accounts:add work --auto

I have named the second account work, but it's a good idea to make it more descriptive than that. In my case, I called it standardcode.

Now go to your project root, and use this command to set the Heroku account for that project:

$ heroku accounts:set work

To see all Heroku accounts that are configured, run this command:

$ heroku accounts

A asterisk will appear next to the active account within the project.

Managing multiple Heroku environments

It's common for Rails projects hosted on Heroku to have at least two environments: development (on your local machine) and production (on Heroku). Pretty quickly, you will realize the importance of setting up a staging environment on Heroku. Deploying your project to a staging app on Heroku allows you to test it in an environment that is as similar as possible to production.

Make sure you have installed the Heroku Toolbelt before proceeding.

Use this command to add a new app on Heroku that will be your staging environment:

$ heroku create myapp-staging --remote staging

At this point, you can use this command to deploy your app to staging:

$ git push staging master

If you were already running your production app on Heroku, you are probably already using this command to deploy to production:

$ git push heroku master

You may want to clean this up a bit, and rename the existing Heroku remote from heroku to production. You can do this easily in the .git/config file in the project root. Find [remote "heroku"] in the git config, and change it to [remote "production"].

If you are not already deploying your production app to Heroku, you can use this command to add a new production app on Heroku:

$ heroku create --remote production

You can now deploy to production using this command:

$ git push production master

Now you have two cleanly named Heroku remotes: staging and production. Nice work!

There is (at least) one major caveat to keeping two environments for your app on Heroku: you now have to specify which remote you want to target when running certain commands. For example, to display the config for your staging app, you would use this command:

$ heroku config --remote staging

If you do not specify the remote, you will see this:

$ heroku config

! Multiple apps in folder and no app specified.
! Specify app with --app APP.

One last approach: what if you want to duplicate your existing Heroku production app into a new staging app, including environment variables, addons, Heroku Postgres data, and all? You can easily do that using heroku fork. See below:

$ heroku fork -a production staging

$ git remote add staging git@heroku.com:myapp-staging.git

That pretty much sums it up. The Heroku Accounts plugin and the Heroku Toolbelt are capable of much more than what I've demonstrated here. Again, please refer to the documentation here and here if you get stuck.

Sep. 17 2015