如何通过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-mainandapi-gateway-mainmeans 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-automationworkflow runsinstallandtestsjobs that both install dependencies—this is unnecessary and wastes time. - Unfinished custom action: Your draft references
my-github-account/cypress-test-actionbut doesn't define how to build that action, so it won't work as-is.
Recommended Solution: Cross-Repo Workflow Triggering (Cleanest & Most Maintainable)
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
- Create a PAT: Go to your GitHub settings → Developer settings → Personal access tokens, generate a token with the
reposcope. - Add the PAT to secrets: In both
front-end-mainandapi-gateway-main, go to Settings → Secrets and variables → Actions → New repository secret, name itPERSONAL_ACCESS_TOKENand paste the token. - Ensure e2e secrets exist: Confirm
CYPRESS_CARGO_BOOKING_PASSWORDandCYPRESS_CARGO_SIGNUP_PASSWORDare still ine2e-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

