From 402b93034307327cef30121918095d0518bea95b Mon Sep 17 00:00:00 2001 From: John Mertic Date: Mon, 1 Dec 2025 09:57:29 -0500 Subject: [PATCH] Refactor Jekyll PR preview workflow Updated Jekyll PR preview workflow to improve deployment and caching. Signed-off-by: John Mertic --- .github/workflows/preview.yml | 100 +++++++++++++++++++++++----------- 1 file changed, 68 insertions(+), 32 deletions(-) diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml index 8f2f686fb..60c9e7c44 100644 --- a/.github/workflows/preview.yml +++ b/.github/workflows/preview.yml @@ -1,66 +1,102 @@ -name: Jekyll PR Previews +name: Jekyll PR Preview on: pull_request: + types: [opened, reopened, synchronize, closed] permissions: contents: read - pages: write + deployments: write id-token: write pull-requests: write + checks: write + +env: + BUILD_DIR: _site jobs: + + # ---------------------------- + # Build PR Preview + # ---------------------------- build: + if: github.event.action != 'closed' runs-on: ubuntu-latest outputs: - pr_path: ${{ steps.setpath.outputs.pr_path }} + page_url: ${{ steps.deploy.outputs.page_url }} steps: - - name: Checkout - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0 + - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0 + + # Cache gems for faster builds + - name: Cache Bundler + uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0 + with: + path: vendor/bundle + key: bundler-${{ hashFiles('**/Gemfile.lock') }} + restore-keys: bundler- - - name: Setup Ruby - uses: ruby/setup-ruby@8aeb6ff8030dd539317f8e1769a044873b56ea71 # v1.268.0 + - uses: ruby/setup-ruby@8aeb6ff8030dd539317f8e1769a044873b56ea71 # v1.268.0 with: ruby-version: "3.2" bundler-cache: true - # Determine output folder: main → root, PR → /pr-/ - - 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 $BUILD_DIR \ + --baseurl "/" - # Upload the artifact for deployment - - name: Upload Pages Artifact - uses: actions/upload-pages-artifact@7b1f4a764d45c48632c6b24a0339c27f5614fb0b # v4.0.0 + # Deploy preview to a unique PR environment + - name: Deploy PR Preview + id: deploy + uses: actions/deploy-pages@d6db90164ac5ed86f2b6aed7e0febac5b3c0c03e # v4.0.5 with: - path: _site + artifact_path: ${{ env.BUILD_DIR }} + environment: pr-${{ github.event.pull_request.number }} - deploy: - runs-on: ubuntu-latest - needs: build + # Add GitHub Check summary + - name: Create check summary + run: | + echo "### 🚀 Preview Ready" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "**Preview URL:**" >> $GITHUB_STEP_SUMMARY + echo "${{ steps.deploy.outputs.page_url }}" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "Automatically updates on new commits." >> $GITHUB_STEP_SUMMARY - # Deploy for both PRs and main + # ---------------------------- + # PR comment (auto-updating single comment) + # ---------------------------- + comment: + needs: build + if: github.event.action != 'closed' + runs-on: ubuntu-latest 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 + - name: Create or update PR comment 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** + 🚀 **Preview Ready** Preview URL: - **https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/pr-${{ github.event.pull_request.number }}/** + **${{ needs.build.outputs.page_url }}** + + This link updates automatically whenever you push commits to this PR. + + # ---------------------------- + # Cleanup when PR closes + # ---------------------------- + cleanup: + if: github.event.action == 'closed' + runs-on: ubuntu-latest + steps: + - name: Delete PR preview environment + run: | + gh api \ + repos/${{ github.repository }}/environments/pr-${{ github.event.pull_request.number }} \ + -X DELETE || echo "Environment already removed" + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}