Skip to content

Commit af8a6dc

Browse files
EXP-2127: Add per-PR preview deployments via GitHub Pages
Add a deploy-preview job to the PR workflow that publishes the built site to the gh-pages branch under pr-preview/pr-<number>/ using rossjrw/pr-preview-action, posts a sticky comment with the preview URL, and tears the preview down when the PR closes. BASE_URL in docusaurus.config.js becomes env-driven so preview builds can target the Pages subpath; production builds are unaffected (env var unset resolves to the current baseUrl of "/"). Fork PRs are skipped: they get neither secrets nor a writable GITHUB_TOKEN, and already cannot build due to ADO npm feed auth. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent e6f9e23 commit af8a6dc

3 files changed

Lines changed: 65 additions & 1 deletion

File tree

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ ZENDESK_KEY=<zendesk key>
22
GTM_ID=<google tag manager id>
33

44
BRANCH=optional (sets the edit path)
5+
BASE_URL=optional (subpath the site is served from, no trailing slash; used by PR previews)

.github/workflows/pr.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ name: PR build
22

33
on:
44
pull_request:
5+
# `closed` is included so the preview deployment is torn down when the PR
6+
# closes; the build/link-check job skips that event
7+
types: [opened, reopened, synchronize, closed]
58
workflow_dispatch: # Allows manual triggering from the GitHub UI
69

710
permissions:
@@ -11,6 +14,7 @@ permissions:
1114

1215
jobs:
1316
build:
17+
if: github.event.action != 'closed'
1418
runs-on: ubuntu-latest
1519
timeout-minutes: 30
1620
steps:
@@ -116,3 +120,60 @@ jobs:
116120
if (filtered.length > 0) {
117121
core.setFailed("There are broken links in the documentation.");
118122
}
123+
124+
# Deploys the built site to GitHub Pages under pr-preview/pr-<number>/ and
125+
# posts a sticky comment on the PR with the preview URL; the preview is
126+
# removed when the PR closes. Builds separately from the job above because
127+
# the preview needs a different baseUrl than the link check. Fork PRs are
128+
# skipped: they get neither secrets nor a writable GITHUB_TOKEN.
129+
deploy-preview:
130+
if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository
131+
runs-on: ubuntu-latest
132+
timeout-minutes: 30
133+
# Concurrent runs for the same PR would race on pushing to gh-pages
134+
concurrency: pr-preview-${{ github.ref }}
135+
permissions:
136+
contents: write
137+
pull-requests: write
138+
steps:
139+
- uses: actions/checkout@v7
140+
with:
141+
# Images are Git LFS-tracked; skip fetching them on teardown, where
142+
# the checkout is only needed so the action can push to gh-pages
143+
lfs: ${{ github.event.action != 'closed' }}
144+
145+
- uses: actions/setup-node@v6
146+
if: github.event.action != 'closed'
147+
with:
148+
node-version: 24
149+
cache: npm
150+
151+
# The ADO registry requires auth even to install; the repo .npmrc is
152+
# credential-less so creds go into ~/.npmrc here
153+
- name: Authenticate to codat-npm feed
154+
if: github.event.action != 'closed'
155+
run: |
156+
{
157+
echo "//pkgs.dev.azure.com/codat/Codat/_packaging/codat-npm/npm/registry/:username=codat"
158+
echo "//pkgs.dev.azure.com/codat/Codat/_packaging/codat-npm/npm/registry/:_password=${ADO_NPM_FEED_TOKEN}"
159+
echo "//pkgs.dev.azure.com/codat/Codat/_packaging/codat-npm/npm/registry/:email=npm-requires-email@example.com"
160+
} >> ~/.npmrc
161+
env:
162+
ADO_NPM_FEED_TOKEN: ${{ secrets.ADO_NPM_FEED_TOKEN }}
163+
164+
- name: Install dependencies
165+
if: github.event.action != 'closed'
166+
run: npm ci
167+
168+
- name: Build site for preview
169+
if: github.event.action != 'closed'
170+
run: npm run build
171+
env:
172+
GTM_ID: ${{ vars.GTM_ID }}
173+
# GitHub Pages serves the preview from a subpath (no trailing slash)
174+
BASE_URL: /codat-docs/pr-preview/pr-${{ github.event.number }}
175+
176+
- name: Deploy preview
177+
uses: rossjrw/pr-preview-action@v1
178+
with:
179+
source-dir: ./build

docusaurus.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ import redirects from "./redirects.config";
8181

8282
import { generateAPISitemaps } from "./src/utils/oas-sitemap.js";
8383

84-
const BASE_URL = "";
84+
// PR preview deploys serve the site from a subpath on GitHub Pages, so the
85+
// preview workflow overrides this. No trailing slash — baseUrl appends one.
86+
const BASE_URL = process.env.BASE_URL ?? "";
8587

8688
require("dotenv").config();
8789

0 commit comments

Comments
 (0)