Skip to content

Commit a54d512

Browse files
authored
Enhance Jekyll PR preview workflow with cleanup
Updated GitHub Actions workflow for Jekyll PR previews. Added cleanup job for closed PRs and improved deployment steps. Signed-off-by: John Mertic <[email protected]>
1 parent 1d0f987 commit a54d512

File tree

1 file changed

+36
-35
lines changed

1 file changed

+36
-35
lines changed

.github/workflows/preview.yml

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@ name: Jekyll PR Previews
22

33
on:
44
pull_request:
5+
types: [opened, synchronize, reopened, closed]
56

67
permissions:
7-
contents: read
8-
pages: write
9-
id-token: write
8+
contents: write # Required to push to preview branch
109
pull-requests: write
1110

1211
jobs:
13-
build:
12+
build-preview:
13+
if: github.event.action != 'closed'
1414
runs-on: ubuntu-latest
15-
outputs:
16-
pr_path: ${{ steps.setpath.outputs.pr_path }}
1715

1816
steps:
1917
- name: Checkout
@@ -22,45 +20,48 @@ jobs:
2220
- name: Setup Ruby
2321
uses: ruby/setup-ruby@8aeb6ff8030dd539317f8e1769a044873b56ea71 # v1.268.0
2422
with:
25-
ruby-version: "3.2"
23+
ruby-version: 3.2
2624
bundler-cache: true
2725

28-
# Determine output folder: main → root, PR → /pr-<number>/
29-
- name: Determine output path
30-
id: setpath
31-
run: |
32-
echo "pr_path=pr-${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT
33-
34-
# Build Jekyll
3526
- name: Build Jekyll
3627
run: |
37-
DEST="./_site/${{ steps.setpath.outputs.pr_path }}"
38-
mkdir -p "$DEST"
39-
bundle exec jekyll build --destination "$DEST" --trace --baseurl "/pr-${{ github.event.pull_request.number }}/"
28+
bundle exec jekyll build --destination _site
4029
41-
# Upload the artifact for deployment
42-
- name: Upload Pages Artifact
43-
uses: actions/upload-pages-artifact@7b1f4a764d45c48632c6b24a0339c27f5614fb0b # v4.0.0
30+
# Deploy ONLY the preview to a separate branch
31+
- name: Deploy Preview to pr-previews branch
32+
uses: JamesIves/github-pages-deploy-action@v4
4433
with:
45-
path: _site
46-
47-
deploy:
48-
runs-on: ubuntu-latest
49-
needs: build
34+
folder: _site
35+
branch: pr-previews # Completely isolated from gh-pages
36+
target-folder: pr-${{ github.event.number }}
37+
clean: false # Do NOT wipe other previews
5038

51-
# Deploy for both PRs and main
52-
steps:
53-
- name: Deploy to GitHub Pages
54-
id: deploy
55-
uses: actions/deploy-pages@d6db90164ac5ed86f2b6aed7e0febac5b3c0c03e # v4.0.5
56-
57-
# Comment PR preview URL
58-
- name: Comment PR Preview URL
39+
# Comment preview link onto the PR
40+
- name: Comment Preview Link
5941
uses: peter-evans/create-or-update-comment@e8674b075228eee787fea43ef493e45ece1004c9 # v5.0.0
6042
with:
6143
issue-number: ${{ github.event.pull_request.number }}
6244
body: |
63-
🔍 **Unique Jekyll PR Preview Ready**
45+
🚀 **PR Preview Ready**
46+
Your preview is available at:
6447
65-
Preview URL:
6648
**https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/pr-${{ github.event.pull_request.number }}/**
49+
50+
cleanup:
51+
if: github.event.action == 'closed'
52+
runs-on: ubuntu-latest
53+
54+
steps:
55+
- name: Checkout preview branch
56+
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
57+
with:
58+
ref: pr-previews
59+
60+
- name: Remove PR preview folder
61+
run: |
62+
rm -rf pr-${{ github.event.pull_request.number }}
63+
git config user.name "github-actions[bot]"
64+
git config user.email "github-actions[bot]@users.noreply.github.com"
65+
git add -A
66+
git commit -m "Remove preview for PR #${{ github.event.pull_request.number }}" || echo "Nothing to delete"
67+
git push

0 commit comments

Comments
 (0)