Refactor Jekyll PR preview workflow #10
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 Preview | |
| on: | |
| pull_request: | |
| types: [opened, reopened, synchronize, closed] | |
| permissions: | |
| contents: read | |
| 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: | |
| page_url: ${{ steps.deploy.outputs.page_url }} | |
| steps: | |
| - 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- | |
| - uses: ruby/setup-ruby@8aeb6ff8030dd539317f8e1769a044873b56ea71 # v1.268.0 | |
| with: | |
| ruby-version: "3.2" | |
| bundler-cache: true | |
| # Build Jekyll | |
| - name: Build Jekyll | |
| run: | | |
| bundle exec jekyll build \ | |
| --destination $BUILD_DIR \ | |
| --baseurl "/" | |
| # Deploy preview to a unique PR environment | |
| - name: Deploy PR Preview | |
| id: deploy | |
| uses: actions/deploy-pages@d6db90164ac5ed86f2b6aed7e0febac5b3c0c03e # v4.0.5 | |
| with: | |
| artifact_path: ${{ env.BUILD_DIR }} | |
| environment: pr-${{ github.event.pull_request.number }} | |
| # 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 | |
| # ---------------------------- | |
| # PR comment (auto-updating single comment) | |
| # ---------------------------- | |
| comment: | |
| needs: build | |
| if: github.event.action != 'closed' | |
| runs-on: ubuntu-latest | |
| steps: | |
| - 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: | | |
| 🚀 **Preview Ready** | |
| Preview URL: | |
| **${{ 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 }} |