Skip to content

Deploy site to GitHub Pages #8

Deploy site to GitHub Pages

Deploy site to GitHub Pages #8

Workflow file for this run

name: Deploy site to GitHub Pages
on:
push:
branches: [main]
paths: ["site/**", ".github/workflows/deploy-pages.yml"]
# Re-deploy (and refresh the cached release snapshot) when a release is
# published, and once a day to catch edits/deletions.
release:
types: [published]
schedule:
- cron: "0 6 * * *"
workflow_dispatch: {}
permissions:
contents: read
pages: write
id-token: write
concurrency:
group: pages
cancel-in-progress: true
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
cache-dependency-path: site/package-lock.json
- run: npm ci
working-directory: site
# Fetch the latest release SERVER-SIDE with the workflow token and bake
# it into the site as a static file. Visitors then read this same-origin
# snapshot instead of hitting GitHub's anonymous API (60 req/hour/IP),
# which was returning 403s and showing the error fallback. Authenticated
# requests have a far higher limit, so this never rate-limits.
# If there's no published release (or the fetch fails) we leave the file
# absent and the site falls back to the live API at runtime.
- name: Cache latest release as a static file
env:
GH_TOKEN: ${{ github.token }}
run: |
if curl -sf \
-H "Authorization: Bearer $GH_TOKEN" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/${{ github.repository }}/releases/latest" \
-o site/public/latest-release.json; then
echo "Cached release: $(node -p "require('./site/public/latest-release.json').tag_name")"
else
echo "No published release or fetch failed; site will use the live API fallback."
rm -f site/public/latest-release.json
fi
- run: npm run build
working-directory: site
# GitHub Pages has no server-side routing: a direct visit to
# /diskern/docs would 404 before React Router ever loads. Serving
# the same SPA shell as the 404 page lets the client-side router
# take over once the JS boots, so deep links and refreshes on
# future pages (e.g. /docs, /changelog) work.
- name: Add SPA fallback for client-side routing
run: cp dist/index.html dist/404.html
working-directory: site
- uses: actions/upload-pages-artifact@v3
with:
path: site/dist
deploy:
needs: build
runs-on: ubuntu-latest
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- id: deployment
uses: actions/deploy-pages@v4