-
Notifications
You must be signed in to change notification settings - Fork 60
212 lines (186 loc) · 7.65 KB
/
preview-deploy.yml
File metadata and controls
212 lines (186 loc) · 7.65 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
# Deploys docs preview for fork PRs via workflow_run.
#
# Security model: fork code runs in the untrusted pull_request context
# (preview.yml docs-build job) and produces a static HTML artifact.
# This workflow runs in the base repo context with secrets, but NEVER
# checks out or executes fork code — it only deploys the built artifact.
# The wrangler config is checked out from the PR head (JSON only, not
# executable) so that preview deploys match the PR's infrastructure changes.
#
# For internal PRs, deployment happens directly in preview.yml (docs-deploy job).
name: Preview Deploy (Forks)
on:
workflow_run:
workflows: ["Preview"]
types: [completed]
permissions:
contents: write
pull-requests: write
concurrency:
group: preview-deploy-${{ github.event.workflow_run.id }}
cancel-in-progress: true
jobs:
deploy:
name: Docs Deploy (Fork)
# Only run for:
# 1. Successful workflow runs
# 2. Pull request events (not push)
# 3. Fork PRs only (internal PRs deploy in preview.yml directly)
if: >-
github.event.workflow_run.conclusion == 'success' &&
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.head_repository.full_name != github.repository
runs-on: ubuntu-latest
timeout-minutes: 10
outputs:
preview_url: ${{ steps.deploy.outputs.preview_url }}
pr_number: ${{ steps.metadata.outputs.pr_number }}
steps:
- name: Checkout (wrangler config only)
uses: actions/checkout@v4
with:
sparse-checkout: packages/kumo-docs-astro/wrangler.jsonc
ref: ${{ github.event.workflow_run.head_sha }}
- name: Resolve PR metadata
id: metadata
uses: actions/github-script@v7
with:
script: |
// workflow_run.pull_requests can be empty for fork PRs.
// Fall back to the Pulls API when that happens.
const prs = context.payload.workflow_run.pull_requests;
if (prs && prs.length > 0) {
core.setOutput('pr_number', prs[0].number.toString());
core.setOutput('head_sha', prs[0].head.sha);
return;
}
// Fallback: find the PR matching the head SHA via API
const headSha = context.payload.workflow_run.head_sha;
const headBranch = context.payload.workflow_run.head_branch;
const { data: pulls } = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
head: `${context.payload.workflow_run.head_repository.full_name.split('/')[0]}:${headBranch}`,
});
const match = pulls.find(pr => pr.head.sha === headSha);
if (!match) {
core.setFailed(`No open PR found for SHA ${headSha} on branch ${headBranch}`);
return;
}
core.setOutput('pr_number', match.number.toString());
core.setOutput('head_sha', headSha);
- name: Download docs artifact
uses: actions/download-artifact@v4
with:
name: docs-preview-dist
path: packages/kumo-docs-astro/dist/
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Install wrangler
run: npm install -g wrangler
- name: Deploy docs preview
id: deploy
working-directory: packages/kumo-docs-astro
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
PR_NUMBER: ${{ steps.metadata.outputs.pr_number }}
HEAD_SHA: ${{ steps.metadata.outputs.head_sha }}
run: |
COMMIT_SHORT="${HEAD_SHA:0:7}"
VERSION_OUTPUT=$(wrangler versions upload --env="" --message "PR #${PR_NUMBER} (${COMMIT_SHORT})" 2>&1) || true
echo "$VERSION_OUTPUT"
if ! echo "$VERSION_OUTPUT" | grep -q "Worker Version ID:"; then
echo "::error::wrangler versions upload failed -- no Worker Version ID in output"
exit 1
fi
PREVIEW_URL=$(echo "$VERSION_OUTPUT" | grep -oE 'Version Preview URL: https://[^ ]+' | sed 's/Version Preview URL: //')
if [ -z "$PREVIEW_URL" ]; then
VERSION_ID=$(echo "$VERSION_OUTPUT" | grep -oE 'Worker Version ID: [a-f0-9-]+' | sed 's/Worker Version ID: //')
if [ -n "$VERSION_ID" ]; then
VERSION_PREFIX=$(echo "$VERSION_ID" | cut -c1-8)
PREVIEW_URL="https://${VERSION_PREFIX}-kumo-docs.design-engineering.workers.dev"
fi
fi
if [ -z "$PREVIEW_URL" ]; then
echo "::error::Failed to determine docs preview URL"
exit 1
fi
echo "preview_url=$PREVIEW_URL" >> "$GITHUB_OUTPUT"
- name: Comment on PR
uses: actions/github-script@v7
env:
PREVIEW_URL: ${{ steps.deploy.outputs.preview_url }}
HEAD_SHA: ${{ steps.metadata.outputs.head_sha }}
PR_NUMBER: ${{ steps.metadata.outputs.pr_number }}
with:
script: |
const marker = '<!-- kumo-docs-preview -->';
const previewUrl = process.env.PREVIEW_URL;
const headSha = process.env.HEAD_SHA;
const prNumber = parseInt(process.env.PR_NUMBER, 10);
const body = [
marker,
'### Docs Preview',
'',
`[View docs preview](${previewUrl})`,
'',
`Commit: \`${headSha.substring(0, 7)}\``,
].join('\n');
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
const existing = comments.find(c => c.body?.startsWith(marker));
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body,
});
}
visual-regression:
name: Visual Regression (Fork)
needs: deploy
if: ${{ needs.deploy.outputs.preview_url }}
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- name: Checkout
uses: actions/checkout@v4
with:
# Checkout the fork's PR head so git diff can compare against main
ref: ${{ github.event.workflow_run.head_sha }}
fetch-depth: 0
- name: Install Dependencies
uses: ./.github/actions/install-dependencies
with:
filter: "@cloudflare/kumo..."
- name: Run Visual Regression
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_PR_NUMBER: ${{ needs.deploy.outputs.pr_number }}
GITHUB_REPOSITORY: ${{ github.repository }}
GITHUB_BASE_REF: main
BEFORE_URL: "https://kumo-ui.com"
AFTER_URL: ${{ needs.deploy.outputs.preview_url }}
SCREENSHOT_WORKER_URL: ${{ secrets.SCREENSHOT_WORKER_URL }}
SCREENSHOT_API_KEY: ${{ secrets.SCREENSHOT_API_KEY }}
run: pnpm tsx ci/visual-regression/run-visual-regression.ts
- name: Upload Screenshots
uses: actions/upload-artifact@v4
if: always()
with:
name: visual-regression-screenshots-fork
path: ci/visual-regression/screenshots/
retention-days: 7