|
| 1 | +name: Deploy Jekyll site to GitHub Pages |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + pull_request: |
| 8 | + types: [opened, synchronize, reopened] |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: read |
| 12 | + pages: write |
| 13 | + id-token: write |
| 14 | + pull-requests: write |
| 15 | + |
| 16 | +concurrency: |
| 17 | + group: "pages-${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.pull_request.number) || 'main' }}" |
| 18 | + cancel-in-progress: true |
| 19 | + |
| 20 | +jobs: |
| 21 | + build: |
| 22 | + runs-on: ubuntu-latest |
| 23 | + steps: |
| 24 | + - name: Checkout |
| 25 | + uses: actions/checkout@v4 |
| 26 | + |
| 27 | + - name: Setup Ruby |
| 28 | + uses: ruby/setup-ruby@v1 |
| 29 | + with: |
| 30 | + ruby-version: '3.2' |
| 31 | + bundler-cache: true |
| 32 | + |
| 33 | + - name: Setup Pages |
| 34 | + id: pages |
| 35 | + uses: actions/configure-pages@v4 |
| 36 | + |
| 37 | + - name: Build with Jekyll |
| 38 | + run: | |
| 39 | + if [ "${{ github.event_name }}" == "pull_request" ]; then |
| 40 | + # For PRs, build with baseurl pointing to PR-specific folder |
| 41 | + bundle exec jekyll build --baseurl "/pr-${{ github.event.pull_request.number }}" |
| 42 | + else |
| 43 | + # For main branch, build to root |
| 44 | + bundle exec jekyll build |
| 45 | + fi |
| 46 | + env: |
| 47 | + JEKYLL_ENV: production |
| 48 | + |
| 49 | + - name: Upload artifact |
| 50 | + uses: actions/upload-pages-artifact@v3 |
| 51 | + with: |
| 52 | + path: _site |
| 53 | + |
| 54 | + # Deploy to main domain when merged into main |
| 55 | + deploy-main: |
| 56 | + if: github.event_name == 'push' && github.ref == 'refs/heads/main' |
| 57 | + environment: |
| 58 | + name: github-pages |
| 59 | + url: ${{ steps.deployment.outputs.page_url }} |
| 60 | + runs-on: ubuntu-latest |
| 61 | + needs: build |
| 62 | + steps: |
| 63 | + - name: Deploy to GitHub Pages |
| 64 | + id: deployment |
| 65 | + uses: actions/deploy-pages@v4 |
| 66 | + |
| 67 | + # Deploy to PR-specific folder on pull requests |
| 68 | + deploy-pr: |
| 69 | + if: github.event_name == 'pull_request' |
| 70 | + runs-on: ubuntu-latest |
| 71 | + needs: build |
| 72 | + steps: |
| 73 | + - name: Checkout gh-pages branch |
| 74 | + uses: actions/checkout@v4 |
| 75 | + with: |
| 76 | + ref: gh-pages |
| 77 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 78 | + |
| 79 | + - name: Download artifact |
| 80 | + uses: actions/download-artifact@v4 |
| 81 | + with: |
| 82 | + name: github-pages |
| 83 | + path: ./artifact |
| 84 | + |
| 85 | + - name: Extract artifact |
| 86 | + run: | |
| 87 | + mkdir -p pr-${{ github.event.pull_request.number }} |
| 88 | + tar -xvf ./artifact/artifact.tar -C pr-${{ github.event.pull_request.number }} |
| 89 | +
|
| 90 | + - name: Configure Git |
| 91 | + run: | |
| 92 | + git config user.name "github-actions[bot]" |
| 93 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 94 | +
|
| 95 | + - name: Commit and push PR preview |
| 96 | + run: | |
| 97 | + git add pr-${{ github.event.pull_request.number }} |
| 98 | + git commit -m "Deploy PR #${{ github.event.pull_request.number }} preview" || echo "No changes to commit" |
| 99 | + git push origin gh-pages |
| 100 | +
|
| 101 | + - name: Comment PR with preview URL |
| 102 | + uses: actions/github-script@v7 |
| 103 | + with: |
| 104 | + script: | |
| 105 | + const prNumber = context.payload.pull_request.number; |
| 106 | + const previewUrl = `https://php-debugger.github.io/pr-${prNumber}/`; |
| 107 | +
|
| 108 | + const comment = `## 🚀 Preview Deployment |
| 109 | +
|
| 110 | + Your changes have been deployed to a preview environment: |
| 111 | +
|
| 112 | + **Preview URL:** ${previewUrl} |
| 113 | +
|
| 114 | + This preview will be updated automatically when you push new commits to this PR.`; |
| 115 | +
|
| 116 | + // Find existing preview comment |
| 117 | + const { data: comments } = await github.rest.issues.listComments({ |
| 118 | + owner: context.repo.owner, |
| 119 | + repo: context.repo.repo, |
| 120 | + issue_number: prNumber, |
| 121 | + }); |
| 122 | +
|
| 123 | + const existingComment = comments.find(c => |
| 124 | + c.user.login === 'github-actions[bot]' && |
| 125 | + c.body.includes('Preview Deployment') |
| 126 | + ); |
| 127 | +
|
| 128 | + if (existingComment) { |
| 129 | + // Update existing comment |
| 130 | + await github.rest.issues.updateComment({ |
| 131 | + owner: context.repo.owner, |
| 132 | + repo: context.repo.repo, |
| 133 | + comment_id: existingComment.id, |
| 134 | + body: comment |
| 135 | + }); |
| 136 | + } else { |
| 137 | + // Create new comment |
| 138 | + await github.rest.issues.createComment({ |
| 139 | + owner: context.repo.owner, |
| 140 | + repo: context.repo.repo, |
| 141 | + issue_number: prNumber, |
| 142 | + body: comment |
| 143 | + }); |
| 144 | + } |
0 commit comments