Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

web-preview.yml

145 lines5.5 KB

1name: Web Preview
2​
3# Label a PR `preview:web` to get a hosted-web preview deployment on Vercel for
4# that push and every subsequent push. The deployment is a plain (non-prod,
5# non-aliased) deploy into the existing hosted-web Vercel project, so the
6# latest/nightly channel aliases are never touched.
7#
8# The build intentionally omits the T3 Connect cloud config (Clerk keys, relay
9# URL): previews boot as the hosted-static app with manual pairing only. Pair a
10# server into a preview with `t3 pair --tailscale` (or any reachable HTTPS
11# backend) and open the pairing URL against the preview origin.
13# The preview must be opened at the exact deployment URL from the PR comment.
14# Vite bakes that URL in as the hosted origin (via VERCEL_URL), and
15# `isHostedStaticApp` matches on origin, so branch-alias URLs will not
16# self-identify as the hosted app.
17​
18on:
19 pull_request:
20 types: [labeled, synchronize, reopened]
21​
22permissions:
23 contents: read
24​
25concurrency:
26 group: web-preview-${{ github.event.pull_request.number }}
27 cancel-in-progress: true
28​
29jobs:
30 deploy:
31 name: Deploy web preview
32 # Same-repo PRs only: fork PRs do not receive the Vercel secrets, and this
33 # workflow should skip rather than fail for them. On `labeled` events, only
34 # the preview label itself triggers a deploy.
35 if: >-
36 github.event.pull_request.head.repo.full_name == github.repository &&
37 contains(github.event.pull_request.labels.*.name, 'preview:web') &&
38 (github.event.action != 'labeled' || github.event.label.name == 'preview:web')
39 runs-on: blacksmith-8vcpu-ubuntu-2404
40 timeout-minutes: 10
41 permissions:
42 contents: read
43 pull-requests: write
44 steps:
45 - name: Checkout
46 uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
47 with:
48 ref: ${{ github.event.pull_request.head.sha }}
49 sparse-checkout: |
50 /*
51 !/.repos/
52 sparse-checkout-cone-mode: false
53​
54 # The Vercel CLI imports @vercel/config from here to read apps/web/vercel.ts.
55 # No dependency lifecycle script runs in this job.
56 - name: Setup Vite+
57 uses: voidzero-dev/setup-vp@250f29ce396baf5e8f24498e17c0dfdebabc26eb # v1.15.0
58 with:
59 node-version-file: package.json
60 cache: true
61 run-install: |
62 args:
63 - --ignore-scripts
64 - --filter=@t3tools/scripts...
65 - --filter=@t3tools/web...
66​
67 - id: deploy
68 name: Deploy preview
69 shell: bash
70 env:
71 VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }}
72 VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
73 VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
74 VERCEL_TEAM_SLUG: ${{ vars.VERCEL_TEAM_SLUG }}
75 run: |
76 set -euo pipefail
77​
78 if [[ -z "${VERCEL_TOKEN:-}" || -z "${VERCEL_ORG_ID:-}" || -z "${VERCEL_PROJECT_ID:-}" ]]; then
79 echo "Missing one or more required Vercel secrets: VERCEL_TOKEN, VERCEL_ORG_ID, VERCEL_PROJECT_ID." >&2
80 exit 1
81 fi
82​
83 vercel_scope="${VERCEL_TEAM_SLUG:-$VERCEL_ORG_ID}"
84​
85 deployment_output="$(
86 vp dlx vercel@53.1.1 deploy \
87 --archive=tgz \
88 --yes \
89 --token "$VERCEL_TOKEN" \
90 --scope "$vercel_scope" \
91 --build-env "VITE_RELEASE_CHANNEL=preview"
92 )"
93 printf '%s\n' "$deployment_output"
94 # Vercel can print lockfile warnings to stdout before the deployment URL.
95 deployment_url="$(printf '%s\n' "$deployment_output" | grep -E '^https://[[:alnum:].-]+\.vercel\.app$')"
96 if [[ "$deployment_url" == *$'\n'* ]]; then
97 echo "Vercel returned multiple deployment URLs." >&2
98 exit 1
99 fi
100​
101 echo "Deployed $deployment_url"
102 echo "deployment_url=$deployment_url" >> "$GITHUB_OUTPUT"
103​
104 - name: Comment deployment URL
105 uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
106 env:
107 DEPLOYMENT_URL: ${{ steps.deploy.outputs.deployment_url }}
108 HEAD_SHA: ${{ github.event.pull_request.head.sha }}
109 with:
110 script: |
111 const marker = "<!-- web-preview-deploy -->";
112 const body = [
113 marker,
114 "### Web preview",
115 "",
116 `${process.env.DEPLOYMENT_URL} (for ${process.env.HEAD_SHA.slice(0, 7)})`,
117 "",
118 "Open this exact URL — the hosted-app origin is baked in at build time.",
119 "Pair a server into it with `t3 pair --tailscale`, or paste a host + pairing",
120 "code under Settings → Connections.",
121 ].join("\n");
122​
123 const { data: comments } = await github.rest.issues.listComments({
124 owner: context.repo.owner,
125 repo: context.repo.repo,
126 issue_number: context.payload.pull_request.number,
127 per_page: 100,
128 });
129 const existing = comments.find((comment) => comment.body?.includes(marker));
130​
131 if (existing) {
132 await github.rest.issues.updateComment({
133 owner: context.repo.owner,
134 repo: context.repo.repo,
135 comment_id: existing.id,
136 body,
137 });
138 } else {
139 await github.rest.issues.createComment({
140 owner: context.repo.owner,
141 repo: context.repo.repo,
142 issue_number: context.payload.pull_request.number,
143 body,
144 });
145 }