添加Artifacts后GitHub Actions中Cypress运行不触发任务失败
GitHub Actions中Cypress测试失败但流水线不终止的修复方案
问题根源
你的配置存在两个关键问题:
- YAML语法错误:测试步骤的
run和continue-on-error未正确缩进,导致continue-on-error: false未生效,测试失败的退出码未被正确捕获。 if: always()的状态覆盖:虽然Artifact上传步骤能正常执行,但测试失败的状态未被传递到job的最终结果,后续成功的步骤让GitHub误判job状态为成功。
修复步骤
1. 修正测试步骤的YAML缩进
给测试步骤添加id并修正缩进,确保continue-on-error正确应用:
- name: Run e2e Test cases id: cypress run: | cd cypress-sdet npm install --include=dev npm run cypress:run continue-on-error: false
2. 保留Artifact上传并强制job失败
在Artifact上传后添加一个步骤,当测试失败时主动终止job,确保流水线状态正确:
- name: Slack Notification uses: 8398a7/action-slack@v3 with: status: ${{ job.status }} fields: workflow,job,commit,repo,ref,author,took,result custom_payload: | { text: "${{ github.workflow }}: ${{ github.repository }} (${{ github.sha }}) by ${{ github.actor }} (${{ steps.cypress.outcome }})", } if: always() - name: Upload build artifacts uses: actions/upload-artifact@v2 with: name: my-artifact path: cypress-sdet/cypress/reports/mocha/output.json if: always() - name: Fail job on test failure if: steps.cypress.outcome == 'failure' run: exit 1
效果说明
- 测试失败时,
Run e2e Test cases步骤会被标记为失败,if: always()确保Slack通知和Artifact上传仍执行。 - 最后一步
Fail job on test failure会触发,通过exit 1强制job进入失败状态,流水线最终状态与测试结果一致。
内容的提问来源于stack exchange,提问作者akhil desai
相关产品推荐
相关产品推荐

