解释如何缓存 bundle install 且兼容私有 gem 服务器

解读

国内 CI/CD 普遍采用「缓存目录 + 私有源」双轨策略:既要让 bundle install 在二次构建时秒级完成,又要确保 私有 gem 服务器(如阿里云 CodeArtifact、腾讯 CODING、自建 Geminabox)的鉴权与可用性。面试官想考察的是:

  1. 你对 Bundler 缓存机制(cache path、deployment、frozen)的理解深度;
  2. 能否在 多阶段 Docker 构建、GitHub Actions、GitLab CI、Jenkins 等主流国内场景下,给出可落地的缓存方案;
  3. 遇到「私有源 401」「缓存失效」「版本漂移」时如何快速止血。

知识点

  • Bundler 缓存层级:--path vendor/bundle、bundle config set --local path、BUNDLE_PATH 环境变量、bundle package --all 与 cache/vendor 区别。
  • 双重源优先级:bundle config set --local mirror.https://rubygems.org https://gems.ruby-china.com 与 source 'https://gems.mycompany.com' 并存时,Bundler 2.3+ 按 全局 mirror → 私有源 → 官方源 顺序解析。
  • 鉴权传递:bundle config set --local gems.mycompany.com ${BUNDLE_GEMS__MYCOMPANY__COM} 把 Basic Auth 写入 ~/.bundle/config,避免硬编码 token;CI 中通过 加密变量 注入。
  • 缓存键设计:国内常用 ${{ runner.os }}-ruby-${{ hashFiles('**/Gemfile.lock') }} 作为 primary key,并保留 restore-keys 做模糊回退;若私有 gem 版本号带 build metadata(如 1.0.0.pre.123456),需把 .gem_build_version 文件加入哈希。
  • deployment 模式:bundle config set --local deployment true 会强制 frozen 并禁止写锁文件,配合 bundle install --local 可跳过网络,但要求 vendor/cache 里已存在 .gem 实体。
  • Docker 多阶段:第一阶段 RUN bundle package --all && bundle install,把 vendor/cache 与 vendor/bundle 一并 COPY --from=builder 到运行时镜像,实现 0 网络安装。
  • 缓存失效急救:bundle install --retry 3 --jobs 4 仍 401 时,可 bundle config unset mirror.https://rubygems.org 临时回退官方源;若私有源宕机,把所需 .gem 提前 gem fetch 到 vendor/cache 并 git lfs 托管,实现 离线路径。

答案

  1. 本地开发

    bundle config set --local path 'vendor/bundle'
    bundle config set --local cache_all true
    bundle config set --local gems.mycompany.com ${PRIVATE_GEM_CREDENTIAL}
    bundle install
    

    提交 Gemfile.lock 与 vendor/cache(.gem 实体)到仓库,确保 CI 无网也能装。

  2. GitHub Actions 示例(国内镜像 + 私有源)

    - uses: actions/cache@v3
      with:
        path: |
          vendor/bundle
          vendor/cache
        key: ${{ runner.os }}-ruby-${{ hashFiles('Gemfile.lock') }}
        restore-keys: |
          ${{ runner.os }}-ruby-
    - run: |
        bundle config set --local path vendor/bundle
        bundle config set --local cache_all true
        bundle config set --local mirror.https://rubygems.org https://gems.ruby-china.com
        bundle config set --local gems.mycompany.com ${{ secrets.PRIVATE_GEM_CREDENTIAL }}
        bundle install --jobs 4 --retry 3
    

    若缓存命中,bundle install 耗时 < 5s;未命中则自动回退官方源,双重保障。

  3. GitLab CI(自建 Runner,内网私有源)

    cache:
      key: "$CI_COMMIT_REF_SLUG-ruby-$CI_NODE_INDEX"
      paths:
        - vendor/bundle
        - vendor/cache
    before_script:
      - bundle config set --local path vendor/bundle
      - bundle config set --local deployment true
      - bundle config set --local gems.mycompany.com ${PRIVATE_GEM_CREDENTIAL}
    script:
      - bundle install --local || bundle install
    

    使用 deployment + --local 先尝试离线,失败再回退网络,兼顾速度与可靠性。

  4. Docker 多阶段(国内镜像加速)

    FROM ruby:3.2-alpine as builder
    WORKDIR /app
    COPY Gemfile* ./
    RUN bundle config set --local mirror.https://rubygems.org https://gems.ruby-china.com && \
        bundle config set --local gems.mycompany.com ${PRIVATE_GEM_CREDENTIAL} && \
        bundle package --all && \
        bundle install --path vendor/bundle
    
    FROM ruby:3.2-alpine
    WORKDIR /app
    COPY --from=builder /app/vendor /app/vendor
    COPY . .
    ENV BUNDLE_PATH=/app/vendor/bundle
    CMD ["bundle", "exec", "puma"]
    

    最终镜像 不含源码层私有 token,仅保留只读 vendor,安全合规。

  5. 私有源 401 快速止血

    bundle config unset gems.mycompany.com
    gem fetch my_private_gem -v 1.2.3 --source https://gems.mycompany.com
    mv my_private_gem-1.2.3.gem vendor/cache/
    bundle install --local
    

    把 .gem 实体提前缓存,私有源宕机也能发版。

拓展思考

  • 零信任网络:把 vendor/cache 做成 OCI 镜像层,通过 Harbor 做鉴权与漏洞扫描,实现「gem 即镜像」的供应链闭环。
  • 动态版本漂移:私有 gem 采用 CalVer 每日构建,CI 中增加 bundle outdated --strict 检测,若 patch 级别 漂移自动创建 MR,人工 review 后合并,兼顾敏捷与稳定。
  • 多架构缓存:国内 ARM 节点逐渐普及,可在缓存 key 中加入 ${{ runner.arch }},避免 x86_64 与 arm64 的 native extension 混用导致 SIGILL。