【问题标题】:Force bundle install to use https:// instead of git:// for GitHub-based gems对于基于 GitHub 的 gem,强制捆绑安装使用 https:// 而不是 git://
【发布时间】:2014-02-11 04:45:22
【问题描述】:

我正在尝试构建一个 Rails 项目,因为我正在处理的主机无法访问 git:// 协议(端口 9418)的 Internet,我收到类似的错误

Fetching git://github.com/pivotal/jasmine.git
fatal: unable to connect to github.com:
github.com[0: 192.30.252.130]: errno=Connection refused

运行bundle install时。

GemFile 中的相关行没有指定 git:// 作为协议,它只是指向 GitHub 作为 gem 的源

gem 'jasmine', :github => 'pivotal/jasmine-gem'

我必须做些什么才能让 bundler 使用 https:// 而不是 git:// 从 GitHub 中提取 gem?

编辑:

除了编辑 GemFile 中受影响的每一行之外,还有其他方法吗?如果项目的 GemFile 更新,我希望避免任何合并问题。

【问题讨论】:

标签: ruby github bundler


【解决方案1】:

你可以这样做:

gem 'jasmine', git: 'https://github.com/pivotal/jasmine-gem.git'

【讨论】:

  • 我只需要在一个地方完成它,并且不希望它在每个服务器的配置中偷偷溜走,所以这对我来说是正确的。
【解决方案2】:

您应该能够在 Gemfile 中放入完整的 Git URL。例如:

gem 'jasmine', :git => 'https://github.com/pivotal/jasmine-gem.git' 

【讨论】:

    【解决方案3】:

    Git 使用 url..insteadOf 配置选项提供 URL 重写功能。

    所以要建立与 github.com 的所有连接,请使用 https:// 而不是 git://

    git config --global url."https://github.com".insteadOf git://github.com
    

    --global 开关为当前用户的所有 git 操作设置配置选项,因此有时它可能过于侵入。但它确实避免了更改当前项目中的 git 配置。

    【讨论】:

    • 优秀的答案!我不想改变 Gemfile 中的每一行,只是为了让事情在一个特定的客户站点上运行
    • @sam-kah-chiin answer (stackoverflow.com/a/41382363/1712589) 对此要好得多,因为关于 ruby bundler 而不是整个 git 的问题。
    • 这对于强制 bundle install 使用我生成的临时 SSH 密钥非常有用,仅用于在测试容器中使用,在一个很难获取gh 用于验证 PAT 的二进制文件。最终运行 git config --global url."git://github.com".insteadOf https://github.com 最初尝试运行 bundle config github.https false 但没有效果。
    【解决方案4】:

    如果您只想对一个 Gemfile 中的所有 gem 进行此操作,可以在文件顶部添加以下行:

    git_source(:github) do |repo_name|
      repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
      "https://github.com/#{repo_name}.git"
    end
    

    您也可以使用bundle config github.https true。但这只会影响您当前的环境。

    This won't be necessary anymore with Bundler 2.0.

    【讨论】:

    • 请注意,此选项需要捆绑程序 1.13.2+。
    • 不要这样做 bundle config github.https true如果你的生产环境在heroku上!
    • @MichaelJohnston 为什么不呢?
    • 你可以为你未来的自己添加一些这样的东西。 Bundler.ui.warn('You are using Bundler 2.0 or greater, you don\'t need this hack any more') if Bundler.bundler_major_version > 1
    • Rails 6 new 将此 oneliner 包含到 Gemfile 中:git_source(:github) { |repo| "https://github.com/#{repo}.git" }
    【解决方案5】:

    如果您要部署到 heroku,您只需将 BUNDLE_GITHUB__HTTPS(注意双下划线)添加为环境变量并将其设置为 true(在您的 heroku 应用程序仪表板中 Settings 选项卡下的@ 987654324@ 部分)。这会将所有此类请求的协议从 git:// 切换到 https://

    【讨论】:

      【解决方案6】:

      使用bundle config github.https true

      【讨论】:

      • 最佳答案!!谢谢
      • 同意。比公认的答案要好得多,因为这个问题是 ruby bundler 相关的,而不是整个 git。
      • 如果您没有相同的设置,此更改可能会扰乱您的生产部署。
      【解决方案7】:

      如果一个解决方案需要在每次安装时执行一个特殊的模糊设置,只需要一点点语法糖,这不是一个解决方案。

      这就是我提出这个作为答案的原因:

      只需使用:https 并报告捆绑程序的安全错误,即默认未加密协议。

      【讨论】:

        猜你喜欢
        • 2021-05-14
        • 1970-01-01
        • 1970-01-01
        • 2012-11-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-22
        • 1970-01-01
        相关资源
        最近更新 更多