You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GitLab CI中Cypress二进制文件缺失问题求助

问题:GitLab CI中Cypress二进制文件缺失错误排查与解决

问题背景

基于cypress/included:10.11.0构建自定义Docker镜像并安装mochawesome相关依赖后,在GitLab CI运行测试时,出现Cypress二进制文件缺失的报错。

自定义Dockerfile代码

FROM cypress/included:10.11.0
WORKDIR /usr/app
RUN npm install --save-dev mochawesome
RUN npm install --save-dev mochawesome-merge

GitLab CI配置(gitlab-ci.yml)

variables:
  npm_config_cache: "$CI_PROJECT_DIR/.npm"
  CYPRESS_CACHE_FOLDER: "$CI_PROJECT_DIR/cache/Cypress"

stages:
  - test
  - report

test:
  image: registry.gitlab.com/xxxx/gitlab-ci/cypress-ci
  stage: test
  script:
    - npx cypress run --reporter junit --reporter-options "mochaFile=reports/all-tests.xml,toConsole=true"
  artifacts:
    when: always
    paths:
      - reports/all-tests.xml
  allow_failure: true

遇到的错误

The cypress npm package is installed, but the Cypress binary is missing. We expected the binary to be installed here:
/builds/veloxalpha/tests/front-tests/cache/Cypress/10.11.0/Cypress/Cypress
Reasons it may be missing:

  • You're caching 'node_modules' but are not caching this path: /root/.cache/Cypress
  • You ran 'npm install' at an earlier build step but did not persist: /root/.cache/Cypress Properly caching the binary will fix this error
    and avoid downloading and unzipping Cypress. Alternatively, you can
    run 'cypress install' to download the binary again.

错误原因与解决建议

核心问题

GitLab CI中设置了CYPRESS_CACHE_FOLDER指向项目目录下的cache/Cypress,但基于cypress/included镜像构建时,Cypress二进制文件默认安装在镜像的/root/.cache/Cypress路径,CI运行时无法在自定义缓存目录找到对应文件,导致报错。

解决步骤

  1. 调整Dockerfile,对齐二进制文件路径
    在Dockerfile中添加环境变量,指定Cypress缓存目录为CI配置的路径,确保镜像构建时二进制文件直接安装到该位置:

    FROM cypress/included:10.11.0
    WORKDIR /usr/app
    # 指定Cypress缓存目录与CI配置一致
    ENV CYPRESS_CACHE_FOLDER=/usr/app/cache/Cypress
    RUN npm install --save-dev mochawesome
    RUN npm install --save-dev mochawesome-merge
    
  2. 添加GitLab CI缓存规则
    在CI配置中增加缓存,保留Cypress二进制文件和npm依赖,避免重复下载:

    variables:
      npm_config_cache: "$CI_PROJECT_DIR/.npm"
      CYPRESS_CACHE_FOLDER: "$CI_PROJECT_DIR/cache/Cypress"
    
    stages:
      - test
      - report
    
    cache:
      key: ${CI_COMMIT_REF_SLUG}
      paths:
        - .npm/
        - cache/Cypress/
    
    test:
      image: registry.gitlab.com/xxxx/gitlab-ci/cypress-ci
      stage: test
      script:
        - npx cypress run --reporter junit --reporter-options "mochaFile=reports/all-tests.xml,toConsole=true"
      artifacts:
        when: always
        paths:
          - reports/all-tests.xml
      allow_failure: true
    
  3. 备选方案:CI运行时强制安装二进制
    若不想修改Dockerfile,可在CI脚本中添加cypress install命令,强制下载二进制到指定缓存目录(此方案效率较低,仅作临时替代):

    test:
      # 其他配置不变
      script:
        - npx cypress install
        - npx cypress run --reporter junit --reporter-options "mochaFile=reports/all-tests.xml,toConsole=true"
    

内容的提问来源于stack exchange,提问作者Marcin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 17:30:13