Fix preview deployment permissions by using peaceiris/actions-gh-pages #2
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: Build and Deploy Live Preview | |
| on: | |
| pull_request: | |
| branches: | |
| - master | |
| types: [opened, synchronize, reopened, closed] | |
| push: | |
| branches: | |
| - preview/* | |
| - staging | |
| permissions: | |
| contents: read | |
| pull-requests: write | |
| pages: write | |
| id-token: write | |
| jobs: | |
| build-preview: | |
| runs-on: ubuntu-latest | |
| if: github.event.action != 'closed' | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Setup Ruby | |
| uses: ruby/setup-ruby@v1 | |
| with: | |
| ruby-version: '3.1' | |
| bundler-cache: true | |
| - name: Get branch/PR info | |
| id: branch-info | |
| run: | | |
| if [ "${{ github.event_name }}" = "pull_request" ]; then | |
| echo "branch-name=pr-${{ github.event.number }}" >> $GITHUB_OUTPUT | |
| echo "is-pr=true" >> $GITHUB_OUTPUT | |
| else | |
| branch_name="${{ github.ref_name }}" | |
| safe_branch=$(echo "$branch_name" | sed 's/[^a-zA-Z0-9-]/-/g' | tr '[:upper:]' '[:lower:]') | |
| echo "branch-name=$safe_branch" >> $GITHUB_OUTPUT | |
| echo "is-pr=false" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Build with Jekyll for preview | |
| run: | | |
| # Update baseurl for preview deployment | |
| sed -i 's|baseurl: ""|baseurl: "/preview/${{ steps.branch-info.outputs.branch-name }}"|' _config.yml | |
| bundle exec jekyll build --destination ./_site | |
| env: | |
| JEKYLL_ENV: production | |
| - name: Deploy to GitHub Pages preview | |
| uses: peaceiris/actions-gh-pages@v3 | |
| with: | |
| github_token: ${{ secrets.GITHUB_TOKEN }} | |
| publish_dir: ./_site | |
| destination_dir: preview/${{ steps.branch-info.outputs.branch-name }} | |
| keep_files: true | |
| - name: Comment on PR with preview URL | |
| if: github.event_name == 'pull_request' | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const { owner, repo } = context.repo; | |
| const prNumber = context.issue.number; | |
| const previewUrl = `https://${owner}.github.io/${repo}/preview/pr-${prNumber}/`; | |
| github.rest.issues.createComment({ | |
| owner, | |
| repo, | |
| issue_number: prNumber, | |
| body: `🚀 **Live Preview Ready!** | |
| Your preview is now live at: **${previewUrl}** | |
| 📝 This preview will be updated automatically when you push new commits to this PR. | |
| 🗑️ The preview will be cleaned up when the PR is merged or closed.` | |
| cleanup-preview: | |
| runs-on: ubuntu-latest | |
| if: github.event_name == 'pull_request' && github.event.action == 'closed' | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: gh-pages | |
| - name: Remove preview directory | |
| run: | | |
| if [ -d "preview/pr-${{ github.event.number }}" ]; then | |
| rm -rf preview/pr-${{ github.event.number }} | |
| echo "Removed preview directory for PR #${{ github.event.number }}" | |
| else | |
| echo "Preview directory for PR #${{ github.event.number }} not found" | |
| fi | |
| - name: Deploy cleanup | |
| uses: peaceiris/actions-gh-pages@v3 | |
| with: | |
| github_token: ${{ secrets.GITHUB_TOKEN }} | |
| publish_dir: . | |
| keep_files: true |