Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 38 additions & 35 deletions .github/workflows/preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ name: Jekyll PR Previews

on:
pull_request:
types: [opened, synchronize, reopened, closed]

permissions:
contents: read
pages: write
id-token: write
contents: write # Required to push to preview branch
pull-requests: write

jobs:
build:
build-preview:
if: github.event.action != 'closed'
runs-on: ubuntu-latest
outputs:
pr_path: ${{ steps.setpath.outputs.pr_path }}

steps:
- name: Checkout
Expand All @@ -22,45 +20,50 @@ jobs:
- name: Setup Ruby
uses: ruby/setup-ruby@8aeb6ff8030dd539317f8e1769a044873b56ea71 # v1.268.0
with:
ruby-version: "3.2"
ruby-version: 3.2
bundler-cache: true
- name: Remove CNAME if present
run: rm -f CNAME

# Determine output folder: main → root, PR → /pr-<number>/
- name: Determine output path
id: setpath
run: |
echo "pr_path=pr-${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT

# Build Jekyll
- name: Build Jekyll
run: |
DEST="./_site/${{ steps.setpath.outputs.pr_path }}"
mkdir -p "$DEST"
bundle exec jekyll build --destination "$DEST" --trace --baseurl "/pr-${{ github.event.pull_request.number }}/"
bundle exec jekyll build --destination _site --baseurl "/pr-${{ github.event.number }}"

# Upload the artifact for deployment
- name: Upload Pages Artifact
uses: actions/upload-pages-artifact@7b1f4a764d45c48632c6b24a0339c27f5614fb0b # v4.0.0
# Deploy ONLY the preview to a separate branch
- name: Deploy Preview to pr-previews branch
uses: JamesIves/github-pages-deploy-action@v4
with:
path: _site

deploy:
runs-on: ubuntu-latest
needs: build
folder: _site
branch: main
target-folder: pr-${{ github.event.number }}
clean: false # Do NOT wipe other previews

# Deploy for both PRs and main
steps:
- name: Deploy to GitHub Pages
id: deploy
uses: actions/deploy-pages@d6db90164ac5ed86f2b6aed7e0febac5b3c0c03e # v4.0.5

# Comment PR preview URL
- name: Comment PR Preview URL
# Comment preview link onto the PR
- name: Comment Preview Link
uses: peter-evans/create-or-update-comment@e8674b075228eee787fea43ef493e45ece1004c9 # v5.0.0
with:
issue-number: ${{ github.event.pull_request.number }}
body: |
🔍 **Unique Jekyll PR Preview Ready**
🚀 **PR Preview Ready**
Your preview is available at:

Preview URL:
**https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/pr-${{ github.event.pull_request.number }}/**

cleanup:
if: github.event.action == 'closed'
runs-on: ubuntu-latest

steps:
- name: Checkout preview branch
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
with:
ref: pr-previews

- name: Remove PR preview folder
run: |
rm -rf pr-${{ github.event.pull_request.number }}
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add -A
git commit -m "Remove preview for PR #${{ github.event.pull_request.number }}" || echo "Nothing to delete"
git push
Loading