From 735a11c84917eba85c502700ab0d69bb36703825 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 1 May 2026 07:16:42 +0000 Subject: [PATCH] Add PR preview environments via GitHub Actions Switch production deployment from artifact-based Pages to a gh-pages branch (peaceiris/actions-gh-pages), enabling PR previews to coexist as subdirectories alongside the live site. Add preview.yml: on every PR open/sync/reopen, builds Hugo with a PR-specific baseURL and deploys to pr-preview/pr-{number}/ on the gh-pages branch via rossjrw/pr-preview-action. The action posts a comment on the PR with the live preview URL and cleans up the subdirectory automatically when the PR is closed. https://claude.ai/code/session_01MA7Ekd1KB4MpXrC6aUeivs --- .github/workflows/deploy.yml | 27 +++++++---------------- .github/workflows/preview.yml | 40 +++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 19 deletions(-) create mode 100644 .github/workflows/preview.yml diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 7af4531..b6901c3 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -7,11 +7,9 @@ on: branches: [main] workflow_dispatch: -# Allow the workflow to write to GitHub Pages +# Write access to push the built site to the gh-pages branch permissions: - contents: read - pages: write - id-token: write + contents: write # Only allow one deployment at a time; don't cancel in-progress runs concurrency: @@ -19,7 +17,7 @@ concurrency: cancel-in-progress: false jobs: - build: + build-and-deploy: runs-on: ubuntu-latest steps: - uses: actions/checkout@v7 @@ -41,18 +39,9 @@ jobs: # hugo.toml sets minifyOutput = false because Hugo's minifier # breaks PaperMod's JSON-LD schema output. - - name: Upload Pages artifact - uses: actions/upload-pages-artifact@v5 + - name: Deploy to gh-pages branch + uses: peaceiris/actions-gh-pages@v4 with: - path: site/public - - deploy: - needs: build - runs-on: ubuntu-latest - environment: - name: github-pages - url: ${{ steps.deployment.outputs.page_url }} - steps: - - name: Deploy to GitHub Pages - id: deployment - uses: actions/deploy-pages@v5 + github_token: ${{ secrets.GITHUB_TOKEN }} + publish_dir: site/public + publish_branch: gh-pages diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml new file mode 100644 index 0000000..35223d8 --- /dev/null +++ b/.github/workflows/preview.yml @@ -0,0 +1,40 @@ +name: PR Preview + +on: + pull_request: + types: [opened, synchronize, reopened, closed] + +# Write to gh-pages branch and post comments on the PR +permissions: + contents: write + pull-requests: write + +jobs: + preview: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v7 + with: + submodules: recursive + fetch-depth: 0 + + - name: Setup Hugo + if: github.event.action != 'closed' + uses: peaceiris/actions-hugo@v3 + with: + hugo-version: '0.155.3' + extended: true + + - name: Build preview + if: github.event.action != 'closed' + # -D includes drafts so in-progress posts are visible in the preview + run: hugo --source site --baseURL "https://mathieson.github.io/blog/pr-preview/pr-${{ github.event.number }}/" -D + + - name: Deploy / remove preview + uses: rossjrw/pr-preview-action@v1 + with: + source-dir: site/public + preview-branch: gh-pages + umbrella-dir: pr-preview + action: auto + token: ${{ secrets.GITHUB_TOKEN }}