GitHub Actions 矩阵拆分跨平台测试
解读
在国内中大型互联网公司的 Scala 服务端/大数据团队面试里,CI/CD 的“跨平台验证”早已不是运维专属,而是开发必须亲自设计的质量门禁。
面试官抛出“GitHub Actions 矩阵拆分跨平台测试”,核心想考察三点:
- 你能否把**Scala 项目在不同操作系统 + JDK 分发版 + 二进制目标(JVM/Native/JS)**的组合爆炸收敛到可维护的 YAML;
- 你能否利用**矩阵拆分(matrix slicing)**把 30+ 维度的组合在免费额度内并行跑完,且失败重跑成本最小;
- 你能否把缓存、产物、测试报告、Docker 镜像与矩阵维度一一对应,避免“缓存污染”或“产物覆盖”。
答不出“矩阵拆分策略”,会被直接判定为“只会写业务代码,撑不起大型分布式工程的交付质量”。
知识点
- GitHub Actions 矩阵语法:matrix 节支持
include、exclude、fail-fast、max-parallel四个关键字; - 矩阵拆分(matrix slicing):通过
strategy.matrix.os+scala-version+platform三维笛卡尔积,再利用include/exclude做白名单/黑名单裁剪; - 并发配额:GitHub 免费组织 20 并发、付费企业 180 并发,国内镜像源(清华/华为)可缓解 Maven Central 拉取慢;
- 缓存键设计:
sbt-cache-${{ runner.os }}-${{ hashFiles('**/build.sbt', 'project/*.scala') }},必须带 OS 维度,否则 Ubuntu 缓存会被 Windows 复用导致路径格式错误; - 跨平台产物隔离:
upload-artifact的name必须包含${{ matrix.os }}-${{ matrix.jdk }}-${{ matrix.scala }},否则 nightly 合并阶段找不到对应包; - Scala 特有坑:
- Scala Native 需要额外装
llvm+clang,Windows 跑不通需exclude: { os: windows-latest, platform: native }; - Scala.js 1.x 在 macOS 12 上偶现
Node.js OOM,需单独给NODE_OPTIONS: --max-old-space-size=4096;
- Scala Native 需要额外装
- 失败重跑:
if: failure()步骤里用actions/upload-artifact把target/test-reports上传,方便本地复现; - 国内加速:
sbt -Dsbt.repository.config=./.sbtrepos-Nexus指向公司 Nexus 私服,避免 Actions 拉包超时; - 矩阵报告聚合:用
dorny/test-reporter在workflow_run事件里二次聚合,把多维矩阵结果汇总成一行通过率,方便飞书/钉钉机器人推送。
答案
给出一个可直接落地的 .github/workflows/ci.yml 片段,演示“三维矩阵拆分 + 缓存 + 产物隔离 + 失败重跑”完整闭环:
name: CI
on:
pull_request:
push:
branches: [main]
env:
SBT_OPTS: -Xms2G -Xmx4G -XX:+UseG1GC
jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
max-parallel: 15
matrix:
os: [ubuntu-22.04, windows-2022, macos-12]
jdk: [8, 11, 17]
scala: [2.12.18, 2.13.12, 3.3.1]
platform: [jvm]
include:
# 仅 Linux 跑 Native 与 JS
- { os: ubuntu-22.04, jdk: 11, scala: 2.13.12, platform: native }
- { os: ubuntu-22.04, jdk: 11, scala: 2.13.12, platform: js }
exclude:
# Scala3 暂不支持 Native
- { scala: 3.3.1, platform: native }
steps:
- uses: actions/checkout@v4
- name: Setup JDK
uses: actions/setup-java@v3
with:
distribution: temurin
java-version: ${{ matrix.jdk }}
- name: Setup Node
if: matrix.platform == 'js'
uses: actions/setup-node@v4
with:
node-version: 18
- name: Setup Scala Native deps
if: matrix.platform == 'native'
run: |
sudo apt-get update
sudo apt-get install -y clang llvm
- name: Cache SBT
uses: actions/cache@v3
with:
path: |
~/.sbt
~/.ivy2/cache
~/.coursier
key: sbt-${{ runner.os }}-${{ hashFiles('**/*.sbt', 'project/**') }}
restore-keys: sbt-${{ runner.os }}-
- name: Test
run: |
sbt ++${{ matrix.scala }} \
set ThisBuild / platform := ${{ matrix.platform }} \
clean test
- name: Upload test reports
if: failure()
uses: actions/upload-artifact@v3
with:
name: test-reports-${{ matrix.os }}-${{ matrix.jdk }}-${{ matrix.scala }}-${{ matrix.platform }}
path: target/test-reports/
- name: Package artifact
if: success()
run: sbt ++${{ matrix.scala }} universal:packageBin
- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: dist-${{ matrix.os }}-${{ matrix.jdk }}-${{ matrix.scala }}-${{ matrix.platform }}
path: target/universal/*.zip
要点解释:
- max-parallel=15 把并发压到免费额度内,fail-fast=false 保证所有维度跑完再统一红;
- include/exclude 把 Native/JS 限制在单平台,避免无意义组合;
- 缓存键带 runner.os,彻底解决 Windows 与 Unix 路径冲突;
- 产物名带四维坐标, nightly 合并阶段可用
matrix上下文精准下载; - 失败重跑把
test-reports按同样坐标上传,本地sbt testQuick可直接对比。
拓展思考
- 矩阵爆炸到 200+ 维度时,可用 动态矩阵(dynamic matrix):先跑一个
pre-job用jq生成 JSON 字符串,再通过fromJson注入,把维度压缩到业务真正关心的白名单; - 私有 Runner(阿里云 ECS/华为云 CCE)+ 标签路由,把 Native 编译任务调度到预装 llvm 的裸金属节点,成本比 GitHub 付费机降低 60%;
- 多阶段缓存:把
~/.sbt/boot与~/.ivy2/cache拆成两层,boot 层按 sbt 版本做 key,cache 命中率提升 20%; - 回归测试分层:矩阵只跑
unit test,integration test用workflow_dispatch手动触发,把 PR 阶段耗时从 25 min 压到 8 min; - 合规审计:国内上市要求“源代码不可出境”,可在矩阵里加
runs-on: self-hosted-[cn-north-1],确保编译、测试、打包全链路留在境内; - 未来 Scala Native 0.5 支持 LLVM 15,矩阵里可用
if: matrix.platform == 'native' && runner.os == 'ubuntu-22.04'提前尝鲜,体现技术前瞻性。