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
53 changes: 48 additions & 5 deletions .github/workflows/docs-pr-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:`
Expand Down Expand Up @@ -65,6 +67,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. Every caller must grant statuses write, even with it empty, or the run fails at startup.
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:
Expand Down Expand Up @@ -211,13 +225,17 @@ jobs:
permissions:
contents: read
pull-requests: write
statuses: write

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: a reusable workflow's job can't request more permissions than its caller grants, so every workflow_call caller now has to grant statuses: write, even when it passes no status_sha. Otherwise the run fails at startup. No other caller exists today (coder/coder doesn't call this workflow), so nothing breaks. I'd mention it in the workflow_call input descriptions or the header comment, so whoever wires a new caller doesn't hit an unexplained startup failure.

steps:
- name: Validate inputs
shell: bash
env:
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; }
Expand All @@ -226,6 +244,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
Expand All @@ -241,13 +265,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
Expand Down Expand Up @@ -335,14 +360,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='<!-- docs-preview-deploy -->'
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" \
Expand All @@ -355,3 +383,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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the status appears only after a successful deploy. While the deploy waits for approval, which is the state these runs spend the longest in, the Checks list shows nothing for docs preview. A pending status from the preview caller (or the build job's end) with the description "awaiting docs-preview approval", replaced by success here, would tell PR authors why there's no URL yet. On failure, a failure or error state in an if: failure() step would avoid leaving a stale success from an earlier push looking current.

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."
98 changes: 98 additions & 0 deletions .github/workflows/engine-pr-preview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# 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:
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"

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
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 }}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: deploy_engine_ref covers the deploy job's checkout, but not the workflow code that runs beside the token. For a pull_request run, a local uses: ./.github/workflows/docs-pr-preview.yml is loaded from the caller's commit, which is the PR's merge commit. So the deploy job's steps are the PR's version of docs-pr-preview.yml. The path filter includes that file, so a PR that edits the deploy job (for example, to echo $VERCEL_TOKEN | base64) previews itself, and only the docs-preview approval stands between that edit and the token. The base-commit engine stops the PR's lockfile and Vercel CLI from running there, but it doesn't stop a change to the YAML itself.

This matches router-pr-preview.yml today, so it isn't a regression. Two things follow from it:

  • The approval should be treated as a code review of .github/workflows/ in that PR. A line in the header comment here and in the README paragraph would say so. A run that fires on every push to every docs-touching PR also invites click-through approvals, which wears this gate down over time.
  • The follow-up the description mentions, "dropping the required reviewer for same-repository pull requests", would put VERCEL_TOKEN within reach of anyone who can push a branch and open a PR. That is essentially the Nov 14–15 incident path. If that follow-up goes ahead, it needs a different design, for example a pull_request_target-style split where the token-holding workflow always comes from main. Removing the reviewer from this structure isn't enough.

# 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
3 changes: 2 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ 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:<port>)`). 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 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 `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
Expand Down
5 changes: 4 additions & 1 deletion apps/offlinedocs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading