GitHub Actions运行Cypress e2e测试提示二进制文件缺失如何解决?
问题原因
- 你在执行
npm ci时使用了--ignore-scripts参数,该参数会跳过所有依赖包的脚本执行,包括Cypress包默认用来下载对应版本二进制文件的postinstall脚本,导致Cypress二进制缺失。 - 当前工作流的缓存规则仅覆盖了
~/.npm目录,没有包含Cypress二进制默认存储的~/.cache/Cypress路径,即使后续正常下载了二进制,下次运行工作流也会丢失需要重新下载。
修复方案
你可以按照以下步骤修改你的.github/workflow/tests.yml配置:
- 在e2e任务中新增Cypress二进制缓存步骤,避免重复下载
- 在安装依赖后新增手动安装Cypress二进制的步骤,规避
--ignore-scripts的影响(如果你项目必须保留--ignore-scripts参数可以用这个方案,不需要保留的话直接把npm ci --ignore-scripts改成npm ci也可以解决问题)
修改后的完整e2e任务配置如下:
e2e: runs-on: ${{ matrix.os }} strategy: matrix: os: [ubuntu-latest] node: [14] env: NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} NPM_TOKEN: ${{ secrets.GITHUB_TOKEN }} steps: - name: Checkout 🛎 uses: actions/checkout@master - name: Setup node env 🏗 and .npmrc file to publish to GitHub Packages uses: actions/setup-node@v2.1.2 with: node-version: ${{ matrix.node }} registry-url: 'https://npm.pkg.github.com' scope: '@observerly' # 新增Cypress二进制缓存配置 - name: Cache Cypress binary 📦 uses: actions/cache@v2 with: path: ~/.cache/Cypress key: ${{ runner.os }}-cypress-${{ hashFiles('**/package-lock.json') }} restore-keys: | ${{ runner.os }}-cypress- - name: Install project dependencies 👨🏻💻 run: npm ci --ignore-scripts # 新增手动安装Cypress二进制步骤 - name: Install Cypress binary run: npx cypress install - name: Run e2e cypress tests 🧪 run: npm run test:e2e:headless
修改后重新运行工作流即可解决二进制缺失的问题。
内容的提问来源于stack exchange,提问作者Micheal J. Roberts
相关产品推荐
相关产品推荐

