Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
218 changes: 218 additions & 0 deletions .github/workflows/router-pr-preview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
# 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:
# 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

- 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 --hard-dereference -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; }
# 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
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='<!-- router-preview-deploy -->'
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ coverage/

# Release dry runs
dist-out/

# Local `vercel build` output
.vercel/
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:<port>)`). 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

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
Loading