Home > Net >  Unable to bundle install a private gem with x-oauth-basic authentication
Unable to bundle install a private gem with x-oauth-basic authentication

Time:09-03

I use a gem, my_gem stored on github in an app, my_app. The Gemfile looks like this:

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.7.4'
gem 'rails', '~> 5.2.6'
...
gem 'my_gem', git: "https://#{ENV['PRIVATE_LIB_TOKEN']}:[email protected]/obromios/my_gem.git", branch: 'master'

About a year ago, I was able to bundle install the app and push to heroku but this no longer works. I reissued an new private access token from Github, giving it access to the repo, and set it in my .env file as follows

PRIVATE_LIB_TOKEN=<token>

When I type bundle install I get the following error message

remote: Repository not found.
fatal: Authentication failed for 'https://github.com/obromios/my_gem.git/'

Retrying `git clone https://[email protected]/obromios/my_gem.git /Users/my_name/.rvm/gems/ruby-2.7.4/cache/bundler/git/my_gem-<some_other_token> --bare --no-hardlinks --quiet` at /Users/my_name/xyz/my_app due to error (2/4): Bundler::Source::Git::GitCommandError Git error: command `git clone https://[email protected]/obromios/my_gem.git /Users/my_name/.rvm/gems/ruby-2.7.4/cache/bundler/git/my_gem-<some_other_token> --bare --no-hardlinks --quiet` in directory my_app has failed.

If this error persists you could try removing the cache directory '/Users/my_name/xyz/my_app'

Looking at /Users/my_name/.rvm/gems/ruby-2.7.4/cache/bundler/git/ there was a directory named my_gem-<yet_another_token>. The tokens are all different but are random hashes of similar length. I removed the my_gem-<yet_another_token> directory and this did not help. I also deleted /Users/xyz/my_app and reinstalled the app, but this did not work.

I note that since last time that this worked, I had set up two factor authentication on Github.

How do I fix this?

CodePudding user response:

This is not an answer to the question, but does provide a workaround as suggested by Sachin Sing. This SO answer points out that accessing the gem using a command like

gem 'my_gem', git: "https://#{ENV['PRIVATE_LIB_TOKEN']}:[email protected]/obromios/my_gem.git"

causes the token to be written to the Gemfile.lock and so appears in the repository. It is suggested a better way is to put the following in the Gemfile

gem 'my_gem', git: "https://github.com/obromios/my_gem.git", branch: 'master'

and then doing the following

export MY_OAUTH_KEY=<token>
bundle config github.com $MY_OAUTH_KEY

This allowed me to bundle install the Gemfile. In order to deploy to heroku, I needed to set the following config variable

heroku config:set BUNDLE_GITHUB__COM=<token> 

Then I can just deploy the app to heroku and the gem is installed properly.

  • Related