|
1 | | -name: Jekyll PR Previews |
| 1 | +name: Jekyll PR Preview |
2 | 2 |
|
3 | 3 | on: |
4 | 4 | pull_request: |
| 5 | + types: [opened, reopened, synchronize, closed] |
5 | 6 |
|
6 | 7 | permissions: |
7 | 8 | contents: read |
8 | | - pages: write |
| 9 | + deployments: write |
9 | 10 | id-token: write |
10 | 11 | pull-requests: write |
| 12 | + checks: write |
| 13 | + |
| 14 | +env: |
| 15 | + BUILD_DIR: _site |
11 | 16 |
|
12 | 17 | jobs: |
| 18 | + |
| 19 | + # ---------------------------- |
| 20 | + # Build PR Preview |
| 21 | + # ---------------------------- |
13 | 22 | build: |
| 23 | + if: github.event.action != 'closed' |
14 | 24 | runs-on: ubuntu-latest |
15 | 25 | outputs: |
16 | | - pr_path: ${{ steps.setpath.outputs.pr_path }} |
| 26 | + page_url: ${{ steps.deploy.outputs.page_url }} |
17 | 27 |
|
18 | 28 | 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- |
21 | 38 |
|
22 | | - - name: Setup Ruby |
23 | | - uses: ruby/setup-ruby@8aeb6ff8030dd539317f8e1769a044873b56ea71 # v1.268.0 |
| 39 | + - uses: ruby/setup-ruby@8aeb6ff8030dd539317f8e1769a044873b56ea71 # v1.268.0 |
24 | 40 | with: |
25 | 41 | ruby-version: "3.2" |
26 | 42 | bundler-cache: true |
27 | 43 |
|
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 | 44 | # Build Jekyll |
35 | 45 | - name: Build Jekyll |
36 | 46 | 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 "/" |
40 | 50 |
|
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 |
44 | 55 | with: |
45 | | - path: _site |
| 56 | + artifact_path: ${{ env.BUILD_DIR }} |
| 57 | + environment: pr-${{ github.event.pull_request.number }} |
46 | 58 |
|
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 |
50 | 68 |
|
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 |
52 | 76 | 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 |
59 | 78 | uses: peter-evans/create-or-update-comment@e8674b075228eee787fea43ef493e45ece1004c9 # v5.0.0 |
60 | 79 | with: |
61 | 80 | issue-number: ${{ github.event.pull_request.number }} |
62 | 81 | body: | |
63 | | - 🔍 **Unique Jekyll PR Preview Ready** |
| 82 | + 🚀 **Preview Ready** |
64 | 83 |
|
65 | 84 | 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