Juggling with multiple Heroku accounts

08 Jul 2013

Say you have 2 Heroku accounts: personal & work-related. Most Heroku commands (like heroku config or heroku ps) work as long as your logged-in user (heroku login) is a collaborator on the target project. Of course you can be a collaborator on as many projects as you want.

But what about deploying code? Depending on your default SSH key, you might get:

$ git push heroku master

!  Your key with fingerprint 1f:cc:[...]:41:d4 is not authorized to access myproject.
fatal: Could not read from remote repository.

If you have 2 accounts, you should already have 2 separate SSH keys:

$ ls ~/.ssh

id_rsa    id_rsa.pub    work_rsa    work_rsa.pub

Unfortunately you can't just link both keys to both accounts: once you've linked work_rsa to your work account, it can't be reused.

$ heroku keys:add

! This key is already in use by another account. Each account must have a unique key.

What we want is to specify which public key to use when pushing to the "heroku" git remote. This can be done by updating the SSH config at ~/.ssh/config:

Host heroku.work
 HostName heroku.com
 IdentityFile ~/.ssh/work_rsa
 IdentitiesOnly yes

Now we can update the git remote to use the new host definition:

BEFORE   heroku   git@heroku.com:myproject.git
AFTER    heroku   git@heroku.work:myproject.git

Each project can specify which SSH key to use, and deployments are still as easy as git push heroku master.