Skip to content

CI: consolidate docs deployment workflows and add PR previews #23

CI: consolidate docs deployment workflows and add PR previews

CI: consolidate docs deployment workflows and add PR previews #23

Workflow file for this run

name: Publish Docs
on:
push:
branches: [master]
pull_request:
types: [opened, reopened, synchronize, closed]
workflow_dispatch:
inputs:
pr_number:
description: 'PR Number'
required: true
run_id:
description: 'Run ID of the build artifact'
required: true
permissions:
contents: write
pull-requests: write
actions: read
concurrency: ci-${{ github.ref }}
jobs:
# 1. Build Preview Artifact (Runs on PR)
build-preview:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
- name: Calculate BASE_URL
run: |
echo "BASE_URL=/pr-preview/pr-${{ github.event.pull_request.number }}/" >> $GITHUB_ENV
- name: Install and Build
run: npm ci && npm run build
env:
BASE_URL: ${{ env.BASE_URL }}
- name: Upload Preview Artifact
uses: actions/upload-artifact@v4
with:
name: preview-build
path: build/
- name: Comment Build Info
uses: marocchino/sticky-pull-request-comment@v2
with:
header: pr-build-info
message: |
### ✅ Build Successful
To deploy this preview, a maintainer must run the **Deploy Preview** workflow.
**Run ID**: `${{ github.run_id }}`
**PR Number**: `${{ github.event.pull_request.number }}`
# 2. Deploy Preview (Manual Trigger)
deploy-preview:
if: github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Download Artifact
uses: actions/download-artifact@v4
with:
name: preview-build
run-id: ${{ inputs.run_id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
path: site
- name: Deploy to GitHub Pages
uses: JamesIves/github-pages-deploy-action@v4
with:
branch: gh-pages
folder: site
target-folder: pr-preview/pr-${{ inputs.pr_number }}
- name: Comment Preview Link
uses: actions/github-script@v6
with:
script: |
const prNumber = ${{ inputs.pr_number }};
const owner = context.repo.owner;
const repo = context.repo.repo;
const url = `https://${owner}.github.io/${repo}/pr-preview/pr-${prNumber}/`;
await github.rest.issues.createComment({
owner,
repo,
issue_number: prNumber,
body: `🚀 **Preview Deployed!**\n\n[View Preview](${url})`
});
# 3. Production Build & Deploy (Runs on Master Push)
build-production:
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with: { node-version: 20 }
- run: npm ci
- run: npm run build
- uses: actions/upload-artifact@v4
with: { name: site, path: build/ }
deploy-production:
needs: build-production
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
with: { name: site, path: site }
- uses: JamesIves/github-pages-deploy-action@v4
with:
branch: gh-pages
folder: site
clean-exclude: pr-preview