使用 bundle add --github 直接引用私有仓库并生成锁

解读

在国内 CI/CD、私有云或企业内网场景下,经常需要把尚未发布到 RubyGems.org 的私有 Gem 直接写进 Gemfile 并生成 Gemfile.lock,以便后续“一键 bundle install”。
bundle add 的 --github 参数可以一步完成“写 Gemfile + 拉源码 + 解析依赖 + 写锁文件”,但私有仓库涉及 SSH 密钥权限、Git 凭证助手、Bundler 协议识别、锁文件可重现性 四个关键点,面试时考官想确认候选人是否理解整套流程及常见坑。

知识点

  1. Bundler 协议识别规则:--github 等价于 git_source(:github),默认构造 https://github.com/owner/repo.git;若把 GITHUB__PROTOCOL=ssh 写到环境变量,则自动切换为 SSH 形式。
  2. SSH 多密钥场景:国内常用 ~/.ssh/config 的 Host 别名解决“同一 GitHub 账号不同密钥”或“自建 GitLab 端口非 22”问题,必须保证 bundle add 时 ssh -T git@github.com 能返回成功。
  3. 凭证助手与 CI:在 GitHub Actions、Gitee Go、Jenkins 等容器里,需要预挂 ssh-agent 并 ssh-add 私钥,或者把 bundle config 的 https:// 凭证写到 BUNDLE_GITHUB__COM 变量(x-access-token 格式)。
  4. 分支、tag、ref 锁死:--branch=main 或 --ref=v1.2.3 会写入 Gemfile.lock 的 revision 字段,保证 可重现构建;若省略则默认远端 HEAD,可能导致不同机器锁文件不一致。
  5. 私有 Gem 的依赖解析:Bundler 需要拉下 .gemspec 才能解析 transitivedependencies,因此本地必须能完整 clone,否则 bundle add 直接报错 Could not find gem。
  6. 锁文件冲突:多人协作时若有人用 HTTPS、有人用 SSH,锁文件会出现 remote: https://... 与 git@... 混用,导致 CI 无法拉取;团队应统一协议并在 README 中强制约定。

答案

  1. 本地一次性操作

    # 确保 SSH 公钥已加到 GitHub 私有仓库的 Deploy keys 或个人账号
    ssh -T git@github.com   # 看到 Hi username! 即正常
    
    # 进入项目根目录,执行
    bundle add my_private_gem \
      --github=owner/my_private_gem \
      --branch=main \
      --ref=4b825dc642cb6eb9a060e54bf8d69288fbee4904
    

    解释:

    • --github 让 Bundler 自动生成 gem 'my_private_gem', github: 'owner/my_private_gem', branch: 'main', ref: '4b825dc...' 并写进 Gemfile;
    • 同时立刻 bundle install,解析依赖并生成 Gemfile.lock,其中 remote: git@github.com:owner/my_private_gem.git 与 revision: 4b825dc... 被锁死,实现 可重现构建。
  2. CI/CD 中的无交互方案
    GitHub Actions 示例:

    - name: Setup SSH key
      run: |
        mkdir -p ~/.ssh
        echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_ed25519
        chmod 600 ~/.ssh/id_ed25519
        ssh-keyscan github.com >> ~/.ssh/known_hosts
    - name: Install dependencies
      run: |
        bundle config set --local github.https false   # 强制走 SSH
        bundle install
    

    若使用 https 方式,则把 bundle config set --local https://github.com.owner/gem.username x-access-token 并写入 ${{ secrets.GITHUB_TOKEN }} 即可。

  3. 常见错误排查清单

    • Permission denied (publickey) → 检查 SSH 密钥是否加进仓库的 Deploy keys 或 ssh-agent 未挂;
    • Could not find gem 'my_private_gem' → 仓库根目录缺少 my_private_gem.gemspec 或命名不匹配;
    • Gemfile.lock conflict → 团队协议不统一,执行 bundle config set --local github.https false 后重新 bundle install 即可对齐。

拓展思考

  1. 多私有源混合:当项目同时依赖 GitHub 私有库、自建 GitLab 库与 RubyGems 私源时,可在 Gemfile 里混用

    gem 'a', github: 'org/a'
    gem 'b', git: 'https://gitlab.company.com/b.git', branch: 'develop'
    source 'https://gems.company.com' do
      gem 'c'
    end
    

    并通过 bundle config set --local mirror.https://rubygems.org https://gems.company.com/mirror 做镜像加速,锁文件依然能保证一致性。

  2. 安全合规:国内金融、政企项目要求“源码不出内网”,可把私有 Gem 打成 .gem 文件后上传至内网 Artifactory,再通过 bundle add --source https://artifactory.intra 引用,避免直接暴露 Git 地址。

  3. 性能优化:大型私有仓库带历史大文件时,可在 .gemspec 里加 metadata['allowed_push_host'] 并启用 git shallow clone,CI 上设置 BUNDLE_GIT__SHALLOW=true 减少传输量。