新手求助:GitLab流水线中Cypress npm包已装但二进制文件缺失
问题根源
你遇到的报错是因为GitLab CI缓存只保留了node_modules目录,但Cypress的二进制文件默认存在用户级缓存目录(比如~/.cache/Cypress),不在node_modules范围内。首次运行npm install时会自动下载二进制文件,但后续复用缓存的node_modules时,二进制文件没被缓存下来,就触发了缺失报错。
解决方案
方案1:调整缓存路径,包含Cypress二进制目录
修改CI配置的cache部分,把Cypress的缓存目录加入缓存路径:
cypress tests: stage: test image: cypress/browsers:node14.17.0-chrome91-ff89 cache: key: package-lock.json paths: - node_modules - ~/.cache/Cypress # 新增这一行,缓存Cypress二进制文件 before_script: - npm install - npm run dev & - ./node_modules/.bin/wait-on http://localhost:3000 script: - npm run cypress only: - merge_requests - master
方案2:让Cypress把二进制存在项目目录下
通过环境变量指定Cypress二进制的存储目录到项目内,这样就能被现有缓存规则覆盖:
cypress tests: stage: test image: cypress/browsers:node14.17.0-chrome91-ff89 cache: key: package-lock.json paths: - node_modules - .cypress-cache # 对应下面设置的存储目录 variables: CYPRESS_CACHE_FOLDER: .cypress-cache # 强制二进制存在项目目录 before_script: - npm install - npm run dev & - ./node_modules/.bin/wait-on http://localhost:3000 script: - npm run cypress only: - merge_requests - master
方案3:安装后强制验证二进制
如果不想改缓存规则,可以在npm install后加验证命令,自动补全缺失的二进制:
cypress tests: stage: test image: cypress/browsers:node14.17.0-chrome91-ff89 cache: key: package-lock.json paths: - node_modules before_script: - npm install - npx cypress verify # 验证二进制,缺失则自动下载 - npm run dev & - ./node_modules/.bin/wait-on http://localhost:3000 script: - npm run cypress only: - merge_requests - master
排查建议
- 查看CI日志里
npm install的输出,对比首次和后续运行时Cypress二进制的下载状态 - 临时注释掉
cache块禁用缓存,看是否还会报错,确认问题是否由缓存导致 - 检查
package.json里cypress的版本,确保和使用的cypress/browsers镜像版本兼容
内容的提问来源于stack exchange,提问作者DraxDomax
相关产品推荐
相关产品推荐

