From e75891138b681fc15effb97f238cfc288fcad764 Mon Sep 17 00:00:00 2001 From: Nick Vigilante Date: Wed, 4 Nov 2026 14:45:26 -0500 Subject: [PATCH 1/3] ci: preview the docs site on every engine pull request push A docs-site change could only be previewed by dispatching docs-pr-preview.yml by hand, and Vercel's own Git builds only cover apps/router, so a change to a shared package such as docs-theme never got a preview at all. engine-pr-preview.yml now runs on each push to a same-repository pull request that touches the docs site or a package, resolves coder/coder main, and calls docs-pr-preview.yml with it. The reusable workflow gains three optional inputs: comment_repository, so the URL comment can go on the engine pull request instead of the content repository's; status_sha, which links the deployment from a "docs preview" commit status; and deploy_engine_ref, which the caller sets to the pull request's base commit so the deploy job never installs the pull request's own lockfile or Vercel CLI next to the token. The deploy still waits for a docs-preview reviewer. Fork pull requests are skipped. Assisted-by: AI --- .github/workflows/docs-pr-preview.yml | 51 +++++++++++++++++++--- .github/workflows/engine-pr-preview.yml | 56 +++++++++++++++++++++++++ AGENTS.md | 2 +- apps/offlinedocs/README.md | 5 ++- 4 files changed, 107 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/engine-pr-preview.yml diff --git a/.github/workflows/docs-pr-preview.yml b/.github/workflows/docs-pr-preview.yml index 1ce07454..10b63703 100644 --- a/.github/workflows/docs-pr-preview.yml +++ b/.github/workflows/docs-pr-preview.yml @@ -65,6 +65,18 @@ on: description: runs-on label; needs 16 GB of memory type: string default: ubuntu-latest + comment_repository: + description: The repository whose pull request pr_number names, as owner/name; empty means content_repository + type: string + default: "" + status_sha: + description: A commit of this repository to mark with a "docs preview" status linking the deployment; empty skips it + type: string + default: "" + deploy_engine_ref: + description: The docs-engine commit the deploy job checks out; empty means the workflow's own commit + type: string + default: "" workflow_dispatch: inputs: content_repository: @@ -211,6 +223,7 @@ jobs: permissions: contents: read pull-requests: write + statuses: write steps: - name: Validate inputs shell: bash @@ -218,6 +231,9 @@ jobs: CONTENT_REPOSITORY: ${{ inputs.content_repository }} CONTENT_REF: ${{ inputs.content_ref }} PR_NUMBER: ${{ inputs.pr_number }} + COMMENT_REPOSITORY: ${{ inputs.comment_repository }} + STATUS_SHA: ${{ inputs.status_sha }} + DEPLOY_ENGINE_REF: ${{ inputs.deploy_engine_ref }} run: | set -euo pipefail [[ "$CONTENT_REPOSITORY" =~ ^[A-Za-z0-9](-?[A-Za-z0-9])*/[A-Za-z0-9._-]+$ ]] || { echo "content_repository must be owner/name" >&2; exit 1; } @@ -226,6 +242,12 @@ jobs: esac [[ "$CONTENT_REF" =~ ^[0-9a-f]{40}$ ]] || { echo "content_ref must be a full 40-hex commit SHA" >&2; exit 1; } [[ -z "$PR_NUMBER" || "$PR_NUMBER" =~ ^[0-9]+$ ]] || { echo "pr_number must be empty or numeric" >&2; exit 1; } + [[ -z "$COMMENT_REPOSITORY" || "$COMMENT_REPOSITORY" =~ ^[A-Za-z0-9](-?[A-Za-z0-9])*/[A-Za-z0-9._-]+$ ]] || { echo "comment_repository must be empty or owner/name" >&2; exit 1; } + case "${COMMENT_REPOSITORY#*/}" in + .|..) echo "comment_repository name must not be . or .." >&2; exit 1;; + esac + [[ -z "$STATUS_SHA" || "$STATUS_SHA" =~ ^[0-9a-f]{40}$ ]] || { echo "status_sha must be empty or a full 40-hex commit SHA" >&2; exit 1; } + [[ -z "$DEPLOY_ENGINE_REF" || "$DEPLOY_ENGINE_REF" =~ ^[0-9a-f]{40}$ ]] || { echo "deploy_engine_ref must be empty or a full 40-hex commit SHA" >&2; exit 1; } - name: Check the deploy target # An environment GitHub created on first use has none of these, so @@ -241,13 +263,14 @@ jobs: [[ "${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 pinned workflow commit + - name: Check out docs-engine for the deploy # The engine only. The pull request's content is never on this - # runner's disk, only the export built from it. + # runner's disk, only the export built from it. An engine pull request + # passes its base commit, so its own lockfile never runs beside the token. uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 with: repository: coder/docs-engine - ref: ${{ github.workflow_sha }} + ref: ${{ inputs.deploy_engine_ref || github.workflow_sha }} path: engine token: ${{ secrets.DOCS_ENGINE_READ_TOKEN || github.token }} persist-credentials: false @@ -335,14 +358,17 @@ jobs: if: inputs.pr_number != '' env: GH_TOKEN: ${{ github.token }} - REPO: ${{ inputs.content_repository }} + CONTENT_REPOSITORY: ${{ inputs.content_repository }} + COMMENT_REPOSITORY: ${{ inputs.comment_repository }} PR_NUMBER: ${{ inputs.pr_number }} SHA: ${{ inputs.content_ref }} + ENGINE_SHA: ${{ github.workflow_sha }} URL: ${{ steps.deploy.outputs.url }} run: | set -euo pipefail + REPO="${COMMENT_REPOSITORY:-$CONTENT_REPOSITORY}" marker='' - body=$(printf '## Docs preview deployment\n\nRendered with docs-engine for %s: %s\n\nThe preview is behind Vercel authentication; sign in with your Coder Vercel account.\n\n%s' "$SHA" "$URL" "$marker") + body=$(printf '## Docs preview deployment\n\nRendered with docs-engine %s from %s at %s: %s\n\nThe preview is behind Vercel authentication; sign in with your Coder Vercel account.\n\n%s' "$ENGINE_SHA" "$CONTENT_REPOSITORY" "$SHA" "$URL" "$marker") # List first and fail loudly: a real API error must abort here rather # than fall through to a POST that duplicates the comment. comments=$(gh api --paginate "repos/${REPO}/issues/${PR_NUMBER}/comments" \ @@ -355,3 +381,18 @@ jobs: gh api --method POST "repos/${REPO}/issues/${PR_NUMBER}/comments" --raw-field body="$body" echo "Created the preview comment." fi + + - name: Mark the commit with the preview + if: inputs.status_sha != '' + env: + GH_TOKEN: ${{ github.token }} + REPO: ${{ github.repository }} + STATUS_SHA: ${{ inputs.status_sha }} + URL: ${{ steps.deploy.outputs.url }} + run: | + set -euo pipefail + gh api --method POST "repos/${REPO}/statuses/${STATUS_SHA}" \ + -f state=success -f context="docs preview" \ + -f description="Prebuilt preview on coder-docs (Vercel login)" \ + -f target_url="$URL" > /dev/null + echo "Marked ${STATUS_SHA} with the preview." diff --git a/.github/workflows/engine-pr-preview.yml b/.github/workflows/engine-pr-preview.yml new file mode 100644 index 00000000..7d6ef4e7 --- /dev/null +++ b/.github/workflows/engine-pr-preview.yml @@ -0,0 +1,56 @@ +# Previews a docs-engine pull request's docs site against coder/coder main on +# every push, through docs-pr-preview.yml. Forks are skipped: their runs get +# no secrets, and the deploy would only wait on an approval nobody should give. +name: engine-pr-preview + +on: + pull_request: + paths: + - apps/docs/** + - apps/offlinedocs/** + - packages/** + - package.json + - pnpm-lock.yaml + - pnpm-workspace.yaml + - turbo.json + - .github/workflows/docs-pr-preview.yml + - .github/workflows/engine-pr-preview.yml + +permissions: + contents: read + +concurrency: + group: engine-pr-preview-${{ github.event.pull_request.number }} + cancel-in-progress: true + +jobs: + resolve: + name: resolve coder/coder main + if: github.event.pull_request.head.repo.full_name == github.repository + runs-on: ubuntu-latest + timeout-minutes: 5 + outputs: + content_ref: ${{ steps.main.outputs.sha }} + steps: + - id: main + run: | + set -euo pipefail + sha=$(git ls-remote https://github.com/coder/coder refs/heads/main | cut -f1) + [[ "$sha" =~ ^[0-9a-f]{40}$ ]] || { echo "could not resolve coder/coder main" >&2; exit 1; } + echo "sha=$sha" >> "$GITHUB_OUTPUT" + echo "coder/coder main is $sha" + + preview: + needs: resolve + uses: ./.github/workflows/docs-pr-preview.yml + permissions: + contents: read + pull-requests: write + statuses: write + with: + content_repository: coder/coder + content_ref: ${{ needs.resolve.outputs.content_ref }} + pr_number: ${{ format('{0}', github.event.pull_request.number) }} + comment_repository: ${{ github.repository }} + status_sha: ${{ github.event.pull_request.head.sha }} + deploy_engine_ref: ${{ github.event.pull_request.base.sha }} diff --git a/AGENTS.md b/AGENTS.md index bb186e76..c9909ade 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -28,7 +28,7 @@ Read [README.md](README.md) and [ARCHITECTURE.md](ARCHITECTURE.md) first for wha - Stylelint checks the CSS in `packages/docs-theme` as part of that package's `lint` script, with `stylelint-config-recommended` only; formatting stays with Oxfmt, so add no Stylelint style rules. - 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` and `coder-docs-router` projects, 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. +- Previews are behind Vercel SSO. Vercel builds only `apps/router`: each branch that changes it gets a stable alias on the `coder-docs` and `coder-docs-router` projects, built from the branch name with `/` replaced by `-`, which the Vercel bot comment on the PR links. A docs-site change is previewed by `engine-pr-preview.yml`, which runs on every push to a pull request from this repository that touches `apps/docs`, `apps/offlinedocs` or `packages/`, builds it against coder/coder `main`, and, once a `docs-preview` reviewer approves the deploy, comments the URL on the PR and links it from a `docs preview` status. ## Code diff --git a/apps/offlinedocs/README.md b/apps/offlinedocs/README.md index 6c288bd8..f8b6d35d 100644 --- a/apps/offlinedocs/README.md +++ b/apps/offlinedocs/README.md @@ -80,7 +80,10 @@ The static export contains no `eval`, and `csp:check` (above) already verifies t The CLI still checks that the project's Root Directory exists under the directory it deploys from, so a deploy must run from a directory that has it. The workflow `.github/workflows/docs-pr-preview.yml` builds one commit of a content repository's `docs/` this way and deploys it as a preview deployment of the `coder-docs` project, behind Vercel login. -Run it by hand from the Actions tab, on the docs-engine branch to preview, with a coder/coder commit SHA. +`.github/workflows/engine-pr-preview.yml` calls it on every push to a docs-engine pull request that touches the docs site or a package, with coder/coder `main` as the content, and has it comment the URL on that pull request and set a `docs preview` status on its head commit. +Pull requests from forks are skipped. +For those runs the deploy job checks out the pull request's base commit, so the lockfile and Vercel CLI that run beside the token are `main`'s, not the pull request's. +It can also be run by hand from the Actions tab, on the docs-engine branch to preview, with a coder/coder commit SHA. coder/coder's own pull request previews no longer use it: they build in coder/coder and deploy to the `coder-docs-pr-preview` project. The workflow has two jobs. `build` holds no Vercel secret. From fcc498364fc61f1a713fc75f8cb10babe99b8c7e Mon Sep 17 00:00:00 2001 From: Nick Vigilante Date: Wed, 4 Nov 2026 14:53:11 -0500 Subject: [PATCH 2/3] ci: pass secrets to the preview so the deploy can read its token A called workflow sees only the secrets its caller passes, environment secrets included, so the deploy job found docs-preview's VERCEL_TOKEN empty. secrets: inherit fixes that; the token stays environment-scoped, and the build job still refuses to run if a Vercel secret is visible to it. Assisted-by: AI --- .github/workflows/engine-pr-preview.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/engine-pr-preview.yml b/.github/workflows/engine-pr-preview.yml index 7d6ef4e7..60159fef 100644 --- a/.github/workflows/engine-pr-preview.yml +++ b/.github/workflows/engine-pr-preview.yml @@ -54,3 +54,5 @@ jobs: comment_repository: ${{ github.repository }} status_sha: ${{ github.event.pull_request.head.sha }} deploy_engine_ref: ${{ github.event.pull_request.base.sha }} + # Without it the deploy job can't read docs-preview's VERCEL_TOKEN; the build job refuses to run if one is visible. + secrets: inherit From 2d15ba79ec6f1e8daffeecdb096b68229a48f59e Mon Sep 17 00:00:00 2001 From: Nick Vigilante Date: Mon, 16 Nov 2026 17:25:56 -0500 Subject: [PATCH 3/3] ci: show a pending or failed docs preview status and document the approval gate The docs preview status only appeared after a successful deploy, so a PR showed nothing while the deploy waited for approval, and a success from an earlier push stayed current when a later run failed. The caller now marks the head commit pending before the build and failure if the build or deploy fails; both jobs use only the job token. For a pull_request run the deploy job's steps come from the PR's own copy of docs-pr-preview.yml, so the docs-preview approval is the only check between a workflow edit and VERCEL_TOKEN. The header comments and AGENTS.md now say that approving a preview means reviewing the PR's workflow changes. The status_sha input also notes that every caller must grant statuses write. Assisted-by: AI --- .github/workflows/docs-pr-preview.yml | 4 ++- .github/workflows/engine-pr-preview.yml | 42 ++++++++++++++++++++++++- AGENTS.md | 1 + 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/.github/workflows/docs-pr-preview.yml b/.github/workflows/docs-pr-preview.yml index 10b63703..e62d1adb 100644 --- a/.github/workflows/docs-pr-preview.yml +++ b/.github/workflows/docs-pr-preview.yml @@ -25,6 +25,8 @@ # script, and uploads the result with the Vercel CLI that # pnpm-lock.yaml pins. # +# A pull_request caller runs the PR's own copy of this file, so the docs-preview approval is a review of the PR's .github/workflows/ changes. +# # The `docs-preview` environment holds the secret VERCEL_TOKEN and the # variables VERCEL_ORG_ID and VERCEL_PROJECT_ID (coder-docs). DOCS_ENGINE_READ_TOKEN, if # used, must be an ordinary repository secret: `build` has no `environment:` @@ -70,7 +72,7 @@ on: type: string default: "" status_sha: - description: A commit of this repository to mark with a "docs preview" status linking the deployment; empty skips it + description: A commit of this repository to mark with a "docs preview" status linking the deployment; empty skips it. Every caller must grant statuses write, even with it empty, or the run fails at startup. type: string default: "" deploy_engine_ref: diff --git a/.github/workflows/engine-pr-preview.yml b/.github/workflows/engine-pr-preview.yml index 60159fef..d1127465 100644 --- a/.github/workflows/engine-pr-preview.yml +++ b/.github/workflows/engine-pr-preview.yml @@ -1,6 +1,7 @@ # Previews a docs-engine pull request's docs site against coder/coder main on # every push, through docs-pr-preview.yml. Forks are skipped: their runs get # no secrets, and the deploy would only wait on an approval nobody should give. +# The deploy runs this PR's own docs-pr-preview.yml, so a docs-preview approval is a review of its .github/workflows/ changes. name: engine-pr-preview on: @@ -40,8 +41,26 @@ jobs: echo "sha=$sha" >> "$GITHUB_OUTPUT" echo "coder/coder main is $sha" - preview: + pending: + name: mark the preview pending needs: resolve + runs-on: ubuntu-latest + timeout-minutes: 5 + permissions: + statuses: write + steps: + - env: + GH_TOKEN: ${{ github.token }} + REPO: ${{ github.repository }} + STATUS_SHA: ${{ github.event.pull_request.head.sha }} + run: | + set -euo pipefail + gh api --method POST "repos/${REPO}/statuses/${STATUS_SHA}" \ + -f state=pending -f context="docs preview" \ + -f description="Building; the deploy awaits docs-preview approval" > /dev/null + + preview: + needs: [resolve, pending] uses: ./.github/workflows/docs-pr-preview.yml permissions: contents: read @@ -56,3 +75,24 @@ jobs: deploy_engine_ref: ${{ github.event.pull_request.base.sha }} # Without it the deploy job can't read docs-preview's VERCEL_TOKEN; the build job refuses to run if one is visible. secrets: inherit + + failed: + name: mark the preview failed + needs: [pending, preview] + if: failure() && needs.pending.result == 'success' + runs-on: ubuntu-latest + timeout-minutes: 5 + permissions: + statuses: write + steps: + - env: + GH_TOKEN: ${{ github.token }} + REPO: ${{ github.repository }} + STATUS_SHA: ${{ github.event.pull_request.head.sha }} + RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} + run: | + set -euo pipefail + gh api --method POST "repos/${REPO}/statuses/${STATUS_SHA}" \ + -f state=failure -f context="docs preview" \ + -f description="The preview was not deployed; see the run" \ + -f target_url="$RUN_URL" > /dev/null diff --git a/AGENTS.md b/AGENTS.md index 44a18b2f..db07e7c1 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -30,6 +30,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 `apps/router` from Git on `main` only, for production; `git.deploymentEnabled` in `apps/router/vercel.json` turns its Git builds off for every other branch, so a pull request gets no Vercel bot preview. A docs-site change is previewed by `engine-pr-preview.yml`, which runs on every push to a pull request from this repository that touches `apps/docs`, `apps/offlinedocs` or `packages/`, builds it against coder/coder `main`, and, once a `docs-preview` reviewer approves the deploy, comments the URL on the PR and links it from a `docs preview` status. + The deploy job runs the PR's own copy of `docs-pr-preview.yml` beside `VERCEL_TOKEN`, so approving a preview means reviewing any `.github/workflows/` change in that PR first. - 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, which are off for every branch but `main`. The build writes coder-docs's project settings itself, so keep them in step with the Vercel project. ## Code