From d895df1e1553e94936ea89985f81639a1061cfc2 Mon Sep 17 00:00:00 2001 From: Nick Vigilante Date: Mon, 9 Nov 2026 19:15:53 -0500 Subject: [PATCH 1/3] ci: preview router changes from Actions instead of Vercel's Git builds Turning off Vercel's Git-triggered previews, which Security asked for before the repository goes public, would leave a change to apps/router with no preview at all. router-pr-preview.yml builds the pull request's router with `vercel build --standalone`, which needs no Vercel login or token, and deploys the output as a coder-docs preview only after a docs-preview reviewer approves, with the Vercel CLI from the base commit's lockfile. The output travels as a tarball because upload-artifact would flatten the function symlinks, and the deploy job refuses an archive with entries outside output/, a .. segment, special files, or a symlink that dangles or leaves the output. Assisted-by: AI --- .github/workflows/router-pr-preview.yml | 214 ++++++++++++++++++++++++ .gitignore | 3 + AGENTS.md | 1 + README.md | 2 +- 4 files changed, 219 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/router-pr-preview.yml diff --git a/.github/workflows/router-pr-preview.yml b/.github/workflows/router-pr-preview.yml new file mode 100644 index 00000000..0daa6c18 --- /dev/null +++ b/.github/workflows/router-pr-preview.yml @@ -0,0 +1,214 @@ +# Previews a docs-engine pull request's apps/router as a login-protected +# preview deployment of the coder-docs Vercel project, so router changes keep +# a preview once Vercel's Git-triggered previews are turned off. +# +# Same split as docs-pr-preview.yml: `build` compiles the pull request's code +# with no Vercel secret and no Vercel login (`vercel build --standalone` needs +# neither), and `deploy` waits for a `docs-preview` reviewer, checks out the +# base commit's engine for the Vercel CLI, checks the output, and uploads it +# with `--target=preview`, so it can't replace production. +# +# The output is a tarball because upload-artifact would turn the function +# symlinks into copies. Forks are skipped: their runs get no secrets. +name: router-pr-preview + +on: + pull_request: + paths: + - apps/router/** + - packages/docs-config/** + - packages/docs-manifest/** + - package.json + - pnpm-lock.yaml + - pnpm-workspace.yaml + - turbo.json + - .github/workflows/router-pr-preview.yml + +permissions: + contents: read + +concurrency: + group: router-pr-preview-${{ github.event.pull_request.number }} + cancel-in-progress: true + +jobs: + build: + name: build router preview + if: github.event.pull_request.head.repo.full_name == github.repository + runs-on: ubuntu-latest + timeout-minutes: 20 + permissions: + contents: read + steps: + - name: Refuse to run holding a Vercel secret + shell: bash + env: + VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} + run: | + set -euo pipefail + if [[ -n "${VERCEL_TOKEN:-}" ]]; then + echo "VERCEL_TOKEN is visible to the build job; it must be scoped to the docs-preview environment, not the repository" >&2 + exit 1 + fi + + - name: Check out the pull request + uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + persist-credentials: false + + - name: Install pnpm + uses: pnpm/action-setup@b906affcce14559ad1aafd4ab0e942779e9f58b1 # v4 + + - name: Set up Node + uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 + with: + node-version: 22.23.4 + + - name: Install dependencies + run: pnpm install --frozen-lockfile + + - name: Build the router with the Build Output API + shell: bash + env: + # coder-docs's preview value; it's public, and read at build time for the redirects. + ROUTER_BASE_PATH: /docs + run: | + set -euo pipefail + # coder-docs's project settings, written here so the build needs no Vercel login. The ids are never read offline. + mkdir -p .vercel + printf '%s\n' '{"projectId":"prj_offline","orgId":"team_offline","settings":{"framework":"nextjs","rootDirectory":"apps/router","nodeVersion":"24.x","installCommand":null,"buildCommand":null,"devCommand":null,"outputDirectory":null}}' > .vercel/project.json + vercel=(pnpm --filter @coder/offlinedocs exec vercel) + "${vercel[@]}" build --cwd "$GITHUB_WORKSPACE" --standalone --target=preview --yes + tar -C .vercel -czf "$RUNNER_TEMP/router-output.tgz" output + + - name: Upload the build output + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 + with: + name: router-output + path: ${{ runner.temp }}/router-output.tgz + retention-days: 3 + if-no-files-found: error + + deploy: + name: deploy router preview + needs: build + runs-on: ubuntu-latest + timeout-minutes: 15 + environment: docs-preview + permissions: + contents: read + pull-requests: write + statuses: write + steps: + - name: Check the deploy target + shell: bash + env: + VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} + VERCEL_ORG_ID: ${{ vars.VERCEL_ORG_ID }} + VERCEL_PROJECT_ID: ${{ vars.VERCEL_PROJECT_ID }} + run: | + set -euo pipefail + [[ -n "${VERCEL_TOKEN:-}" ]] || { echo "the docs-preview environment has no VERCEL_TOKEN secret" >&2; exit 1; } + [[ "${VERCEL_ORG_ID:-}" =~ ^team_[A-Za-z0-9]+$ ]] || { echo "the docs-preview environment's VERCEL_ORG_ID variable must be a team id" >&2; exit 1; } + [[ "${VERCEL_PROJECT_ID:-}" =~ ^prj_[A-Za-z0-9]+$ ]] || { echo "the docs-preview environment's VERCEL_PROJECT_ID variable must be a project id" >&2; exit 1; } + + - name: Check out docs-engine at the pull request's base commit + # The Vercel CLI that runs next to the token comes from the base's lockfile, not the pull request's. + uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + ref: ${{ github.event.pull_request.base.sha }} + path: engine + persist-credentials: false + + - name: Install pnpm + uses: pnpm/action-setup@b906affcce14559ad1aafd4ab0e942779e9f58b1 # v4 + with: + package_json_file: engine/package.json + + - name: Set up Node + uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 + with: + node-version: 22.23.4 + + - name: Install the Vercel CLI from the lockfile + working-directory: engine + run: pnpm install --frozen-lockfile --filter @coder/offlinedocs + + - name: Download the build output + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + name: router-output + path: ${{ runner.temp }}/artifact + + - name: Check and unpack the build output + # The tarball came from the pull request's code, so it is data: nothing in it runs here, + # and no path from it is printed, since a file name could carry a workflow command. + shell: bash + run: | + set -euo pipefail + archive="$RUNNER_TEMP/artifact/router-output.tgz" + stage="$RUNNER_TEMP/vercel-deploy" + names=$(tar -tzf "$archive") + bad=$(printf '%s\n' "$names" | grep -cvE '^output(/|$)' || true) + [[ "$bad" -eq 0 ]] || { echo "the archive holds $bad entries outside output/" >&2; exit 1; } + bad=$(printf '%s\n' "$names" | grep -cE '(^|/)\.\.(/|$)' || true) + [[ "$bad" -eq 0 ]] || { echo "the archive holds $bad entries with a .. segment" >&2; exit 1; } + rm -rf "$stage" + mkdir -p "$stage/.vercel" + tar -xzf "$archive" -C "$stage/.vercel" --no-same-owner --no-same-permissions + out="$stage/.vercel/output" + [[ -f "$out/config.json" ]] || { echo "the output has no config.json" >&2; exit 1; } + odd=$(find "$out" ! -type f ! -type d ! -type l -printf x | wc -c) + [[ "$odd" -eq 0 ]] || { echo "the output holds $odd special files" >&2; exit 1; } + root=$(realpath -e -- "$out") + escaping=0 + while IFS= read -r -d '' link; do + target=$(realpath -e -- "$link" 2>/dev/null) || { escaping=$((escaping + 1)); continue; } + [[ "$target" == "$root"/* ]] || escaping=$((escaping + 1)) + done < <(find "$out" -type l -print0) + [[ "$escaping" -eq 0 ]] || { echo "$escaping symlinks dangle or point outside the output" >&2; exit 1; } + # coder-docs's Root Directory; the CLI refuses to deploy unless it exists under --cwd. + mkdir -p "$stage/apps/router" + + - name: Deploy the prebuilt output as a coder-docs preview + id: deploy + working-directory: engine/apps/offlinedocs + shell: bash + env: + VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} + VERCEL_ORG_ID: ${{ vars.VERCEL_ORG_ID }} + VERCEL_PROJECT_ID: ${{ vars.VERCEL_PROJECT_ID }} + PR_NUMBER: ${{ github.event.pull_request.number }} + HEAD_SHA: ${{ github.event.pull_request.head.sha }} + run: | + set -euo pipefail + stage="$RUNNER_TEMP/vercel-deploy" + stop=$(openssl rand -hex 16) + echo "::stop-commands::${stop}" + url=$(pnpm exec vercel deploy --cwd "$stage" --prebuilt --target=preview --archive=tgz --yes --meta pr_number="$PR_NUMBER" --meta head_sha="$HEAD_SHA") + echo "::${stop}::" + [[ "$url" =~ ^https://[A-Za-z0-9.-]+$ ]] || { echo "the Vercel CLI did not print a deployment URL" >&2; exit 1; } + echo "url=$url" >> "$GITHUB_OUTPUT" + echo "Preview: $url" + + - name: Report the preview on the pull request + env: + GH_TOKEN: ${{ github.token }} + REPO: ${{ github.repository }} + PR_NUMBER: ${{ github.event.pull_request.number }} + HEAD_SHA: ${{ github.event.pull_request.head.sha }} + URL: ${{ steps.deploy.outputs.url }} + run: | + set -euo pipefail + gh api --method POST "repos/${REPO}/statuses/${HEAD_SHA}" \ + -f state=success -f context="router preview" -f description="Router preview deployed" -f target_url="$URL" > /dev/null + marker='' + body=$(printf '## Router preview\n\napps/router at %s: %s/docs/\n\nThe preview is behind Vercel authentication; sign in with your Coder Vercel account.\n\n%s' "$HEAD_SHA" "$URL" "$marker") + comments=$(gh api --paginate "repos/${REPO}/issues/${PR_NUMBER}/comments" \ + --jq ".[] | select(.user.login == \"github-actions[bot]\") | select(.body | contains(\"${marker}\")) | .id") + existing_id=$(printf '%s\n' "$comments" | head -n 1) + if [ -n "$existing_id" ]; then + gh api --method PATCH "repos/${REPO}/issues/comments/${existing_id}" --raw-field body="$body" > /dev/null + else + gh api --method POST "repos/${REPO}/issues/${PR_NUMBER}/comments" --raw-field body="$body" > /dev/null + fi diff --git a/.gitignore b/.gitignore index 3110371f..f4606a78 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,6 @@ coverage/ # Release dry runs dist-out/ + +# Local `vercel build` output +.vercel/ diff --git a/AGENTS.md b/AGENTS.md index e73f5239..0b8bff4f 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -29,6 +29,7 @@ Read [README.md](README.md) and [ARCHITECTURE.md](ARCHITECTURE.md) first for wha - For local dev against the real corpus, run `pnpm dev` in `apps/docs` with `DOCS_REPO` pointing at a `coder/coder` checkout. Without it, a small fixture corpus is used. - Stop a dev server by its port (`kill $(lsof -ti tcp:)`). Never use `pkill -f 'next dev'`, because other sessions run dev servers on the same machine. - Previews are behind Vercel SSO. Vercel builds only `apps/router`: each branch that changes it gets a stable alias on the `coder-docs` project, built from the branch name with `/` replaced by `-`, which the Vercel bot comment on the PR links. A docs-site change is previewed by running `docs-pr-preview.yml` on the branch, which deploys a prebuilt preview to `coder-docs` and prints its URL. +- A pull request that changes `apps/router`, `docs-config` or `docs-manifest` also gets a router preview from `router-pr-preview.yml`: it builds with `vercel build --standalone` and no Vercel credentials, and deploys once a `docs-preview` reviewer approves, then links the URL in a comment and a `router preview` status. It replaces the Vercel Git previews when those are turned off. The build writes coder-docs's project settings itself, so keep them in step with the Vercel project. ## Code diff --git a/README.md b/README.md index adb2e779..1a9c5f05 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ The docs run on two Vercel projects on the `codercom` team. | Project | Builds | Serves | Previews | | ----------------------- | ----------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------- | -| `coder-docs` | `apps/router` from Git (Root Directory `apps/router`), with `ROUTER_BASE_PATH=/docs` and `DOCS_ANALYTICS=1` | `coder-docs.coderite.app/docs`, from its own manifest and archive stores, behind coder.com's `/docs` rewrite | Behind Vercel login: router branch previews from Git, and docs-site previews prebuilt by `docs-pr-preview.yml` | +| `coder-docs` | `apps/router` from Git (Root Directory `apps/router`), with `ROUTER_BASE_PATH=/docs` and `DOCS_ANALYTICS=1` | `coder-docs.coderite.app/docs`, from its own manifest and archive stores, behind coder.com's `/docs` rewrite | Behind Vercel login: router previews from Git and from `router-pr-preview.yml`, and docs-site previews prebuilt by `docs-pr-preview.yml` | | `coder-docs-pr-preview` | Nothing from this repository; coder/coder's own workflows deploy prebuilt output | No production site | Public coder/coder pull request previews only | The retired `/beta-docs` site's project, `coder-docs-router`, is deleted after this repository stops referring to it; see "Retiring the beta site" in `apps/docs/README.md`. From 91fce81fa3defaa2720fe2ebb1101b8af1c31657 Mon Sep 17 00:00:00 2001 From: Nick Vigilante Date: Tue, 10 Nov 2026 10:31:40 -0500 Subject: [PATCH 2/3] chore: re-run the router preview An empty commit so the preview's deploy job waits for a docs-preview reviewer again. Assisted-by: AI From b84c29accffd28e481338dde54d066082ab11677 Mon Sep 17 00:00:00 2001 From: Nick Vigilante Date: Thu, 12 Nov 2026 10:24:56 -0500 Subject: [PATCH 3/3] ci(router-preview): build on node 24 and refuse link entries before unpacking The router declares Node 24 and coder-docs runs it on Node 24, but the build job installed Node 22, so the function was traced under a different runtime than it runs on. The deploy job listed only entry names before extracting, which hides link targets. It now refuses any hard link or absolute symlink in the listing, so no entry can be written through a link to a path outside the stage; the build stores hard-linked files as copies so a real build has none. Relative symlinks with .. stay allowed: the function bundles use them, and the existing post-extraction check still rejects any that leave the output. Assisted-by: AI --- .github/workflows/router-pr-preview.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/router-pr-preview.yml b/.github/workflows/router-pr-preview.yml index 0daa6c18..3ebdd40c 100644 --- a/.github/workflows/router-pr-preview.yml +++ b/.github/workflows/router-pr-preview.yml @@ -62,7 +62,8 @@ jobs: - name: Set up Node uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 with: - node-version: 22.23.4 + # The router's engines and coder-docs's runtime are Node 24. + node-version: 24.22.3 - name: Install dependencies run: pnpm install --frozen-lockfile @@ -79,7 +80,7 @@ jobs: printf '%s\n' '{"projectId":"prj_offline","orgId":"team_offline","settings":{"framework":"nextjs","rootDirectory":"apps/router","nodeVersion":"24.x","installCommand":null,"buildCommand":null,"devCommand":null,"outputDirectory":null}}' > .vercel/project.json vercel=(pnpm --filter @coder/offlinedocs exec vercel) "${vercel[@]}" build --cwd "$GITHUB_WORKSPACE" --standalone --target=preview --yes - tar -C .vercel -czf "$RUNNER_TEMP/router-output.tgz" output + tar -C .vercel --hard-dereference -czf "$RUNNER_TEMP/router-output.tgz" output - name: Upload the build output uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 @@ -153,6 +154,9 @@ jobs: [[ "$bad" -eq 0 ]] || { echo "the archive holds $bad entries outside output/" >&2; exit 1; } bad=$(printf '%s\n' "$names" | grep -cE '(^|/)\.\.(/|$)' || true) [[ "$bad" -eq 0 ]] || { echo "the archive holds $bad entries with a .. segment" >&2; exit 1; } + # A hard link or an absolute symlink could let a later entry write outside the stage. + bad=$(tar -tvzf "$archive" | awk '$1 ~ /^h/ || ($1 ~ /^l/ && / -> \//)' | wc -l) + [[ "$bad" -eq 0 ]] || { echo "the archive holds $bad hard links or absolute symlinks" >&2; exit 1; } rm -rf "$stage" mkdir -p "$stage/.vercel" tar -xzf "$archive" -C "$stage/.vercel" --no-same-owner --no-same-permissions