-
Notifications
You must be signed in to change notification settings - Fork 0
340 lines (309 loc) · 12.2 KB
/
preview-seed.yml
File metadata and controls
340 lines (309 loc) · 12.2 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
name: preview-validation
on:
deployment_status:
permissions:
contents: read
deployments: read
pull-requests: write
jobs:
seed-preview:
name: Preview validation (seed + runtime QA)
if: >
github.event.deployment_status.state == 'success' &&
(
startsWith(github.event.deployment.environment || '', 'Preview') ||
startsWith(github.event.deployment_status.environment || '', 'Preview')
)
concurrency:
group: preview-seed-${{ github.event.deployment.id }}
cancel-in-progress: false
runs-on: blacksmith-2vcpu-ubuntu-2404
env:
PREVIEW_URL: ${{ github.event.deployment_status.environment_url || github.event.deployment_status.target_url || github.event.deployment.environment_url || '' }}
DEPLOYMENT_ID: ${{ github.event.deployment.id }}
DEPLOYMENT_REF: ${{ github.event.deployment.ref }}
DEPLOYMENT_SHA: ${{ github.event.deployment.sha }}
CONVEX_DEPLOY_KEY: ${{ secrets.CONVEX_DEPLOY_KEY_PREVIEW }}
BROWSERBASE_API_KEY: ${{ secrets.BROWSERBASE_API_KEY }}
BROWSERBASE_PROJECT_ID: ${{ secrets.BROWSERBASE_PROJECT_ID }}
VERCEL_AUTOMATION_BYPASS_SECRET: ${{ secrets.VERCEL_AUTOMATION_BYPASS_SECRET }}
steps:
- name: Resolve pull request context
id: pr
uses: actions/github-script@v8
env:
DEPLOY_REF: ${{ env.DEPLOYMENT_REF }}
DEPLOY_SHA: ${{ env.DEPLOYMENT_SHA }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const rawRef = process.env.DEPLOY_REF ?? "";
const rawSha = process.env.DEPLOY_SHA ?? "";
const repo = context.repo;
const isCommitSha = (value) => /^[0-9a-f]{40}$/i.test(value);
const setPrOutputs = (pr, reason) => {
core.info(`Resolved PR #${pr.number} (${reason}).`);
const isReleaseVersionBumpPr =
/^chore\/bump-version-/.test(pr.head?.ref ?? "") &&
(pr.user?.login === "github-actions[bot]" || pr.user?.login === "app/github-actions");
if (isReleaseVersionBumpPr) {
core.info(`PR #${pr.number} is an automated release bump; skipping preview seed.`);
core.setOutput("skip", "true");
return;
}
core.setOutput("skip", "false");
core.setOutput("number", String(pr.number));
core.setOutput("title", pr.title ?? "");
core.setOutput("headSha", pr.head?.sha ?? "");
};
const resolveByCommitSha = async (commitSha) => {
if (!commitSha) {
return null;
}
core.info(`Looking up PR associated with commit ${commitSha}.`);
const { data: associated } =
await github.rest.repos.listPullRequestsAssociatedWithCommit({
...repo,
commit_sha: commitSha,
per_page: 20,
});
const openPr = associated.find((pr) => pr.state === "open");
if (openPr) {
return openPr;
}
return null;
};
const match = rawRef.match(/^refs\/pull\/(\d+)\/merge$/);
if (match) {
const pull_number = Number.parseInt(match[1], 10);
const { data: pr } = await github.rest.pulls.get({
...repo,
pull_number,
});
if (pr.state !== "open") {
core.info(`PR #${pull_number} is not open; skipping preview seed.`);
core.setOutput("skip", "true");
return;
}
setPrOutputs(pr, `deployment ref ${rawRef}`);
return;
}
const normalized =
rawRef.startsWith("refs/heads/") ?
rawRef.replace("refs/heads/", "") :
rawRef;
if (normalized && !isCommitSha(normalized)) {
const { data: prs } = await github.rest.pulls.list({
...repo,
head: `${repo.owner}:${normalized}`,
state: "open",
per_page: 1,
});
if (prs.length > 0) {
setPrOutputs(prs[0], `branch ${normalized}`);
return;
}
core.info(`No open PR found by branch ${normalized}.`);
}
const commitCandidate =
isCommitSha(normalized) ? normalized : (isCommitSha(rawSha) ? rawSha : "");
const commitPr = await resolveByCommitSha(commitCandidate);
if (commitPr) {
setPrOutputs(commitPr, `commit ${commitCandidate}`);
return;
}
core.info(
`No open PR found for deployment ref '${rawRef}' and sha '${rawSha}'; skipping preview seed.`,
);
core.setOutput("skip", "true");
- name: Skip when deployment has no matching PR
if: steps.pr.outputs.skip == 'true'
run: |
echo "Preview deployment ${DEPLOYMENT_ID} is not tied to an open PR; skipping seed run."
- name: Validate preview URL
if: steps.pr.outputs.skip != 'true'
run: |
if [ -z "${PREVIEW_URL:-}" ]; then
echo "Deployment status event did not include an environment URL."
exit 1
fi
case "${PREVIEW_URL}" in
http://*|https://*) ;;
*)
echo "Deployment URL '${PREVIEW_URL}' is invalid; expected http(s) URL."
exit 1
;;
esac
echo "Seeding preview deployment at ${PREVIEW_URL}"
- name: Check Convex credentials
if: steps.pr.outputs.skip != 'true'
run: |
if [ -z "${CONVEX_DEPLOY_KEY:-}" ]; then
echo "CONVEX_DEPLOY_KEY_PREVIEW secret is not configured."
exit 1
fi
- name: Check Browserbase credentials
if: steps.pr.outputs.skip != 'true'
run: |
if [ -z "${BROWSERBASE_API_KEY:-}" ]; then
echo "BROWSERBASE_API_KEY secret is not configured."
exit 1
fi
if [ -z "${BROWSERBASE_PROJECT_ID:-}" ]; then
echo "BROWSERBASE_PROJECT_ID secret is not configured."
exit 1
fi
- name: Check Vercel automation bypass credential
if: steps.pr.outputs.skip != 'true'
run: |
if [ -z "${VERCEL_AUTOMATION_BYPASS_SECRET:-}" ]; then
echo "VERCEL_AUTOMATION_BYPASS_SECRET secret is not configured."
exit 1
fi
- name: Checkout
if: steps.pr.outputs.skip != 'true'
uses: actions/checkout@v6
- name: Setup Bun
if: steps.pr.outputs.skip != 'true'
uses: oven-sh/setup-bun@v2
with:
bun-version: 1.3.11
- name: Install dependencies
if: steps.pr.outputs.skip != 'true'
run: bun install --frozen-lockfile
- name: Install agent-browser CLI
if: steps.pr.outputs.skip != 'true'
run: npm install -g agent-browser@latest
- name: Install Playwright Chromium
if: steps.pr.outputs.skip != 'true'
run: npx playwright install --with-deps chromium
- name: Seed preview catalog
id: seed
if: steps.pr.outputs.skip != 'true'
env:
VERCEL_URL: ${{ env.PREVIEW_URL }}
run: |
set -euo pipefail
LOG_FILE="$RUNNER_TEMP/preview-seed.log"
echo "log=$LOG_FILE" >> "$GITHUB_OUTPUT"
{
echo "Target preview: ${VERCEL_URL}"
bun run scripts/seed-vercel-deployment.ts --vercel-url "${VERCEL_URL}"
} | tee "$LOG_FILE"
- name: Run browser QA on preview deployment
if: steps.pr.outputs.skip != 'true'
run: |
USE_BROWSERBASE=1 \
bash ./scripts/browser/run-local-browser-qa.sh \
--external-url "${PREVIEW_URL}" \
--out-dir artifacts/browser-qa-preview
- name: Read replay metadata
id: browser_qa_meta
if: steps.pr.outputs.skip != 'true'
run: |
REPLAY_URL=$(jq -r '.browserbaseReplayUrl // empty' artifacts/browser-qa-preview/results.json)
echo "replay_url=${REPLAY_URL}" >> "$GITHUB_OUTPUT"
- name: Upload browser QA artifacts
id: upload_browser_qa
if: steps.pr.outputs.skip != 'true'
uses: actions/upload-artifact@v7
with:
name: browser-qa-preview-${{ github.event.deployment.id }}
path: artifacts/browser-qa-preview/**
if-no-files-found: error
- name: Comment browser QA links on PR
if: steps.pr.outputs.skip != 'true'
uses: actions/github-script@v8
env:
ARTIFACT_URL: ${{ steps.upload_browser_qa.outputs.artifact-url }}
REPLAY_URL: ${{ steps.browser_qa_meta.outputs.replay_url }}
PR_NUMBER: ${{ steps.pr.outputs.number }}
with:
script: |
const marker = '<!-- browser-qa-report -->';
const issue_number = Number.parseInt(process.env.PR_NUMBER ?? "", 10);
if (!Number.isFinite(issue_number) || issue_number <= 0) {
core.info("No PR number found; skipping browser QA comment.");
return;
}
const artifactUrl = process.env.ARTIFACT_URL ?? '';
const replayUrl = process.env.REPLAY_URL ?? '';
const bodyLines = [
marker,
'## Browser QA (agent-browser)',
artifactUrl
? `- Screenshots artifact: [browser-qa](${artifactUrl})`
: '- Screenshots artifact: unavailable',
replayUrl
? `- Browserbase replay: [session replay](${replayUrl})`
: '- Browserbase replay: unavailable',
];
const body = bodyLines.join('\n');
const { owner, repo } = context.repo;
const comments = await github.paginate(github.rest.issues.listComments, {
owner,
repo,
issue_number,
per_page: 100,
});
const existing = comments.find((comment) =>
typeof comment.body === 'string' && comment.body.includes(marker)
);
if (existing) {
await github.rest.issues.updateComment({
owner,
repo,
comment_id: existing.id,
body,
});
return;
}
await github.rest.issues.createComment({
owner,
repo,
issue_number,
body,
});
- name: Publish summary
if: steps.pr.outputs.skip != 'true' && always()
env:
SUMMARY_STATUS: ${{ job.status }}
PR_NUMBER: ${{ steps.pr.outputs.number }}
PR_TITLE: ${{ steps.pr.outputs.title }}
LOG_PATH: ${{ steps.seed.outputs.log }}
PREVIEW_URL: ${{ env.PREVIEW_URL }}
run: |
{
echo "## Preview seed status"
echo ""
echo "- Deployment ID: ${DEPLOYMENT_ID}"
echo "- Preview URL: ${PREVIEW_URL}"
if [ -n "${PR_NUMBER:-}" ]; then
echo "- PR: #${PR_NUMBER} ${PR_TITLE}"
fi
echo "- Result: ${SUMMARY_STATUS}"
echo "- SHA: ${DEPLOYMENT_SHA}"
} >> "$GITHUB_STEP_SUMMARY"
if [ -f "artifacts/browser-qa-preview/results.json" ]; then
REPLAY_URL=$(jq -r '.browserbaseReplayUrl // empty' artifacts/browser-qa-preview/results.json)
{
echo ""
echo "## Browser QA"
if [ -n "$REPLAY_URL" ]; then
echo "- Browserbase replay: ${REPLAY_URL}"
else
echo "- Browserbase replay: unavailable"
fi
} >> "$GITHUB_STEP_SUMMARY"
fi
if [ -n "${LOG_PATH:-}" ] && [ -f "${LOG_PATH}" ]; then
{
echo ""
echo "<details>"
echo "<summary>Seed output (tail)</summary>"
echo ""
tail -n 50 "${LOG_PATH}"
echo ""
echo "</details>"
} >> "$GITHUB_STEP_SUMMARY"
fi