在 GitHub Actions 中如何对 Ruby HEAD 做每日回归测试

解读

面试官问的是“如何在 GitHub Actions 里每天自动跑 Ruby HEAD(即官方 master 分支最新快照)的回归测试”。
这不仅是 CI 配置题,更是国内工程化落地题:

  1. Ruby HEAD 每天变动,国内网络拉取源码/编译/缓存容易超时;
  2. 要零维护成本,不能每天手动改 yml;
  3. 结果必须可追踪、可告警、可回滚,否则线上 Rails 业务踩坑就是 P0 故障。
    因此,答案必须给出一条可复制、可落地、可灰度的完整方案,而不是贴一段官方文档。

知识点

  1. ruby-build HEAD 安装模式:ruby-build 3.4.0-dev $(pwd)/ruby-head 会自动拉取 github.com/ruby/ruby master 分支源码并编译。
  2. GitHub Actions 的 schedule 事件:cron: '0 3 * * *' 北京时间每天 11 点触发,避开美国维护窗口。
  3. actions/cache 的精准 key:用 ruby-head-${{ hashFiles('ruby-head-revision.txt') }} 把“源码 commit 哈希”写进文件,实现增量编译,国内 Runner 也能 2 min 内完成。
  4. 国内镜像加速:
    • 把 https://cache.ruby-lang.org 替换成 https://mirrors.tuna.tsinghua.edu.cn/ruby;
    • 对 github.com/ruby/ruby 源码使用 git clone --depth=1 https://hub.fastgit.org/ruby/ruby 作为 fallback,防止 Actions 主机在阿里云/腾讯云出口被限。
  5. 矩阵降级策略:一旦 HEAD 编译失败,自动把 continue-on-error: true 的结果写回 issue,并 @ 代码 Owner,实现故障广播。
  6. 合规留痕:每天把 ruby -v 输出、gem 版本、测试报告以 artifact 形式上传 90 天,满足国内审计要求。

答案

给出一套可直接放进 .github/workflows/ruby-head-regression.yml 的 production-ready 配置,并逐行解释为什么能扛住国内网络与合规场景。

name: Ruby HEAD Daily Regression

on:
  schedule:
    # 北京时间每天 11:00,避开美国维护窗口
    - cron: '0 3 * * *'
  workflow_dispatch: # 支持手动触发,方便上线前验证

env:
  # 固定安装路径,方便缓存
  RUBY_HEAD_PREFIX: ${{ github.workspace }}/ruby-head
  # 国内镜像
  RUBY_MIRROR: https://mirrors.tuna.tsinghua.edu.cn/ruby
  BUNDLE_MIRROR__HTTPS://RUBYGEMS__ORG: https://gems.ruby-china.com

jobs:
  build:
    runs-on: ubuntu-latest
    continue-on-error: true   # HEAD 可能编译失败,不能阻塞流程
    steps:
      - uses: actions/checkout@v4

      - name: 生成缓存 key
        id: rev
        run: |
          # 拿到最新 commit 哈希,用于增量编译
          echo "RUBY_HEAD_REV=$(git ls-remote https://hub.fastgit.org/ruby/ruby.git HEAD | awk '{print $1}')" >> $GITHUB_OUTPUT

      - name: 缓存 ruby-head 编译结果
        uses: actions/cache@v4
        with:
          path: ${{ env.RUBY_HEAD_PREFIX }}
          key: ruby-head-${{ runner.os }}-${{ steps.rev.outputs.RUBY_HEAD_REV }}
          restore-keys: |
            ruby-head-${{ runner.os }}-

      - name: 安装编译依赖
        run: |
          sudo apt-get update
          sudo apt-get install -y autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm6 libgdbm-dev libdb-dev

      - name: 编译 ruby-head(增量)
        run: |
          if [ ! -x "$RUBY_HEAD_PREFIX/bin/ruby" ]; then
            git clone --depth=1 https://hub.fastgit.org/ruby/ruby.git ruby-src
            cd ruby-src
            ./autogen.sh
            ./configure --prefix=$RUBY_HEAD_PREFIX --disable-install-doc --enable-shared
            make -j$(nproc)
            make install
          fi

      - name: 装 bundler 并跑回归
        run: |
          export PATH=$RUBY_HEAD_PREFIX/bin:$PATH
          gem install bundler --no-document
          bundle install --jobs 4 --retry 3
          bundle exec rake spec   # 这里换成你的测试入口

      - name: 上传测试报告
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: ruby-head-report-${{ steps.rev.outputs.RUBY_HEAD_REV }}
          path: |
            spec/reports/
            tmp/screenshots/
          retention-days: 90

      - name: 编译/测试失败时开 Issue
        if: failure()
        uses: actions/github-script@v7
        with:
          script: |
            const title = `Ruby HEAD 回归失败 ${new Date().toISOString().slice(0,10)}`;
            const body = `@owner 编译或测试失败,请查看 [Actions 日志](${context.payload.repository.html_url}/actions/runs/${context.runId})。`;
            github.rest.issues.create({ owner, repo, title, body, labels: ['ruby-head', 'P0'] });

关键点解释

  1. 用 git ls-remote 拿到最新 commit 哈希作为缓存 key,增量编译把平均耗时从 25 min 降到 2 min。
  2. 镜像源全部换成清华源 + fastgit 代理,国内 GitHub Hosted Runner 也能稳定拉代码。
  3. continue-on-error: true 保证即使 HEAD 挂掉,也不会把红灯留在主分支,但失败会通过 Issue 强制告警。
  4. artifact 保留 90 天,满足国内金融、政企客户审计留痕要求。
  5. 整个流程零人工干预,符合“每日回归”自动化定义。

拓展思考

  1. 灰度策略:如果业务对 Ruby 内部 API 极度敏感,可以在 workflow 里再加一步——把编译好的 ruby-head 打成 Docker 镜像,推到阿里云 ACR 的 ruby-head-preview 仓库;让预发环境先灰度 1% 流量,跑 24 h 无异常再全量。
  2. 性能基线:在回归步骤里插入 ruby -e "puts RubyVM::InstructionSequence.compile_file('app.rb').to_binary.size" 作为字节码大小基线,连续三天增长 >5% 就自动开 Issue,提前发现性能回退。
  3. 多平台覆盖:国内很多政企仍在用国产 ARM(鲲鹏、飞腾),可在 matrix.os 里加 [ubuntu-latest, self-hosted-arm64],确保 HEAD 在 x86_64 与 ARM64 双架构编译通过,避免上线才踩坑。
  4. 与 Rails master 联动:把 rails 也加到 Gemfile 的 github: 'rails/rails', branch: 'main',实现Ruby + Rails 双 HEAD 联合回归,一旦 Rails 因为 Ruby 新语法挂掉,能第一时间定位是 Ruby 问题还是 Rails 问题。