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

GitHub多工作流触发异常:仅CI运行,Cypress工作流未触发

问题分析与解决方案

分开工作流的修复方案

你的Cypress工作流未触发,核心是需要调整两个工作流的路径规则,确保各自仅在对应文件变更时触发:

调整CI工作流路径规则

修改build.yml,排除Cypress测试文件,这样仅当非测试文件变更时触发CI工作流,避免不必要的运行:

name: CI

on:
  push:
    paths: 
      - "**/*"
      - "!.github/**/*"
      - "!src/**/*.test-cy.{js,jsx,ts,tsx}"

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [20.x]
    steps:
      - uses: actions/checkout@v3
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
          cache: "npm"
          cache-dependency-path: "package-lock.json"
      - run: npm ci
      - run: npm run build --if-present
      - run: npm run build-storybook --if-present

保留Cypress工作流路径规则

cypress.yml的规则无需修改,确保仅当src目录下的Cypress测试文件变更时触发:

name: Cypress

on:
  push:
    paths: ["src/**/*.test-cy.{js,jsx,ts,tsx}"]

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [20.x]
    steps:
      - uses: actions/checkout@v3
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
          cache: "npm"
          cache-dependency-path: "package-lock.json"
      - run: npm ci
      - run: npm run test:cypress --if-present

合并为单个工作流的方案

如果想把两个任务整合到同一工作流,可通过if条件判断控制每个Job的运行时机:

name: CI & Cypress

on:
  push:
    paths:
      - "**/*"
      - "!.github/**/*"

jobs:
  build:
    runs-on: ubuntu-latest
    # 仅当提交未修改Cypress测试文件时运行
    if: ${{ !any(map(github.event.head_commit.modified, 'endsWith(@, ".test-cy.js") || endsWith(@, ".test-cy.jsx") || endsWith(@, ".test-cy.ts") || endsWith(@, ".test-cy.tsx")')) }}
    strategy:
      matrix:
        node-version: [20.x]
    steps:
      - uses: actions/checkout@v3
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
          cache: "npm"
          cache-dependency-path: "package-lock.json"
      - run: npm ci
      - run: npm run build --if-present
      - run: npm run build-storybook --if-present

  test:
    runs-on: ubuntu-latest
    # 仅当提交修改了Cypress测试文件时运行
    if: ${{ any(map(github.event.head_commit.modified, 'endsWith(@, ".test-cy.js") || endsWith(@, ".test-cy.jsx") || endsWith(@, ".test-cy.ts") || endsWith(@, ".test-cy.tsx")')) }}
    strategy:
      matrix:
        node-version: [20.x]
    steps:
      - uses: actions/checkout@v3
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
          cache: "npm"
          cache-dependency-path: "package-lock.json"
      - run: npm ci
      - run: npm run test:cypress --if-present

注意事项

  • 若推送包含多个提交,github.event.head_commit.modified仅读取最后一个提交的修改文件,可改用遍历所有提交的规则:
    if: ${{ !any(map(github.event.commits.*.modified, 'endsWith(@, ".test-cy.js") || endsWith(@, ".test-cy.jsx") || endsWith(@, ".test-cy.ts") || endsWith(@, ".test-cy.tsx")')) }}
    
  • 若测试文件不在src目录下,需将路径规则调整为**/*.test-cy.{js,jsx,ts,tsx}以匹配所有目录下的测试文件。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 05:05:02