8

I have a private repo that I'm trying to access when deploying to Heroku. However, Heroku doesn't let me clone the private repo, and gives me the following error (as i would expect):

Host key verification failed.
       fatal: The remote end hung up unexpectedly
       Git error: command `git clone
       'git@github.com:pr/lm-models.git'
       "/tmp/build_3r0z0znww0zda/vendor/bundle/ruby/1.9.1/cache/bundler/git/lm-models-aab025aaadbe07202b16e1db7505ae1726f8723a"
       --bare --no-hardlinks` in directory /tmp/build_3r0z0znww0zda has failed.
 !
 !     Failed to install gems via Bundler.
 !
 !     Heroku push rejected, failed to compile Ruby/rails app

I have found this, but do not want to display my username/password in clear text:

Linking heroku app to a private(organization) github repo

Community
  • 1
  • 1
Kamilski81
  • 14,409
  • 33
  • 108
  • 161
  • What do you get if you do `heroku login`? are you able to log-in successfully? – fmendez Apr 01 '13 at 23:34
  • prompted me for login, and after login, i tried pushing again (but it failed) – Kamilski81 Apr 01 '13 at 23:39
  • Possible duplicate of [Installing private ssh deploy keys on Heroku](http://stackoverflow.com/questions/25961970/installing-private-ssh-deploy-keys-on-heroku) – Aidan Feb 21 '16 at 11:10

3 Answers3

6

This worked for me:

  1. Generate a Github Access Token
  2. In requirements.txt list private module as follows:

    git+https://your_user_name:your_git_token@github.com/your_company/your_module.git
    
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
TinaW
  • 989
  • 1
  • 18
  • 28
  • 1
    From the linked page, an important warning: *Treat your tokens like passwords and keep them secret. When working with the API, use tokens as environment variables instead of hardcoding them into your programs.* – Michele May 27 '20 at 08:39
4

Heroku only supports HTTP(S) Basic authentication with Git out of the box. That's unfortunate as it means you'd need to add your credentials as part of the installation URL and commit that as plain text in your list of dependencies. For your app to support SSH keys instead, do the following:

  1. Create a new SSH key which will be used by Heroku to access the GitHub repository. Choose a distinct name, e.g. id_rsa_heroku.
  2. Add the public part of the key to your GitHub account (link to settings).
  3. Use the heroku-buildpack-ssh-key: heroku buildpacks:add https://github.com/heroku/heroku-buildpack-ssh-key.git -i 1
  4. Set the private part of the key as an environment variable for your Heroku app: heroku config:set BUILDPACK_SSH_KEY=$(cat ~/.ssh/id_rsa_heroku)

From this moment, Heroku should be able to access and download code from any private repositories you have access to.

Honza Javorek
  • 8,566
  • 8
  • 47
  • 66
2

You need to use username/password in the Gemfile, or vendor the dependency. You can also use Gemfury (assuming it's a gem):

catsby
  • 11,276
  • 3
  • 37
  • 37