-
Notifications
You must be signed in to change notification settings - Fork 0
219 lines (196 loc) · 8.96 KB
/
vrt.yaml
File metadata and controls
219 lines (196 loc) · 8.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
name: Visual Regression Tests
on:
pull_request:
types: [opened, synchronize, reopened, edited]
branches:
- main
paths:
- "src/**"
- "e2e/**"
- "playwright.config.ts"
- "package.json"
- "pnpm-lock.yaml"
- "next.config.mjs"
- "contentlayer.config.ts"
- "tailwind.config.ts"
- ".github/workflows/vrt.yaml"
workflow_dispatch:
inputs:
update_snapshots:
description: "Update baseline snapshots"
required: false
type: boolean
default: false
env:
R2_BUCKET: blog-vrt
R2_PUBLIC_URL: ${{ secrets.R2_PUBLIC_URL }}
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
PLAYWRIGHT_BROWSERS_PATH: ~/.cache/ms-playwright
jobs:
vrt:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- uses: actions/checkout@v6
- name: Check for update snapshots checkbox
id: check-update
run: |
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
echo "update_snapshots=${{ inputs.update_snapshots }}" >> $GITHUB_OUTPUT
else
# PRボディからチェックボックスを確認
if [[ -z "${{ github.event.pull_request.number }}" ]]; then
echo "update_snapshots=false" >> $GITHUB_OUTPUT
exit 0
fi
PR_BODY=$(gh pr view ${{ github.event.pull_request.number }} --json body -q .body)
if echo "$PR_BODY" | grep -qiE '\[[ ]*[xX][ ]*\].*スナップショットを更新する'; then
echo "update_snapshots=true" >> $GITHUB_OUTPUT
else
echo "update_snapshots=false" >> $GITHUB_OUTPUT
fi
fi
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Install pnpm
uses: pnpm/action-setup@v4
with:
version: 10
- name: Use Node.js
uses: actions/setup-node@v6
with:
node-version: 24.12.0
cache: "pnpm"
- name: Install dependencies
run: pnpm install
- name: Setup Playwright
uses: ./.github/actions/setup-playwright
- name: Build content
run: pnpm build:content
- name: Download baseline snapshots from R2
if: steps.check-update.outputs.update_snapshots != 'true'
run: |
mkdir -p e2e/vrt.spec.ts-snapshots
# -linuxサフィックス付きファイルを試し、なければサフィックスなしをダウンロード
pnpm wrangler r2 object get ${{ env.R2_BUCKET }}/baselines/home-page-chromium-linux.png --file e2e/vrt.spec.ts-snapshots/home-page-chromium-linux.png --remote || \
pnpm wrangler r2 object get ${{ env.R2_BUCKET }}/baselines/home-page-chromium.png --file e2e/vrt.spec.ts-snapshots/home-page-chromium-linux.png --remote || \
echo "Baseline home-page not found"
pnpm wrangler r2 object get ${{ env.R2_BUCKET }}/baselines/article-page-chromium-linux.png --file e2e/vrt.spec.ts-snapshots/article-page-chromium-linux.png --remote || \
pnpm wrangler r2 object get ${{ env.R2_BUCKET }}/baselines/article-page-chromium.png --file e2e/vrt.spec.ts-snapshots/article-page-chromium-linux.png --remote || \
echo "Baseline article-page not found"
- name: Run VRT tests
id: vrt
run: |
if [[ "${{ steps.check-update.outputs.update_snapshots }}" == "true" ]]; then
# スナップショット更新モード - テストをスキップして新規スクリーンショット生成
pnpm exec playwright test vrt.spec.ts --update-snapshots || true
else
# 通常の比較テスト
pnpm exec playwright test vrt.spec.ts || echo "vrt_failed=true" >> $GITHUB_OUTPUT
fi
continue-on-error: true
- name: Upload report to R2
if: always()
run: |
PR_NUM=${{ github.event.pull_request.number || 'manual' }}
RUN_ID=${{ github.run_id }}
if [ -d "playwright-report" ]; then
# playwright-reportディレクトリ内のすべてのファイルをR2にアップロード
find playwright-report -type f | while read file; do
key="${file#playwright-report/}"
pnpm wrangler r2 object put ${{ env.R2_BUCKET }}/reports/pr-${PR_NUM}/run-${RUN_ID}/$key --file "$file" --remote
done
fi
- name: Upload new snapshots to R2 (update mode)
if: steps.check-update.outputs.update_snapshots == 'true'
run: |
PR_NUM=${{ github.event.pull_request.number || 'manual' }}
if [ -d "e2e/vrt.spec.ts-snapshots" ]; then
# スナップショットをR2にアップロード
find e2e/vrt.spec.ts-snapshots -type f -name "*.png" | while read file; do
filename=$(basename "$file")
pnpm wrangler r2 object put ${{ env.R2_BUCKET }}/snapshots/pr-${PR_NUM}/$filename --file "$file" --remote
done
fi
- name: Collect screenshot URLs
id: screenshots
if: always()
run: |
PR_NUM=${{ github.event.pull_request.number || 'manual' }}
RUN_ID=${{ github.run_id }}
REPORT_URL="${{ env.R2_PUBLIC_URL }}/reports/pr-${PR_NUM}/run-${RUN_ID}/index.html"
echo "report_url=${REPORT_URL}" >> $GITHUB_OUTPUT
# 差分画像のURLを収集
DIFF_IMAGES=""
if [ -d "test-results" ]; then
for diff in test-results/**/diff*.png; do
if [ -f "$diff" ]; then
FILENAME=$(basename "$diff")
TESTNAME=$(dirname "$diff" | xargs basename)
# test-resultsディレクトリもR2にアップロード
pnpm wrangler r2 object put ${{ env.R2_BUCKET }}/reports/pr-${PR_NUM}/run-${RUN_ID}/screenshots/${TESTNAME}-${FILENAME} --file "$diff" --remote
DIFF_IMAGES="${DIFF_IMAGES}- \n"
fi
done
fi
echo "diff_images<<EOF" >> $GITHUB_OUTPUT
printf "%b\n" "$DIFF_IMAGES" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Comment on PR
if: always() && github.event_name == 'pull_request'
uses: actions/github-script@v8
with:
script: |
const updateMode = '${{ steps.check-update.outputs.update_snapshots }}' === 'true';
const vrtFailed = '${{ steps.vrt.outputs.vrt_failed }}' === 'true';
const reportUrl = '${{ steps.screenshots.outputs.report_url }}';
const diffImages = `${{ steps.screenshots.outputs.diff_images }}`;
const prNum = context.issue.number;
let body = '## Visual Regression Test Results\n\n';
if (updateMode) {
body += '**Mode**: スナップショット更新\n\n';
body += '新しいスナップショットが生成され、R2にアップロードされました。\n';
body += 'マージ後、ベースライン更新ワークフローが実行されます。\n\n';
} else if (vrtFailed) {
body += '**Status**: :x: ビジュアルの差分を検出\n\n';
body += '### 差分画像\n\n';
if (diffImages) {
body += diffImages + '\n';
}
body += '\n';
} else {
body += '**Status**: :white_check_mark: ビジュアルの差分なし\n\n';
}
body += `[レポートを確認](${reportUrl})\n\n`;
body += '---\n';
body += '*これらの変更が意図的なものである場合、PRテンプレートの「スナップショットを更新する」にチェックを入れて再実行してください。*';
// 既存のVRTコメントを検索
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNum,
});
const botComment = comments.find(c =>
c.user.type === 'Bot' && c.body.includes('Visual Regression Test Results')
);
if (botComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNum,
body: body,
});
}
- name: Fail if VRT detected differences
if: steps.vrt.outputs.vrt_failed == 'true' && steps.check-update.outputs.update_snapshots != 'true'
run: exit 1