sailfork / t3code-github / .github/ workflows/ web-preview.yml

name: Web Preview

# Label a PR `preview:web` to get a hosted-web preview deployment on Vercel for
# that push and every subsequent push. The deployment is a plain (non-prod,
# non-aliased) deploy into the existing hosted-web Vercel project, so the
# latest/nightly channel aliases are never touched.
#
# The build intentionally omits the T3 Connect cloud config (Clerk keys, relay
# URL): previews boot as the hosted-static app with manual pairing only. Pair a
# server into a preview with `t3 pair --tailscale` (or any reachable HTTPS
# backend) and open the pairing URL against the preview origin.
#
# The preview must be opened at the exact deployment URL from the PR comment.
# Vite bakes that URL in as the hosted origin (via VERCEL_URL), and
# `isHostedStaticApp` matches on origin, so branch-alias URLs will not
# self-identify as the hosted app.

on:
  pull_request:
    types: [labeled, synchronize, reopened]

permissions:
  contents: read
  pull-requests: write

concurrency:
  group: web-preview-${{ github.event.pull_request.number }}
  cancel-in-progress: true

jobs:
  deploy:
    name: Deploy web preview
    # Same-repo PRs only: fork PRs do not receive the Vercel secrets, and this
    # workflow should skip rather than fail for them. On `labeled` events, only
    # the preview label itself triggers a deploy.
    if: >-
      github.event.pull_request.head.repo.full_name == github.repository &&
      contains(github.event.pull_request.labels.*.name, 'preview:web') &&
      (github.event.action != 'labeled' || github.event.label.name == 'preview:web')
    runs-on: blacksmith-8vcpu-ubuntu-2404
    timeout-minutes: 10
    env:
      VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }}
      VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
      VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
      VERCEL_TEAM_SLUG: ${{ vars.VERCEL_TEAM_SLUG }}
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          ref: ${{ github.event.pull_request.head.sha }}
          sparse-checkout: |
            /*
            !/.repos/
          sparse-checkout-cone-mode: false

      - name: Setup Vite+
        uses: voidzero-dev/setup-vp@v1
        with:
          node-version-file: package.json
          cache: true
          run-install: |
            args:
              - --filter=@t3tools/scripts...
              - --filter=@t3tools/web...

      - id: deploy
        name: Deploy preview
        shell: bash
        run: |
          set -euo pipefail

          if [[ -z "${VERCEL_TOKEN:-}" || -z "${VERCEL_ORG_ID:-}" || -z "${VERCEL_PROJECT_ID:-}" ]]; then
            echo "Missing one or more required Vercel secrets: VERCEL_TOKEN, VERCEL_ORG_ID, VERCEL_PROJECT_ID." >&2
            exit 1
          fi

          vercel_scope="${VERCEL_TEAM_SLUG:-$VERCEL_ORG_ID}"

          deployment_output="$(
            vp dlx vercel@53.1.1 deploy \
              --archive=tgz \
              --yes \
              --token "$VERCEL_TOKEN" \
              --scope "$vercel_scope" \
              --build-env "VITE_RELEASE_CHANNEL=preview"
          )"
          printf '%s\n' "$deployment_output"
          # Vercel can print lockfile warnings to stdout before the deployment URL.
          deployment_url="$(printf '%s\n' "$deployment_output" | grep -E '^https://[[:alnum:].-]+\.vercel\.app
		
		
		
		
	

)"
          if [[ "$deployment_url" == *
		
		
		
		
	

\n'* ]]; then
            echo "Vercel returned multiple deployment URLs." >&2
            exit 1
          fi

          echo "Deployed $deployment_url"
          echo "deployment_url=$deployment_url" >> "$GITHUB_OUTPUT"

      - name: Comment deployment URL
        uses: actions/github-script@v8
        env:
          DEPLOYMENT_URL: ${{ steps.deploy.outputs.deployment_url }}
          HEAD_SHA: ${{ github.event.pull_request.head.sha }}
        with:
          script: |
            const marker = "<!-- web-preview-deploy -->";
            const body = [
              marker,
              "### Web preview",
              "",
              `${process.env.DEPLOYMENT_URL} (for ${process.env.HEAD_SHA.slice(0, 7)})`,
              "",
              "Open this exact URL — the hosted-app origin is baked in at build time.",
              "Pair a server into it with `t3 pair --tailscale`, or paste a host + pairing",
              "code under Settings → Connections.",
            ].join("\n");

            const { data: comments } = await github.rest.issues.listComments({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: context.payload.pull_request.number,
              per_page: 100,
            });
            const existing = comments.find((comment) => comment.body?.includes(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: context.payload.pull_request.number,
                body,
              });
            }
)\"\n if [[ \"$deployment_url\" == * \\n'* ]]; then\n echo \"Vercel returned multiple deployment URLs.\" >&2\n exit 1\n fi\n\n echo \"Deployed $deployment_url\"\n echo \"deployment_url=$deployment_url\" >> \"$GITHUB_OUTPUT\"\n\n - name: Comment deployment URL\n uses: actions/github-script@v8\n env:\n DEPLOYMENT_URL: ${{ steps.deploy.outputs.deployment_url }}\n HEAD_SHA: ${{ github.event.pull_request.head.sha }}\n with:\n script: |\n const marker = \"\u003c!-- web-preview-deploy -->\";\n const body = [\n marker,\n \"### Web preview\",\n \"\",\n `${process.env.DEPLOYMENT_URL} (for ${process.env.HEAD_SHA.slice(0, 7)})`,\n \"\",\n \"Open this exact URL — the hosted-app origin is baked in at build time.\",\n \"Pair a server into it with `t3 pair --tailscale`, or paste a host + pairing\",\n \"code under Settings → Connections.\",\n ].join(\"\\n\");\n\n const { data: comments } = await github.rest.issues.listComments({\n owner: context.repo.owner,\n repo: context.repo.repo,\n issue_number: context.payload.pull_request.number,\n per_page: 100,\n });\n const existing = comments.find((comment) => comment.body?.includes(marker));\n\n if (existing) {\n await github.rest.issues.updateComment({\n owner: context.repo.owner,\n repo: context.repo.repo,\n comment_id: existing.id,\n body,\n });\n } else {\n await github.rest.issues.createComment({\n owner: context.repo.owner,\n repo: context.repo.repo,\n issue_number: context.payload.pull_request.number,\n body,\n });\n }\n","preview":null,"history":null,"version":"4b3f8952d503db5ef7f402683e1e074909b6988f"},"/_session?shell=1":null}