Managing Multiple Heroku Accounts

Heroku
© Heroku

I’ve been using Heroku for some time now. Well, ever since the Heroku Garden days — which was over a year ago. Seems longer. Anyway, I’ve got a handful of apps on Heroku. Some prototypes and some in production. I also have multiple accounts in play. The only problem is that this isn’t the easiest thing in the world to manage. In case you travel down this path, here are some tips.

Requirements

The following assumes you are on the MacOS, already own multiple Heroku accounts, have installed the Heroku gem, and have deployed a Ruby on Rails application to Heroku before. Otherwise, read the Quickstart docs and you’ll be up and running in no time.

Credentials

Let’s start with your credentials file (i.e. ~/.heroku/credentials). This file is usually created for you by the Heroku gem when you setup your first account. It is a text file with your account login (i.e. email address) on the first line and your password on the second line. To setup multiple accounts, I’d suggest the following:

  • ~/.heroku/credentials - Your current account that is in play.
  • ~/.heroku/account_1.credentials - Your first account.
  • ~/.heroku/account_2.credentials - Your second account.

When you need to switch accounts simply copy the contents of the account you are switching to over the existing credentials file (example: account_2.credentials now becomes credentials). I wrote a simple Ruby gem called Heroku Plus that easily automates the switching of accounts for you. Now, from the command line, you can simply type the following to switch between accounts:

herokup -a -s <account>

Security Keys

Right, so we have the credentials files out of the way but there is one last step. You need to associate your public key with each account. Here is my suggestion:

  1. Change directory to ~/.ssh
  2. Create your private and public keys for each account by running the following command: ssh-keygen -t rsa -C "<email>" -f ~/.ssh/<account>.identity
  3. Type heros <account> to switch account and add the new keys for each: heroku keys:add [path to keyfile].pub

For each Heroku account beyond your first, you’ll need to make use of the SSH config file (i.e. ~/.ssh/config) as follows (thanks to the Heroku support team on this one):

  • Host official.heroku.com
  • HostName heroku.com
  • User git
  • IdentityFile ~/.ssh/<account>.identity
  • IdentitiesOnly yes

Also, for each account beyond the first, you’ll need to switch to each app directory and type the following command: git remote add heroku git@official.heroku.com:<your app>.git. This will allow you to push and deploy your changes back to the Heroku servers and finalizes your setup.

Workflow

From this point forward, you can switch between accounts as follows:

  1. Change directory to the app you want to work on.
  2. Type: hp -a -s <account>.
  3. Make changes to your code.
  4. Type: git commit -a -m “Your comments.”
  5. Type: git push heroku.
  6. Have a beer.

Tags:

Monday, February 22nd, 2010 Services

10 Comments to Managing Multiple Heroku Accounts

  1. You can use ENV['HOME'] to get the user’s home directory in your ruby scripts. That should make them a little easier for users to download and use without having to hardcode paths. It should also work on unix/linux.

  2. Eric Saxby on March 24th, 2010
  3. Eric, good point and thanks. I’ve updated both scripts to be a bit more intelligent based on your feedback.

  4. Brooke Kuhlmann on March 24th, 2010
  5. Great post, helped me a lot!

    The only sad thing is that the heroku gem seems to be explicitly searching for git@heroku.com:.. to determine which app it applies to. So, with git@official.heroku.com, I need to specify the app, like this, heroku info –app myapp

    A big problem, no, annoying, yes. :) Let me know if I’m mistaken!

  6. Oma Amundsen on April 30th, 2010
  7. Yep, you’re correct. You’ll have to specify the –app argument for any additional Heroku account beyond your first.

    It is annoying, I agree, but I haven’t found a good solution for this yet. I’m all ears if you run across any solution, patch, etc.

  8. Brooke Kuhlmann on April 30th, 2010
  9. Thanks for posting this how-to. I started working with a second Heroku account today, and this made it a lot easier.

    I modified credentials.rb so it wouldn’t display passwords in clear text. I’m frequently working with others at my machine, and I don’t want to inadvertently display a password when checking which credentials are active.

    I posted my tweaked version here: http://gist.github.com/474211

  10. Jim Benton on July 13th, 2010
  11. You bet. Glad this post was of help.

    Thanks for the Gist. I’ve incorporated your changes and updated my code in the credentials.rb file so future downloads pick up the changes as well.

  12. Brooke Kuhlmann on July 13th, 2010
  13. To get around using the –all switch on heroku commands, you just need to swap out an identity file for ssh as well. I posted a blog on the subject: http://collectiveidea.com/blog/archives/2010/08/06/heroku-ing-with-multiple-personalities/ and my version of your switcher is in here: http://gist.github.com/511789

  14. Keith Gaddis on August 6th, 2010
  15. Thanks Keith. Based on your feedback and everyone else’s feedback, I’ve decided to turn this into a gem for easier management. You can find it here.

    I’ve also updated this post to reference the gem now as well.

  16. Brooke Kuhlmann on August 15th, 2010
  17. Could you explain why the git-approach that Heroku has implemented doesn’t work for you? In my experience, simply adding a collaborator to your app works just fine and saves the hassle of having to switch between accounts.

  18. J. Haagmans on September 21st, 2010
  19. You make a very good point, simply using Git as it was intended works extremely well. I really can’t argue that point. However, there are edge cases where Git doesn’t solve everything. For example, when setting up client accounts for non-technical people, I don’t always wish or am allowed to own the account or be associated with the billing aspects of the account. I also want to be able to easily and quickly turn over the keys to the kingdom in some cases by simply giving the account credentials to the next developer/business owner with minimal hassle. Finally, I like the added security of keeping my primary account completely separate from any client account (yes, I know I could easily regenerate my public key). This subtle difference might not seem like much but is handy for me (and others it seems).

  20. Brooke Kuhlmann on September 21st, 2010

Leave a comment

You must be logged in to post a comment.