如何在 CI 中把 a11y 错误阈值设为严重级别,一旦不通过即阻断发布
解读
面试官真正想考察的是:
- 你是否了解 Storybook 官方无障碍插件 @storybook/addon-a11y 的运行机制与报告格式;
- 能否把插件输出的 violations 与 CI 门禁(GitLab CI、GitHub Actions、Jenkins 等)打通,实现“严重级别即阻断”的硬规则;
- 是否具备在 国内网络环境 下落地该方案的经验,如镜像源、私有 NPM、离线扫描等;
- 能否兼顾 性能与稳定性,避免把扫描任务变成发布瓶颈。
一句话:不是“跑个命令”,而是“让流水线在严重 a11y 问题面前直接失败”。
知识点
- @storybook/addon-a11y 的底层是 axe-core, violations 数组里每条记录包含 impact 字段,值域为 minor、moderate、serious、critical。
- Storybook 6.5+ 支持 test-runner(基于 Jest + Playwright),官方提供 --coverage 与 --a11y 参数,可在无头环境批量跑所有 story。
- 通过 jest-playwright 的 page.evaluate 可以拿到 axe.run() 原始结果,自定义断言函数即可按 impact 过滤。
- 国内常用 CI:
- GitLab CI:通过 artifacts:reports: junit 把 a11y 报告变成合并请求里的红线;
- GitHub Actions:利用 job.status = failure 配合 actions/upload-artifact 留痕;
- Jenkins:在 pipeline 里调用 sh “npm run test:storybook:a11y”,根据退出码 0/1 决定是否 error;
- 阻断策略:
- 仅拦截 impact === 'critical';
- 允许 known-issues.json 做 baseline 豁免,但新增 critical 仍需人工 review;
- 性能优化:
- 在 Docker 镜像里预装 playwright 浏览器,避免每次下载 Chromium;
- 使用 --shard 参数并行跑 story;
- 把 build-storybook 与 a11y-scan 拆成两个 job,利用 cache 共享静态资源。
答案
下面给出一套可直接落地的 GitLab CI 模板,核心思路同样适用于 GitHub Actions / Jenkins,只需替换语法。
步骤 1:安装依赖
npm i -D @storybook/test-runner @storybook/addon-a11y jest-junit
步骤 2:在项目根目录新建 .storybook/test-runner.js
const { getStoryContext } = require('@storybook/test-runner');
module.exports = {
async postRender(page, context) {
// 只拦截 critical
const violations = await page.evaluate(async () => {
const results = await window.axe.run();
return results.violations.filter(v => v.impact === 'critical');
});
if (violations.length > 0) {
// 打印到终端,方便 CI 日志定位
console.error('A11Y CRITICAL:', JSON.stringify(violations, null, 2));
// 让 Jest 断言失败,进程退出码为 1
expect(violations).toEqual([]);
}
},
};
步骤 3:package.json 脚本
"scripts": {
"build-storybook": "storybook build",
"test:storybook:a11y": "test-storybook --url http://127.0.0.1:8080 --junit"
}
步骤 4:GitLab CI 片段(国内镜像加速)
variables:
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: 0
NPM_CONFIG_REGISTRY: https://registry.npmmirror.com
stages:
- build
- test
build:storybook:
stage: build
image: node:18-alpine
script:
- npm ci
- npm run build-storybook
artifacts:
paths:
- storybook-static
expire_in: 1 day
test:a11y:
stage: test
image: mcr.microsoft.com/playwright:v1.40.0-focal
dependencies:
- build:storybook
script:
# 启动静态服务器
- npx http-server storybook-static -p 8080 --silent &
- npx wait-on http://127.0.0.1:8080
# 运行 a11y 扫描,一旦 critical 存在就会退出码 1
- npm run test:storybook:a11y
artifacts:
when: always
reports:
junit: junit.xml
expire_in: 7 days
allow_failure: false # 关键点:不允许失败,直接阻断合并/发布
结果:
- 若存在 critical 级别 violations,test:a11y job 失败,GitLab MR 出现红线,后续 deploy 阶段不会执行,实现“一旦不通过即阻断发布”。
- 日志里可看到具体组件、story 名称、违反的 WCAG 条款,方便研发立即修复。
拓展思考
- 阈值分级:如果业务暂时无法一步到位,可把策略拆成 “warning 仅注释,critical 才阻断”,通过 impact 阈值 环境变量控制,方便灰度。
- 双轨报告:把同一套 axe-core 结果同时输出 junit(给 CI 看)与 HTML(给设计师看),实现 “技术红线 + 体验可视化” 双重闭环。
- 组件库场景:在 Lerna / Monorepo 下,利用 --shard 按照包维度并行,10 分钟任务可压缩到 2 分钟,保证 大库也能每日多次发布。
- 合规审计:国内金融、政府项目常要求 GB/T 37668-2019(等效 WCAG 2.1),可在 test-runner 里自定义 tags,只扫描 LEVEL_AA 规则集,报告直接用于第三方审计,减少重复检测费用。