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

如何通过GitHub Actions在前端及网关仓库PR时触发E2E自动化测试

Hey there! Let's break down your current approach and fix it to get E2E tests running across all three repos smoothly.

First, let's talk about the issues with your draft plan

  • Duplicate code maintenance headache: Copying the entire E2E workflow into front-end-main and api-gateway-main means you'll have to update three files every time you tweak test commands, dependencies, or secrets. That's a recipe for inconsistencies.
  • Redundant dependency installs: Your original e2e-automation workflow runs install and tests jobs that both install dependencies—this is unnecessary and wastes time.
  • Unfinished custom action: Your draft references my-github-account/cypress-test-action but doesn't define how to build that action, so it won't work as-is.

We'll keep all E2E test logic centralized in e2e-automation, then set up the other two repos to trigger this workflow whenever a PR is opened or code is pushed to main.

Step 1: Update the e2e-automation workflow

Modify .github/workflows/main.yml to accept trigger events from other repos, and optimize the existing jobs:

name: Cypress Automation Tests
on:
  # Keep existing triggers for e2e repo itself
  pull_request:
    types: [opened, synchronize, reopened]
  push:
    branches: [main]
  # Add support for cross-repo triggers
  repository_dispatch:
    types: [run-e2e-tests]

env:
  CYPRESS_CARGO_BOOKING_PASSWORD: ${{ secrets.CYPRESS_CARGO_BOOKING_PASSWORD }}
  CYPRESS_CARGO_SIGNUP_PASSWORD: ${{ secrets.CYPRESS_CARGO_SIGNUP_PASSWORD }}

jobs:
  cypress-tests:
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout E2E test code
        uses: actions/checkout@v4

      - name: Install dependencies & run tests
        uses: cypress-io/github-action@v6
        with:
          browser: chrome
          command: npm run cy:run -- --env grepTags="@Regression+-@Failing",ENV="staging"
          # Cache dependencies to skip redundant installs
          cache: true

      - name: Upload screenshots on test failure
        uses: actions/upload-artifact@v4
        if: failure()
        with:
          name: cypress-screenshots
          path: cypress/screenshots
          retention-days: 7 # Auto-delete artifacts after 7 days

      - name: Upload test videos (always)
        uses: actions/upload-artifact@v4
        if: always()
        with:
          name: cypress-videos
          path: cypress/videos
          retention-days: 7

Step 2: Add trigger workflows to front-end-main and api-gateway-main

Create a new file .github/workflows/trigger-e2e.yml in both repos with this content:

name: Trigger E2E Tests
on:
  pull_request:
    types: [opened, synchronize, reopened]
  push:
    branches: [main]

jobs:
  trigger-e2e-workflow:
    runs-on: ubuntu-22.04
    steps:
      - name: Send trigger to e2e-automation repo
        uses: peter-evans/repository-dispatch@v3
        with:
          # Create a PAT (Personal Access Token) with repo scope, add it as a secret in this repo
          token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
          # Replace with your actual e2e-automation repo path (e.g., your-username/e2e-automation)
          repository: your-github-username/e2e-automation
          event-type: run-e2e-tests
          # Optional: Pass context about the frontend/API change (for debugging)
          client-payload: |
            {
              "source-repo": "${{ github.repository }}",
              "branch": "${{ github.head_ref || github.ref_name }}",
              "pr-number": "${{ github.event.pull_request.number || 'N/A' }}"
            }

Step 3: Set up required secrets

  1. Create a PAT: Go to your GitHub settings → Developer settings → Personal access tokens, generate a token with the repo scope.
  2. Add the PAT to secrets: In both front-end-main and api-gateway-main, go to Settings → Secrets and variables → Actions → New repository secret, name it PERSONAL_ACCESS_TOKEN and paste the token.
  3. Ensure e2e secrets exist: Confirm CYPRESS_CARGO_BOOKING_PASSWORD and CYPRESS_CARGO_SIGNUP_PASSWORD are still in e2e-automation's secrets.

Alternative Solution: Reusable Custom Action

If you prefer not to use cross-repo triggers, you can package the E2E tests into a reusable GitHub Action that all three repos can reference.

Step 1: Create the reusable action in e2e-automation

Add a root-level file action.yml to the repo:

name: 'Cypress E2E Regression Tests'
description: 'Run centralized Cypress regression tests'
runs:
  using: 'composite'
  steps:
    - name: Checkout E2E test code
      uses: actions/checkout@v4
      with:
        repository: your-github-username/e2e-automation
        path: e2e-automation

    - name: Install dependencies & run tests
      working-directory: e2e-automation
      uses: cypress-io/github-action@v6
      with:
        browser: chrome
        command: npm run cy:run -- --env grepTags="@Regression+-@Failing",ENV="staging"
        cache: true

    - name: Upload screenshots on failure
      uses: actions/upload-artifact@v4
      if: failure()
      with:
        name: cypress-screenshots
        path: e2e-automation/cypress/screenshots
        retention-days: 7

    - name: Upload test videos
      uses: actions/upload-artifact@v4
      if: always()
      with:
        name: cypress-videos
        path: e2e-automation/cypress/videos
        retention-days: 7

Step 2: Update e2e-automation's workflow to use the action

name: Cypress Automation Tests
on:
  pull_request:
    types: [opened, synchronize, reopened]
  push:
    branches: [main]

env:
  CYPRESS_CARGO_BOOKING_PASSWORD: ${{ secrets.CYPRESS_CARGO_BOOKING_PASSWORD }}
  CYPRESS_CARGO_SIGNUP_PASSWORD: ${{ secrets.CYPRESS_CARGO_SIGNUP_PASSWORD }}

jobs:
  tests:
    runs-on: ubuntu-22.04
    steps:
      - uses: your-github-username/e2e-automation@main
        env:
          CYPRESS_CARGO_BOOKING_PASSWORD: ${{ secrets.CYPRESS_CARGO_BOOKING_PASSWORD }}
          CYPRESS_CARGO_SIGNUP_PASSWORD: ${{ secrets.CYPRESS_CARGO_SIGNUP_PASSWORD }}

Step 3: Add the action to front-end-main and api-gateway-main

Create .github/workflows/run-e2e.yml in both repos:

name: Run E2E Tests
on:
  pull_request:
    types: [opened, synchronize, reopened]
  push:
    branches: [main]

env:
  CYPRESS_CARGO_BOOKING_PASSWORD: ${{ secrets.CYPRESS_CARGO_BOOKING_PASSWORD }}
  CYPRESS_CARGO_SIGNUP_PASSWORD: ${{ secrets.CYPRESS_CARGO_SIGNUP_PASSWORD }}

jobs:
  e2e-tests:
    runs-on: ubuntu-22.04
    steps:
      - uses: your-github-username/e2e-automation@main
        env:
          CYPRESS_CARGO_BOOKING_PASSWORD: ${{ secrets.CYPRESS_CARGO_BOOKING_PASSWORD }}
          CYPRESS_CARGO_SIGNUP_PASSWORD: ${{ secrets.CYPRESS_CARGO_SIGNUP_PASSWORD }}

Note: For this approach, you'll need to copy the two CYPRESS secrets into front-end-main and api-gateway-main, or use organization-level secrets to avoid duplication.


Final Recommendation

Go with the cross-repo trigger solution—it keeps all test logic in one place, avoids duplicate secrets, and makes future updates much easier.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 18:30:56