Lighthouse CI 为啥在 GitHub Actions 里跑不通?

シ爱欢 阅读 2

我在本地用 Lighthouse CI 跑得好好的,但一推到 GitHub Actions 就报错说“Unable to connect to Chrome”。我明明用了官方推荐的 lighthouseci action,也加了 --collect.settings.chromeFlags='--no-sandbox',还是不行。

页面本身很简单,就一个带渐变背景的按钮,CSS 长这样:

.btn {
  background: linear-gradient(45deg, #ff6b6b, #ffa5a5);
  border: none;
  padding: 12px 24px;
  color: white;
  border-radius: 8px;
  cursor: pointer;
}

是不是 CI 环境里缺了啥依赖?还是 Chrome 启动参数没配对?真的搞不懂了……

我来解答 赞 3 收藏
二维码
手机扫码查看
1 条解答
Tr° 红凤
问题在于 GitHub Actions 的 Linux 环境跑 Chrome 需要额外的参数,光 --no-sandbox 不够。

在 lighthouseci 的配置里改成这样:

- name: Run Lighthouse CI
uses: treosh/lighthouse-ci-action@v9
with:
urls: |
https://your-site.com
uploadArtifacts: true
temporaryPublicStorage: true
configPath: ./lighthouserc.js


然后在 lighthouserc.js 里配置 Chrome 启动参数:

module.exports = {
ci: {
collect: {
settings: {
chromeFlags: '--no-sandbox --disable-gpu --disable-dev-shm-usage --headless'
}
}
}
};


关键就是 --disable-dev-shm-usage 这个参数,GitHub Actions 的容器默认 /dev/shm 只有 64MB,Chrome 跑不起来。加上这个参数让 Chrome 用磁盘临时文件代替共享内存,就搞定了。
点赞
2026-03-14 13:02