|
| 1 | +# E2E Tests against Vercel Preview Deployments |
| 2 | +# |
| 3 | +# This workflow runs Playwright tests against Vercel preview deployments. |
| 4 | +# It is triggered automatically by Vercel when a deployment to preview succeeds. |
| 5 | +# |
| 6 | +# How it works: |
| 7 | +# 1. Vercel deploys your code to a preview URL |
| 8 | +# 2. On success, Vercel sends a repository_dispatch event to GitHub |
| 9 | +# 3. This workflow receives the event with the preview URL in the payload |
| 10 | +# 4. Playwright tests run against the live Vercel preview deployment |
| 11 | +# |
| 12 | +# This provides confidence before merging to main (which triggers production deploy). |
| 13 | +# |
| 14 | +# Reference: https://vercel.com/guides/how-can-i-run-end-to-end-tests-after-my-vercel-preview-deployment |
| 15 | + |
| 16 | +name: E2E Tests (Vercel Preview) |
| 17 | + |
| 18 | +permissions: |
| 19 | + contents: read |
| 20 | + statuses: write |
| 21 | + |
| 22 | +on: |
| 23 | + # Triggered by Vercel when a deployment succeeds |
| 24 | + repository_dispatch: |
| 25 | + types: |
| 26 | + - "vercel.deployment.success" |
| 27 | + |
| 28 | +# Cancel in-progress runs when a newer deployment succeeds on the same branch. |
| 29 | +# Groups by git.ref (branch name) from Vercel's payload, not URL (which is unique per deploy). |
| 30 | +concurrency: |
| 31 | + group: ${{ github.workflow }}-${{ github.event.client_payload.git.ref }} |
| 32 | + cancel-in-progress: true |
| 33 | + |
| 34 | +# Note: If you have Deployment Protection enabled on Vercel, |
| 35 | +# you'll need to configure Protection Bypass for Automation. |
| 36 | +# See: https://vercel.com/docs/deployment-protection/methods-to-bypass-deployment-protection/protection-bypass-automation |
| 37 | + |
| 38 | +jobs: |
| 39 | + e2e-preview: |
| 40 | + name: Run E2E Tests on Preview |
| 41 | + # Only run for repository_dispatch events (safety check) |
| 42 | + if: github.event_name == 'repository_dispatch' |
| 43 | + timeout-minutes: 10 |
| 44 | + runs-on: ubuntu-latest |
| 45 | + |
| 46 | + steps: |
| 47 | + # Step: Checkout the exact commit that was deployed |
| 48 | + # Uses the SHA from Vercel's payload to ensure we test the right code |
| 49 | + - uses: actions/checkout@v6 |
| 50 | + with: |
| 51 | + ref: ${{ github.event.client_payload.git.sha }} |
| 52 | + |
| 53 | + # Step: Setup Node.js |
| 54 | + - uses: actions/setup-node@v6 |
| 55 | + with: |
| 56 | + node-version: "lts/*" |
| 57 | + cache: "npm" |
| 58 | + |
| 59 | + # Step: Install dependencies |
| 60 | + - name: Install dependencies |
| 61 | + run: npm ci |
| 62 | + |
| 63 | + # Step: Install Playwright Browsers |
| 64 | + - name: Install Playwright Browsers |
| 65 | + run: npx playwright install chromium webkit --with-deps |
| 66 | + |
| 67 | + # Step: Run E2E tests against Vercel preview URL |
| 68 | + # No build needed — we're testing the deployed preview, not localhost |
| 69 | + - name: Run Playwright tests |
| 70 | + run: npx playwright test |
| 71 | + env: |
| 72 | + # The Vercel preview URL from the deployment event payload |
| 73 | + # This is passed to playwright.config.ts which uses it as baseURL |
| 74 | + BASE_URL: ${{ github.event.client_payload.url }} |
| 75 | + # Bypass secret for Vercel Deployment Protection (required when protection is enabled) |
| 76 | + # This allows Playwright to access protected preview deployments |
| 77 | + VERCEL_AUTOMATION_BYPASS_SECRET: ${{ secrets.VERCEL_AUTOMATION_BYPASS_SECRET }} |
| 78 | + |
| 79 | + # Step: Upload test report |
| 80 | + - uses: actions/upload-artifact@v5 |
| 81 | + if: ${{ !cancelled() }} |
| 82 | + with: |
| 83 | + name: playwright-report-vercel-preview |
| 84 | + path: .playwright/playwright-report/ |
| 85 | + retention-days: 30 |
| 86 | + |
| 87 | + # Step: Report status back to the PR commit |
| 88 | + # Since repository_dispatch runs on main, we must explicitly report status |
| 89 | + # to the original commit SHA so the PR's required check is satisfied |
| 90 | + - name: Report status to PR |
| 91 | + if: always() |
| 92 | + uses: actions/github-script@v8 |
| 93 | + with: |
| 94 | + script: | |
| 95 | + const state = '${{ job.status }}' === 'cancelled' ? 'error' : '${{ job.status }}'; |
| 96 | + await github.rest.repos.createCommitStatus({ |
| 97 | + owner: context.repo.owner, |
| 98 | + repo: context.repo.repo, |
| 99 | + sha: '${{ github.event.client_payload.git.sha }}', |
| 100 | + state: state, |
| 101 | + context: 'Run E2E Tests on Preview', |
| 102 | + description: 'E2E tests against Vercel preview', |
| 103 | + target_url: '${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}' |
| 104 | + }); |
0 commit comments