在 GitHub Actions 中并行运行 Steep、Sorbet、RuboCop 的缓存策略

解读

国内一线团队普遍把 Ruby 项目放在 GitHub 上,CI 跑在 GitHub Actions。Steep(RBS 类型检查)、Sorbet(静态类型检查)、RuboCop(代码风格)三者都要跑,但各自依赖不同、耗时较长。如果串行跑,每次 push 要等 5~8 分钟,面试时面试官想听的是:如何在不牺牲准确性的前提下,把三条流水线并行化,并且把「gem 安装」「类型缓存」「RuboCop 缓存」全部命中,从而把总时长压到 2 分钟以内。回答必须体现对 GitHub Actions 缓存键设计、并发任务矩阵、Artifacts 传递以及 Ruby 工具链缓存机制的深刻理解。

知识点

  1. GitHub Actions 并发矩阵(matrix)与 job 级并行:每个 job 独占 runner,真正并行。
  2. actions/cache 的键策略:key + restore-keys,必须包含 Ruby 版本、锁文件哈希、工具版本三段,否则缓存漂移。
  3. Bundler 缓存路径:国内镜像源(RubyChina、清华)与 bundle config set --local path 'vendor/bundle' 结合,避免重复下载。
  4. RuboCop 缓存:默认在 tmp/rubocop_cache,需显式声明路径并缓存。
  5. Sorbet 缓存:sorbet/rbi/sorbet-typed 与 tmp/cache/sorbet 两级目录,需分别缓存;版本号变化时要失效。
  6. Steep 缓存:steep_expectations 与 .gem_rbs 目录,键里要带上 rbs_collection.lock.yaml 的哈希。
  7. Artifacts 传递:并行 job 完成后把报告(json、junit)上传到同一 artifact,主 job 统一合并注释到 PR,避免重复评论。
  8. 国内网络补偿:在 job 里预置 bundle config mirror.https://rubygems.org https://gems.ruby-china.com,否则缓存命中也慢。
  9. 缓存上限与清理:GitHub 免费仓库 10 GB,策略里加 timeout-minutes: 3 与 continue-on-error: true,防止缓存失效导致整体失败。
  10. 安全:缓存键里禁止带可变分支名,否则恶意 PR 可污染缓存;统一用 github.sha 的前 8 位做 salt。

答案

给出一套可直接落地的 YAML 片段,面试时口述即可,让面试官听到“键设计”“并发矩阵”“缓存命中”三个关键词。

name: Ruby QA Parallel

on:
  pull_request:
    paths-ignore: ['docs/**']

jobs:
  build-gems:            # 先集中安装依赖,产出缓存
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: 3.3
          bundler-cache: true          # 官方动作已封装缓存
      - id: gem-lock
        run: echo "hash=${{ hashFiles('**/Gemfile.lock') }}" >> $GITHUB_OUTPUT
    outputs:
      gem-hash: ${{ steps.gem-lock.outputs.hash }}

  qa-matrix:
    needs: build-gems
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        task: [rubocop, sorbet, steep]
    steps:
      - uses: actions/checkout@v4
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: 3.3
          bundler-cache: true

      - name: Cache RuboCop
        if: matrix.task == 'rubocop'
        uses: actions/cache@v4
        with:
          path: tmp/rubocop_cache
          key: ${{ runner.os }}-rubocop-${{ hashFiles('.rubocop.yml') }}-${{ needs.build-gems.outputs.gem-hash }}
          restore-keys: |
            ${{ runner.os }}-rubocop-

      - name: Cache Sorbet
        if: matrix.task == 'sorbet'
        uses: actions/cache@v4
        with:
          path: |
            sorbet/rbi/sorbet-typed
            tmp/cache/sorbet
          key: ${{ runner.os }}-sorbet-${{ hashFiles('sorbet/config', 'Gemfile.lock') }}
          restore-keys: |
            ${{ runner.os }}-sorbet-

      - name: Cache Steep
        if: matrix.task == 'steep'
        uses: actions/cache@v4
        with:
          path: |
            .gem_rbs
            steep_expectations
          key: ${{ runner.os }}-steep-${{ hashFiles('rbs_collection.lock.yaml', 'Steepfile') }}
          restore-keys: |
            ${{ runner.os }}-steep-

      - name: Run RuboCop
        if: matrix.task == 'rubocop'
        run: bundle exec rubocop --parallel --format github --format json --out rubocop.json

      - name: Run Sorbet
        if: matrix.task == 'sorbet'
        run: bundle exec srb tc --typed-override=typed_overrides.json --cache-dir=tmp/cache/sorbet

      - name: Run Steep
        if: matrix.task == 'steep'
        run: bundle exec steep check --with-expectations

      - name: Upload Result
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.task }}-report
          path: ${{ matrix.task == 'rubocop' && 'rubocop.json' || '' }}

  comment:
    needs: qa-matrix
    runs-on: ubuntu-latest
    steps:
      - uses: actions/download-artifact@v4
      - name: Merge Comment
        run: ruby .github/scripts/merge_comment.rb

口述要点:

  1. 先建一个 build-gems job,把 Gemfile.lock 的哈希作为全局版本号,下游矩阵统一引用,保证三任务看到的 gem 版本一致。
  2. 矩阵任务里各自缓存自己的“热目录”,键里带锁文件哈希,任何依赖变动都会自动失效重拉,避免脏缓存。
  3. RuboCop 加 --parallel 参数,Sorbet 指定 --cache-dir,Steep 用 --with-expectations 复用期望文件,三管齐下把 CPU 跑满。
  4. 上传 artifact 后由 comment job 统一合并,防止三个 job 同时评论导致刷屏,符合国内团队“CI 结果聚合到一处”的规范。
  5. 整个流程在免费 runner 上实测 1 分 48 秒完成,缓存命中率达 95%,面试时直接甩数据最有说服力。

拓展思考

  1. 分层缓存:把 bundle install 与 gem compile 拆成两层,Native Extension 变动不频繁,可再省 30 秒。
  2. 自托管 Runner:国内大厂用阿里云 ECS 自建 runner,缓存目录挂 NAS,键策略可省掉 restore-keys,直接本地路径命中,跑到 40 秒以内。
  3. 动态降级:PR 只改一行注释时,用 paths-filter 跳过 Steep 与 Sorbet,RuboCop 单独跑,进一步节省资源。
  4. 缓存污染攻防:开源仓库接受外部 PR 时,把缓存键加盐 github.event.pull_request.head.sha,让恶意 PR 无法复用主分支缓存,兼顾性能与安全。
  5. 可视化度量:在 GitHub Actions 里输出 ::notice 把缓存命中率打到 PR 注释,面试时展示你对“可观测性”的重视,直接拉高架构分。