|
| 1 | +# e2e visual baseline update: 维护者手动触发,把指定 PR 的 @visual baseline 更新回 PR 分支。 |
| 2 | +# 只支持同仓库分支 PR;fork PR 不回写 baseline,避免权限和信任边界混乱。 |
| 3 | + |
| 4 | +name: e2e-baseline-update |
| 5 | + |
| 6 | +on: |
| 7 | + workflow_dispatch: |
| 8 | + inputs: |
| 9 | + pr_number: |
| 10 | + description: "PR number whose head branch should receive updated visual baselines" |
| 11 | + required: true |
| 12 | + |
| 13 | +permissions: |
| 14 | + contents: write |
| 15 | + pull-requests: read |
| 16 | + |
| 17 | +concurrency: |
| 18 | + group: e2e-baseline-update-${{ inputs.pr_number }} |
| 19 | + cancel-in-progress: false |
| 20 | + |
| 21 | +jobs: |
| 22 | + update: |
| 23 | + runs-on: ubuntu-22.04 |
| 24 | + container: |
| 25 | + image: mcr.microsoft.com/playwright:v1.61.1-jammy |
| 26 | + env: |
| 27 | + E2E_TARGET: local |
| 28 | + PW_PREVIEW_PORT: "5176" |
| 29 | + steps: |
| 30 | + - name: Resolve PR head |
| 31 | + id: pr |
| 32 | + uses: actions/github-script@v7 |
| 33 | + env: |
| 34 | + PR_NUMBER: ${{ inputs.pr_number }} |
| 35 | + with: |
| 36 | + script: | |
| 37 | + const prNumber = Number(process.env.PR_NUMBER); |
| 38 | + const { owner, repo } = context.repo; |
| 39 | + const { data: pr } = await github.rest.pulls.get({ owner, repo, pull_number: prNumber }); |
| 40 | + if (pr.head.repo.full_name !== `${owner}/${repo}`) { |
| 41 | + core.setFailed(`PR #${prNumber} is from ${pr.head.repo.full_name}; baseline update only pushes to same-repo branches.`); |
| 42 | + return; |
| 43 | + } |
| 44 | + core.setOutput('head_ref', pr.head.ref); |
| 45 | + core.setOutput('head_sha', pr.head.sha); |
| 46 | + core.setOutput('base_ref', pr.base.ref); |
| 47 | + - uses: actions/checkout@v4 |
| 48 | + with: |
| 49 | + ref: ${{ steps.pr.outputs.head_ref }} |
| 50 | + fetch-depth: 0 |
| 51 | + - name: Trust workspace for git |
| 52 | + shell: bash |
| 53 | + run: git config --global --add safe.directory "$GITHUB_WORKSPACE" |
| 54 | + - uses: pnpm/action-setup@v4 |
| 55 | + - uses: actions/setup-node@v4 |
| 56 | + with: |
| 57 | + node-version: "22.16.0" |
| 58 | + cache: "pnpm" |
| 59 | + - name: Install dependencies |
| 60 | + run: pnpm install --frozen-lockfile --prefer-offline |
| 61 | + - name: Build e2e bundle |
| 62 | + working-directory: apps/web |
| 63 | + run: pnpm build:e2e |
| 64 | + - name: Run @p0 business gate before baseline update |
| 65 | + working-directory: apps/web |
| 66 | + shell: bash |
| 67 | + run: | |
| 68 | + set +e |
| 69 | + E2E_LOG="e2e-kit/playwright-report/e2e-baseline-update-p0.log" |
| 70 | + SUMMARY="e2e-kit/playwright-report/e2e-baseline-update-summary.txt" |
| 71 | + mkdir -p "$(dirname "$E2E_LOG")" |
| 72 | + pnpm exec playwright test --config=e2e-kit/playwright.ci.config.ts --grep "@p0" 2>&1 | tee "$E2E_LOG" |
| 73 | + PW_EXIT=${PIPESTATUS[0]} |
| 74 | + set -e |
| 75 | +
|
| 76 | + JUNIT="e2e-kit/playwright-report/junit.xml" |
| 77 | + if [[ ! -f "$JUNIT" ]]; then |
| 78 | + echo "❌ junit.xml missing, refuse to update baseline" |
| 79 | + exit 1 |
| 80 | + fi |
| 81 | +
|
| 82 | + FAILED_NAMES=$(awk ' |
| 83 | + /<testcase / { match($0, /name="[^"]*"/); name = substr($0, RSTART+6, RLENGTH-7); has_fail=0; next } |
| 84 | + /<failure|<error/ { if (name && !has_fail) { print name; has_fail=1 } } |
| 85 | + /<\/testcase>/ { name="" } |
| 86 | + ' "$JUNIT") |
| 87 | + TOTAL_FAIL=$(echo "$FAILED_NAMES" | grep -c . || true) |
| 88 | + VISUAL_FAIL=$(echo "$FAILED_NAMES" | grep "@visual" | grep -vc "@p0" || true) |
| 89 | + BUSINESS_FAIL=$((TOTAL_FAIL - VISUAL_FAIL)) |
| 90 | + TOTAL_TESTS=$(grep -oE 'tests="[0-9]+"' "$JUNIT" | head -1 | grep -oE '[0-9]+' || echo 0) |
| 91 | + SKIPPED=$(grep -oE 'skipped="[0-9]+"' "$JUNIT" | head -1 | grep -oE '[0-9]+' || echo 0) |
| 92 | + ERRORS=$(grep -oE 'errors="[0-9]+"' "$JUNIT" | head -1 | grep -oE '[0-9]+' || echo 0) |
| 93 | + PROXY_ERRORS=$(grep -c "http proxy error:" "$E2E_LOG" || true) |
| 94 | +
|
| 95 | + { |
| 96 | + echo "PW_EXIT=$PW_EXIT" |
| 97 | + echo "TOTAL_TESTS=$TOTAL_TESTS" |
| 98 | + echo "SKIPPED=$SKIPPED" |
| 99 | + echo "ERRORS=$ERRORS" |
| 100 | + echo "BUSINESS_FAIL=$BUSINESS_FAIL" |
| 101 | + echo "VISUAL_FAIL=$VISUAL_FAIL" |
| 102 | + echo "PROXY_ERRORS=$PROXY_ERRORS" |
| 103 | + echo "---FAIL_NAMES---" |
| 104 | + echo "$FAILED_NAMES" |
| 105 | + } > "$SUMMARY" |
| 106 | + cat "$SUMMARY" |
| 107 | +
|
| 108 | + EXPECTED_P0=29 |
| 109 | + if [[ "$TOTAL_TESTS" -eq 0 || "$TOTAL_TESTS" -lt "$EXPECTED_P0" || "$SKIPPED" -gt 0 || "$ERRORS" -gt 0 ]]; then |
| 110 | + echo "❌ @p0 did not really pass cleanly; refuse to update baseline" |
| 111 | + exit 1 |
| 112 | + elif [[ "$PROXY_ERRORS" -gt 0 ]]; then |
| 113 | + echo "❌ Vite proxy error detected; refuse to update baseline" |
| 114 | + exit 1 |
| 115 | + elif [[ "$BUSINESS_FAIL" -gt 0 || "$PW_EXIT" -ne 0 ]]; then |
| 116 | + echo "❌ @p0 business gate failed; refuse to update baseline" |
| 117 | + exit 1 |
| 118 | + fi |
| 119 | + - name: Update visual baseline snapshots |
| 120 | + working-directory: apps/web |
| 121 | + shell: bash |
| 122 | + run: | |
| 123 | + set -euo pipefail |
| 124 | + E2E_LOG="e2e-kit/playwright-report/e2e-baseline-update-visual.log" |
| 125 | + mkdir -p "$(dirname "$E2E_LOG")" |
| 126 | + pnpm exec playwright test --config=e2e-kit/playwright.ci.config.ts \ |
| 127 | + --grep "@visual" --update-snapshots 2>&1 | tee "$E2E_LOG" |
| 128 | + - name: Verify updated visual baselines |
| 129 | + working-directory: apps/web |
| 130 | + shell: bash |
| 131 | + run: | |
| 132 | + set -euo pipefail |
| 133 | + E2E_LOG="e2e-kit/playwright-report/e2e-baseline-update-verify.log" |
| 134 | + mkdir -p "$(dirname "$E2E_LOG")" |
| 135 | + pnpm exec playwright test --config=e2e-kit/playwright.ci.config.ts \ |
| 136 | + --grep "@visual" 2>&1 | tee "$E2E_LOG" |
| 137 | + if grep -R "http proxy error:" e2e-kit/playwright-report/*.log; then |
| 138 | + echo "❌ visual update 出现 Vite proxy error, 拒绝提交 baseline" |
| 139 | + exit 1 |
| 140 | + fi |
| 141 | + - name: Commit snapshots to PR branch |
| 142 | + shell: bash |
| 143 | + env: |
| 144 | + HEAD_REF: ${{ steps.pr.outputs.head_ref }} |
| 145 | + run: | |
| 146 | + set -euo pipefail |
| 147 | + if ! command -v git >/dev/null 2>&1; then |
| 148 | + echo "❌ container image missing git binary" |
| 149 | + exit 1 |
| 150 | + fi |
| 151 | +
|
| 152 | + git config user.name "github-actions[bot]" |
| 153 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 154 | + git add -f apps/web/e2e-kit/screenshots/ |
| 155 | +
|
| 156 | + if git diff --cached --quiet; then |
| 157 | + echo "No visual baseline changes to commit." |
| 158 | + exit 0 |
| 159 | + fi |
| 160 | +
|
| 161 | + git commit -m "chore(e2e): update visual baselines" |
| 162 | + git push origin "HEAD:$HEAD_REF" |
| 163 | + - name: Upload playwright report |
| 164 | + if: always() |
| 165 | + uses: actions/upload-artifact@v4 |
| 166 | + with: |
| 167 | + name: playwright-report-visual-update-${{ inputs.pr_number }} |
| 168 | + path: | |
| 169 | + apps/web/e2e-kit/playwright-report/ |
| 170 | + apps/web/e2e-kit/test-results/ |
| 171 | + retention-days: 30 |
0 commit comments