Enhance Jekyll PR preview workflow with cleanup #21
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Jekyll PR Previews | |
| on: | |
| pull_request: | |
| types: [opened, synchronize, reopened, closed] | |
| permissions: | |
| contents: write # Required to push to preview branch | |
| pull-requests: write | |
| jobs: | |
| build-preview: | |
| if: github.event.action != 'closed' | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0 | |
| - name: Setup Ruby | |
| uses: ruby/setup-ruby@8aeb6ff8030dd539317f8e1769a044873b56ea71 # v1.268.0 | |
| with: | |
| ruby-version: 3.2 | |
| bundler-cache: true | |
| - name: Remove CNAME if present | |
| run: rm -f CNAME | |
| - name: Build Jekyll | |
| run: | | |
| bundle exec jekyll build --destination _site --baseurl "/pr-${{ github.event.number }}" | |
| # Deploy ONLY the preview to a separate branch | |
| - name: Deploy Preview to pr-previews branch | |
| uses: JamesIves/github-pages-deploy-action@v4 | |
| with: | |
| folder: _site | |
| branch: main | |
| target-folder: pr-${{ github.event.number }} | |
| clean: false # Do NOT wipe other previews | |
| # 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: | | |
| 🚀 **PR Preview Ready** | |
| Your preview is available at: | |
| **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 |