Repository navigation
ci: preview the docs site on every engine pull request push #311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e758911
fcc4983
35d1b97
25415da
0d5bb8c
2d15ba7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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,13 +225,17 @@ jobs: | |
| permissions: | ||
| contents: read | ||
| pull-requests: write | ||
| statuses: write | ||
| 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; } | ||
|
|
@@ -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='<!-- 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" \ | ||
|
|
@@ -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 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| 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." | ||
| 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 }} | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: This matches
|
||
| # 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 | ||
There was a problem hiding this comment.
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_callcaller now has to grantstatuses: write, even when it passes nostatus_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 theworkflow_callinput descriptions or the header comment, so whoever wires a new caller doesn't hit an unexplained startup failure.