diff --git a/.github/workflows/docs-pr-preview.yml b/.github/workflows/docs-pr-preview.yml index 1ce07454..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:` @@ -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: @@ -211,6 +225,7 @@ jobs: permissions: contents: read pull-requests: write + statuses: write steps: - name: Validate inputs shell: bash @@ -218,6 +233,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 +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 @@ -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 @@ -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='' - 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 +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 + 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..d1127465 --- /dev/null +++ b/.github/workflows/engine-pr-preview.yml @@ -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 }} + # 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 43b9aa36..db07e7c1 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -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:)`). 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 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.