简体   繁体   中英

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: 'git@bitbucket.org: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.

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

gem 'my_gem', :git => 'https://my_username:my_password@github.com/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: 'git@bitbucket.org: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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM