matts1
matts1

Reputation: 865

Deploy heroku app with private git repo in gemfile

I've made a rails app, and want to deploy it to Heroku. However, git push heroku master is failing because it doesn't have permission to access a gem referenced in the gemfile (as the repository is private). This line is producing the error.

gem 'auth', branch: 'master', git: '[email protected]:myteam/auth.git'

First I looked here, and so I tried generating a new set of public and private keys and then transferring them over with heroku keys:add, but it wouldn't accept a private key.

I also tried this to simulate an ssh and ran heroku run bash, then tried to run ssh-keygen, but the files in there were temporary and disappeared when I tried to run the server.

Upvotes: 1

Views: 480

Answers (1)

K M Rakibul Islam
K M Rakibul Islam

Reputation: 34338

The easiest way to do this is by putting the username and password into the URL, as in Basic HTTP Auth, e.g.

gem 'my_gem', :git => 'https://my_username:[email protected]/my_github_account/my_repo.git', :ref => 'revision_no'

If you don't want to put the password into the Gemfile, then you can put it into an environment variable.

On Heroku you can add the environment variables using this command:

heroku config:add ENV_VAR=VALUE

and then in the Gemfile you'd use this environment variable, like this:

gem 'my_gem',
      :git => "https://#{ENV['var_private_gem_username']}:#{ENV['var_private_gem_password']}@github.com/my_github_account.git",
      :ref => 'rev'

To avoid having to use HTTPS during development, you could also try splitting up development and production modes, like so:

group :development do
    gem 'auth', branch: 'master', git: '[email protected]:myteam/auth.git'
end
group production do
    gem 'my_gem',
      :git => "https://#{ENV['var_private_gem_username']}:#{ENV['var_private_gem_password']}@github.com/my_github_account.git",
      :ref => 'rev'
end

Note: Neither method is 100% secure. But, the second option (storing in the heroku's environment variable) is more secure than the first option (storing them in a plain text). But, there is no other alternatives to do such thing (AFAIK).

Upvotes: 2

Related Questions