Skip to content

Commit 5f79fdf

Browse files
authored
chore: update screenshots workflow (#249)
1 parent aaf8d88 commit 5f79fdf

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Setup Playwright
2+
description: Setup and cache Playwright browser binaries
3+
4+
runs:
5+
using: composite
6+
steps:
7+
- name: Resolve package versions
8+
id: resolve-package-versions
9+
shell: bash
10+
run: |
11+
echo "PLAYWRIGHT_BROWSERS_PATH=$HOME/.cache/playwright-bin" >> $GITHUB_ENV
12+
PLAYWRIGHT_VERSION="$(cat package-lock.json | jq --raw-output '.packages."node_modules/playwright".version')"
13+
echo "PLAYWRIGHT_VERSION=$PLAYWRIGHT_VERSION" >> $GITHUB_ENV
14+
15+
- name: Cache Playwright v${{ steps.resolve-package-versions.outputs.PLAYWRIGHT_VERSION }}
16+
uses: actions/cache@v4
17+
id: playwright-cache
18+
with:
19+
key: ${{ runner.os }}-playwright-${{ env.PLAYWRIGHT_VERSION }}
20+
path: ${{ env.PLAYWRIGHT_BROWSERS_PATH }}
21+
restore-keys: |
22+
${{ runner.os }}-playwright-
23+
24+
- name: Install Playwright Dependencies
25+
shell: bash
26+
if: steps.playwright-cache.outputs.cache-hit != 'true'
27+
run: npx playwright install --with-deps --only-shell
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
name: Update Visual Regression Screenshots
2+
3+
on:
4+
issue_comment:
5+
types: [created]
6+
7+
env:
8+
AUTHOR_NAME: 'gravity-ui[bot]'
9+
AUTHOR_EMAIL: '207856522+gravity-ui[bot]@users.noreply.github.com'
10+
COMMIT_MESSAGE: |
11+
test: update visual regression screenshots
12+
13+
Co-authored-by: ${{ github.event.sender.name }} <${{ github.event.sender.id }}+${{ github.event.sender.name }}@users.noreply.github.com>
14+
15+
jobs:
16+
update-screenshots:
17+
runs-on: ubuntu-latest
18+
19+
if: ${{ github.event.issue.pull_request && !github.event.issue.pull_request.merged_at && contains(github.event.comment.body, '/update-screenshots') }}
20+
21+
# one at a time per branch
22+
concurrency:
23+
group: visual-regression-screenshots@${{ github.event.issue.number }}
24+
cancel-in-progress: true
25+
26+
permissions:
27+
contents: write # needs to push changes
28+
29+
steps:
30+
- name: Checkout
31+
uses: actions/checkout@v6
32+
33+
- name: Configure Git
34+
run: |
35+
git config --global user.name "${{ env.AUTHOR_NAME }}"
36+
git config --global user.email "${{ env.AUTHOR_EMAIL }}"
37+
38+
- name: Generate token
39+
id: generate-token
40+
uses: actions/create-github-app-token@v2
41+
with:
42+
app-id: ${{ secrets.GRAVITY_UI_APP_ID }}
43+
private-key: ${{ secrets.GRAVITY_UI_APP_PRIVATE_KEY }}
44+
45+
- name: Checkout pull request
46+
run: gh pr checkout ${{ github.event.issue.number }}
47+
env:
48+
GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
49+
50+
- name: Setup Node.js
51+
uses: actions/setup-node@v6
52+
with:
53+
node-version-file: .nvmrc
54+
55+
- name: Install dependencies
56+
run: npm ci
57+
58+
- name: Install Playwright Browsers
59+
uses: ./.github/actions/setup-playwright
60+
61+
# the magic happens below 🪄
62+
- name: Update Visual Regression Screenshots
63+
run: npm run test -- --update
64+
65+
# check what changed
66+
- name: Check for changes
67+
id: check_changes
68+
run: |
69+
CHANGED_FILES=$(git status --porcelain | awk '{print $2}')
70+
if [ "${CHANGED_FILES:+x}" ]; then
71+
echo "changes=true" >> $GITHUB_OUTPUT
72+
echo "Changes detected"
73+
74+
# save the list for the summary
75+
echo "changed_files<<EOF" >> $GITHUB_OUTPUT
76+
echo "$CHANGED_FILES" >> $GITHUB_OUTPUT
77+
echo "EOF" >> $GITHUB_OUTPUT
78+
echo "changed_count=$(echo "$CHANGED_FILES" | wc -l)" >> $GITHUB_OUTPUT
79+
else
80+
echo "changes=false" >> $GITHUB_OUTPUT
81+
echo "No changes detected"
82+
fi
83+
84+
# commit if there are changes
85+
- name: Commit changes
86+
if: steps.check_changes.outputs.changes == 'true'
87+
run: |
88+
git add -A
89+
git commit -m "${{ env.COMMIT_MESSAGE }}"
90+
91+
- name: Push changes
92+
if: steps.check_changes.outputs.changes == 'true'
93+
run: git push
94+
env:
95+
GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
96+
97+
# pretty summary for humans
98+
- name: Summary
99+
run: |
100+
if [[ "${{ steps.check_changes.outputs.changes }}" == "true" ]]; then
101+
echo "### 📸 Visual Regression Screenshots Updated" >> $GITHUB_STEP_SUMMARY
102+
echo "" >> $GITHUB_STEP_SUMMARY
103+
echo "Successfully updated **${{ steps.check_changes.outputs.changed_count }}** screenshot(s) on PR #${{ github.event.issue.number }}" >> $GITHUB_STEP_SUMMARY
104+
echo "" >> $GITHUB_STEP_SUMMARY
105+
echo "#### Changed Files:" >> $GITHUB_STEP_SUMMARY
106+
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
107+
echo "${{ steps.check_changes.outputs.changed_files }}" >> $GITHUB_STEP_SUMMARY
108+
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
109+
echo "" >> $GITHUB_STEP_SUMMARY
110+
echo "✅ The updated screenshots have been committed and pushed. Your visual regression baseline is now up to date!" >> $GITHUB_STEP_SUMMARY
111+
else
112+
echo "### ℹ️ No Screenshot Updates Required" >> $GITHUB_STEP_SUMMARY
113+
echo "" >> $GITHUB_STEP_SUMMARY
114+
echo "The visual regression test command ran successfully but no screenshots needed updating." >> $GITHUB_STEP_SUMMARY
115+
echo "" >> $GITHUB_STEP_SUMMARY
116+
echo "All screenshots are already up to date! 🎉" >> $GITHUB_STEP_SUMMARY
117+
fi

0 commit comments

Comments
 (0)