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

GitHub Actions自动Cypress测试未触发问题求助

GitHub Action未触发Cypress测试排查与修复

问题背景

我正在创建一个GitHub Action,期望当目标为staging分支的PR被打开、更新或重新打开时自动运行Cypress测试。当前技术栈为Vue 2、Node 16及兼容版本的Cypress,已编写对应的YAML配置文件及package.json,但创建两个PR后测试未触发,请求帮忙排查解决。

现有配置代码

GitHub Action YAML配置

name: Run Cypress Tests

on:
  pull_request:
    branches:
      - staging
    types: [opened, synchronize, reopened]

jobs:
  cypress:
    runs-on: ubuntu-latest
    steps:
      - name: Check out code
        uses: actions/checkout@v2

      - name: Set up Node.js
        uses: actions/setup-node@v2
        with:
          node-version: '16'

      - name: Install dependencies
        run: |
          yarn install
          yarn serve --port 3000

      - name: Run Cypress tests
        run: |
          npx cypress run --headless

      - name: Post comment if Cypress fails
        if: failure()
        run: |
          PR_URL=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
                        "https://api.github.com/repos/${{ github.repository }}/pulls" | jq -r '.[0].url')
          
          COMMENT="The Cypress tests failed. Please fix the issues before merging."
          curl -X POST -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
          -d "{\"body\": \"$COMMENT\"}" \
          "https://api.github.com/repos/${{ github.repository }}/issues/comments"
          
          # Make GitHub request changes if Cypress fails
          curl -X POST -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
          -d "{\"event\": \"REQUEST_CHANGES\", \"body\": \"Cypress tests have failed. Please fix the issues.\"}" \
          "https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/reviews"

package.json配置

{
  "name": "front",
  "version": "1.1.2",
  "private": true,
  "engines": {
    "npm": ">=6.0.0 <9.0.0",
    "node": ">=14.0.0 <17.0.0"
  },
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint",
    "deploy": "aws --profile vue-deployer --region us-east-1 s3 sync ./dist s3://pichau-erp-front --delete"
  },
  "dependencies": {
    "axios": "^0.21.1",
    "bootstrap": "^5.2.0",
    "buffer": "^6.0.3",
    "chart.js": "^2.9.4",
    "core-js": "^3.6.5",
    "jquery": "^3.6.0",
    "loader-utils": "^3.2.1",
    "lodash": "^4.17.21",
    "minimist": "^1.2.7",
    "moment": "^2.29.4",
    "pinia": "^2.1.7",
    "popper.js": "^1.16.1",
    "sockjs-client": "^1.6.1",
    "sweetalert2": "^11.10.6",
    "v-mask": "^2.3.0",
    "v-money": "^0.8.1",
    "vee-validate": "^2.2.15",
    "vue": "^2.7.3",
    "vue-chartjs": "^3.5.1",
    "vue-jwt-decode": "^0.1.0",
    "vue-notification": "^1.3.20",
    "vue-router": "^3.2.0",
    "vue-simple-alert": "^1.1.1",
    "vuedraggable": "^2.24.3",
    "vuetify": "^2.7.1",
    "vuex": "^3.6.2",
    "webstomp-client": "^1.2.6",
    "xlsx": "^0.18.5",
    "xml-js": "^1.6.11",
    "yarn": "^1.22.22"
  },
  "devDependencies": {
    "@babel/core": "^7.20.2",
    "@babel/eslint-parser": "^7.19.1",
    "@vue/cli-plugin-babel": "^5.0.8",
    "@vue/cli-plugin-eslint": "^5.0.8",
    "@vue/cli-plugin-router": "^5.0.8",
    "@vue/cli-plugin-vuex": "^5.0.8",
    "@vue/cli-service": "^5.0.8",
    "@vue/eslint-config-prettier": "^6.0.0",
    "cypress": "^13.16.0",
    "eslint": "^7.32.0",
    "eslint-plugin-cypress": "2.11.3",
    "eslint-plugin-prettier": "^3.3.1",
    "eslint-plugin-vue": "^6.2.2",
    "prettier": "2.8.8",
    "sass": "~1.32.0",
    "sass-loader": "^10.0.0",
    "vue-cli-plugin-vuetify": "~2.4.2",
    "vue-template-compiler": "^2.6.11",
    "vuetify-loader": "^1.9.2"
  }
}

排查与修复步骤

1. 触发条件分支配置验证

GitHub Actions中pull_request事件的branches字段默认匹配PR的目标分支,但如果PR是从fork仓库发起的,需确认仓库是否允许运行外部PR的Action(仓库设置→Actions→General→Workflow permissions,开启允许fork PR运行Action)。

若内部PR仍未触发,可明确指定base分支强化匹配逻辑:

on:
  pull_request:
    types: [opened, synchronize, reopened]
    base: staging

2. 服务启动阻塞问题

当前配置中yarn serve --port 3000会持续占用终端,导致后续Cypress步骤无法执行。需将服务后台启动,并等待服务就绪后再运行测试:

  • 修改服务启动步骤:
    - name: Install dependencies and start dev server
      run: |
        yarn install
        yarn add -D wait-on
        yarn serve --port 3000 &
        yarn wait-on http://localhost:3000
    

3. 评论提交逻辑错误

当前评论提交的API路径错误且冗余获取PR信息,修正如下:

- name: Post comment if Cypress fails
  if: failure()
  run: |
    COMMENT="The Cypress tests failed. Please fix the issues before merging."
    # 提交评论到当前PR
    curl -X POST -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
    -H "Content-Type: application/json" \
    -d "{\"body\": \"$COMMENT\"}" \
    "https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments"
    
    # 请求修改
    curl -X POST -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
    -H "Content-Type: application/json" \
    -d "{\"event\": \"REQUEST_CHANGES\", \"body\": \"Cypress tests have failed. Please fix the issues.\"}" \
    "https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/reviews"

同时需确保仓库Workflow权限设置为Read and write permissions,否则GITHUB_TOKEN无权限提交评论和请求修改。

4. 版本兼容性验证

  • Node 16符合package.json中engines的>=14.0.0 <17.0.0要求,无需调整。
  • Cypress 13.16.0与Vue 2.7.3兼容,若测试运行报错,可尝试降级到Cypress 12.x版本(更稳定适配Vue 2)。

最终修正后的YAML配置

name: Run Cypress Tests

on:
  pull_request:
    types: [opened, synchronize, reopened]
    base: staging

jobs:
  cypress:
    runs-on: ubuntu-latest
    steps:
      - name: Check out code
        uses: actions/checkout@v2

      - name: Set up Node.js
        uses: actions/setup-node@v2
        with:
          node-version: '16'

      - name: Install dependencies and start dev server
        run: |
          yarn install
          yarn add -D wait-on
          yarn serve --port 3000 &
          yarn wait-on http://localhost:3000

      - name: Run Cypress tests
        run: npx cypress run --headless

      - name: Post comment if Cypress fails
        if: failure()
        run: |
          COMMENT="The Cypress tests failed. Please fix the issues before merging."
          curl -X POST -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
          -H "Content-Type: application/json" \
          -d "{\"body\": \"$COMMENT\"}" \
          "https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments"
          
          curl -X POST -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
          -H "Content-Type: application/json" \
          -d "{\"event\": \"REQUEST_CHANGES\", \"body\": \"Cypress tests have failed. Please fix the issues.\"}" \
          "https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/reviews"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 01:24:56