我使用 gem,
my_gem
存储在 github 上的应用程序中,my_app
。 Gemfile
看起来像这样:
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'
大约一年前,我能够
bundle install
应用程序并推送到 heroku
但这不再有效。我从 Github 重新发布了一个新的私有访问令牌,让它可以访问 repo,并将其设置在我的 .env 文件中,如下所示
PRIVATE_LIB_TOKEN=<token>
当我键入
bundle install
时,我收到以下错误消息
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'
查看
/Users/my_name/.rvm/gems/ruby-2.7.4/cache/bundler/git/
有一个名为 my_gem-my_gem-<yet_another_token>
目录,但这没有帮助。我也删除了/Users/xyz/my_app
并重新安装了应用程序,但这没有用。
我注意到,自从上次成功以来,我已经在 Github 上设置了双因素身份验证。
我该如何解决这个问题?
这不是问题的答案,但确实提供了 Sachin Sing 建议的解决方法。 This SO answer points to accessing the gem using a command like
gem 'my_gem', git: "https://#{ENV['PRIVATE_LIB_TOKEN']}:[email protected]/obromios/my_gem.git"
导致令牌被写入
Gemfile.lock
并因此出现在存储库中。建议更好的方法是将以下内容放在Gemfile
gem 'my_gem', git: "https://github.com/obromios/my_gem.git", branch: 'master'
然后做以下
export MY_OAUTH_KEY=<token>
bundle config github.com $MY_OAUTH_KEY
这让我可以捆绑安装
Gemfile
。为了部署到 heroku,我需要设置以下配置变量
heroku config:set BUNDLE_GITHUB__COM=<token>
然后我可以将应用程序部署到 heroku,并且 gem 已正确安装。
最安全的方法是按照以下方式配置 github 个人令牌。在按照他们的官方文档创建您的 github 个人访问令牌后,请执行以下操作:
bundle config github.com YOUR_GITHUB_PERSONAL_TOKEN:x-oauth-basic
BUNDLE_GITHUB__COM
ENV变量:BUNDLE_GITHUB__COM = YOUR_GITHUB_PERSONAL_TOKEN
您也可以通过以下方式从您的终端执行此操作:
heroku config:set BUNDLE_GITHUB__COM=YOUR_GITHUB_PERSONAL_TOKEN