Skip to content

Commit 402b930

Browse files
authored
Refactor Jekyll PR preview workflow
Updated Jekyll PR preview workflow to improve deployment and caching. Signed-off-by: John Mertic <[email protected]>
1 parent 1d0f987 commit 402b930

File tree

1 file changed

+68
-32
lines changed

1 file changed

+68
-32
lines changed

.github/workflows/preview.yml

Lines changed: 68 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,102 @@
1-
name: Jekyll PR Previews
1+
name: Jekyll PR Preview
22

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

67
permissions:
78
contents: read
8-
pages: write
9+
deployments: write
910
id-token: write
1011
pull-requests: write
12+
checks: write
13+
14+
env:
15+
BUILD_DIR: _site
1116

1217
jobs:
18+
19+
# ----------------------------
20+
# Build PR Preview
21+
# ----------------------------
1322
build:
23+
if: github.event.action != 'closed'
1424
runs-on: ubuntu-latest
1525
outputs:
16-
pr_path: ${{ steps.setpath.outputs.pr_path }}
26+
page_url: ${{ steps.deploy.outputs.page_url }}
1727

1828
steps:
19-
- name: Checkout
20-
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
29+
- uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
30+
31+
# Cache gems for faster builds
32+
- name: Cache Bundler
33+
uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
34+
with:
35+
path: vendor/bundle
36+
key: bundler-${{ hashFiles('**/Gemfile.lock') }}
37+
restore-keys: bundler-
2138

22-
- name: Setup Ruby
23-
uses: ruby/setup-ruby@8aeb6ff8030dd539317f8e1769a044873b56ea71 # v1.268.0
39+
- uses: ruby/setup-ruby@8aeb6ff8030dd539317f8e1769a044873b56ea71 # v1.268.0
2440
with:
2541
ruby-version: "3.2"
2642
bundler-cache: true
2743

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-
3444
# Build Jekyll
3545
- name: Build Jekyll
3646
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 }}/"
47+
bundle exec jekyll build \
48+
--destination $BUILD_DIR \
49+
--baseurl "/"
4050
41-
# Upload the artifact for deployment
42-
- name: Upload Pages Artifact
43-
uses: actions/upload-pages-artifact@7b1f4a764d45c48632c6b24a0339c27f5614fb0b # v4.0.0
51+
# Deploy preview to a unique PR environment
52+
- name: Deploy PR Preview
53+
id: deploy
54+
uses: actions/deploy-pages@d6db90164ac5ed86f2b6aed7e0febac5b3c0c03e # v4.0.5
4455
with:
45-
path: _site
56+
artifact_path: ${{ env.BUILD_DIR }}
57+
environment: pr-${{ github.event.pull_request.number }}
4658

47-
deploy:
48-
runs-on: ubuntu-latest
49-
needs: build
59+
# Add GitHub Check summary
60+
- name: Create check summary
61+
run: |
62+
echo "### 🚀 Preview Ready" >> $GITHUB_STEP_SUMMARY
63+
echo "" >> $GITHUB_STEP_SUMMARY
64+
echo "**Preview URL:**" >> $GITHUB_STEP_SUMMARY
65+
echo "${{ steps.deploy.outputs.page_url }}" >> $GITHUB_STEP_SUMMARY
66+
echo "" >> $GITHUB_STEP_SUMMARY
67+
echo "Automatically updates on new commits." >> $GITHUB_STEP_SUMMARY
5068
51-
# Deploy for both PRs and main
69+
# ----------------------------
70+
# PR comment (auto-updating single comment)
71+
# ----------------------------
72+
comment:
73+
needs: build
74+
if: github.event.action != 'closed'
75+
runs-on: ubuntu-latest
5276
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
77+
- name: Create or update PR comment
5978
uses: peter-evans/create-or-update-comment@e8674b075228eee787fea43ef493e45ece1004c9 # v5.0.0
6079
with:
6180
issue-number: ${{ github.event.pull_request.number }}
6281
body: |
63-
🔍 **Unique Jekyll PR Preview Ready**
82+
🚀 **Preview Ready**
6483
6584
Preview URL:
66-
**https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/pr-${{ github.event.pull_request.number }}/**
85+
**${{ needs.build.outputs.page_url }}**
86+
87+
This link updates automatically whenever you push commits to this PR.
88+
89+
# ----------------------------
90+
# Cleanup when PR closes
91+
# ----------------------------
92+
cleanup:
93+
if: github.event.action == 'closed'
94+
runs-on: ubuntu-latest
95+
steps:
96+
- name: Delete PR preview environment
97+
run: |
98+
gh api \
99+
repos/${{ github.repository }}/environments/pr-${{ github.event.pull_request.number }} \
100+
-X DELETE || echo "Environment already removed"
101+
env:
102+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)