|
| 1 | +name: PR Documentation Preview |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened, closed] |
| 6 | + paths: |
| 7 | + - 'docs/**' |
| 8 | + - 'mkdocs.yml' |
| 9 | + - 'requirements.txt' |
| 10 | + - '.github/workflows/pr-preview.yaml' |
| 11 | + |
| 12 | +permissions: |
| 13 | + contents: write |
| 14 | + pull-requests: write |
| 15 | + pages: write |
| 16 | + id-token: write |
| 17 | + |
| 18 | +jobs: |
| 19 | + build-preview: |
| 20 | + if: github.event.action != 'closed' |
| 21 | + runs-on: ubuntu-latest |
| 22 | + steps: |
| 23 | + - name: Checkout repository |
| 24 | + uses: actions/checkout@v4 |
| 25 | + with: |
| 26 | + submodules: true |
| 27 | + fetch-depth: 0 |
| 28 | + |
| 29 | + - name: Setup Python |
| 30 | + uses: actions/setup-python@v5 |
| 31 | + with: |
| 32 | + python-version: '3.11' |
| 33 | + |
| 34 | + - name: Cache pip dependencies |
| 35 | + uses: actions/cache@v4 |
| 36 | + with: |
| 37 | + path: ~/.cache/pip |
| 38 | + key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} |
| 39 | + restore-keys: | |
| 40 | + ${{ runner.os }}-pip- |
| 41 | +
|
| 42 | + - name: Install requirements |
| 43 | + run: pip install -r requirements.txt |
| 44 | + |
| 45 | + - name: Build documentation |
| 46 | + run: mkdocs build --verbose |
| 47 | + |
| 48 | + - name: Setup preview directory |
| 49 | + run: | |
| 50 | + mkdir -p pr-previews |
| 51 | + cp -r site/* pr-previews/ |
| 52 | + echo "<html><head><meta http-equiv='refresh' content='0; url=pr-${{ github.event.number }}/'></head><body>Redirecting to PR preview...</body></html>" > pr-previews/index.html |
| 53 | +
|
| 54 | + - name: Deploy to GitHub Pages (PR Preview) |
| 55 | + uses: peaceiris/actions-gh-pages@v3 |
| 56 | + with: |
| 57 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 58 | + publish_dir: ./site |
| 59 | + destination_dir: pr-${{ github.event.number }} |
| 60 | + keep_files: true |
| 61 | + enable_jekyll: false |
| 62 | + |
| 63 | + - name: Comment PR with preview link |
| 64 | + uses: actions/github-script@v7 |
| 65 | + with: |
| 66 | + script: | |
| 67 | + const prNumber = context.payload.pull_request.number; |
| 68 | + const repoName = context.repo.repo; |
| 69 | + const repoOwner = context.repo.owner; |
| 70 | + const previewUrl = `https://${repoOwner}.github.io/${repoName}/pr-${prNumber}/`; |
| 71 | + |
| 72 | + // Check if we already commented |
| 73 | + const comments = await github.rest.issues.listComments({ |
| 74 | + owner: repoOwner, |
| 75 | + repo: repoName, |
| 76 | + issue_number: prNumber |
| 77 | + }); |
| 78 | + |
| 79 | + const botComment = comments.data.find(comment => |
| 80 | + comment.user.login === 'github-actions[bot]' && |
| 81 | + comment.body.includes('📖 Documentation Preview') |
| 82 | + ); |
| 83 | + |
| 84 | + const commentBody = `## 📖 Documentation Preview |
| 85 | + |
| 86 | + The documentation for this PR has been built and is available for preview: |
| 87 | + |
| 88 | + 🔗 **[View PR Preview](${previewUrl})** |
| 89 | + |
| 90 | + This preview will be updated automatically when you push changes to this PR. |
| 91 | + |
| 92 | + --- |
| 93 | + *Preview generated from commit: ${context.sha.substring(0, 7)}*`; |
| 94 | + |
| 95 | + if (botComment) { |
| 96 | + // Update existing comment |
| 97 | + await github.rest.issues.updateComment({ |
| 98 | + owner: repoOwner, |
| 99 | + repo: repoName, |
| 100 | + comment_id: botComment.id, |
| 101 | + body: commentBody |
| 102 | + }); |
| 103 | + } else { |
| 104 | + // Create new comment |
| 105 | + await github.rest.issues.createComment({ |
| 106 | + owner: repoOwner, |
| 107 | + repo: repoName, |
| 108 | + issue_number: prNumber, |
| 109 | + body: commentBody |
| 110 | + }); |
| 111 | + } |
| 112 | +
|
| 113 | + cleanup-preview: |
| 114 | + if: github.event.action == 'closed' |
| 115 | + runs-on: ubuntu-latest |
| 116 | + steps: |
| 117 | + - name: Checkout gh-pages branch |
| 118 | + uses: actions/checkout@v4 |
| 119 | + with: |
| 120 | + ref: gh-pages |
| 121 | + fetch-depth: 0 |
| 122 | + |
| 123 | + - name: Remove PR preview directory |
| 124 | + run: | |
| 125 | + if [ -d "pr-${{ github.event.number }}" ]; then |
| 126 | + git config --local user.email "[email protected]" |
| 127 | + git config --local user.name "GitHub Action" |
| 128 | + rm -rf pr-${{ github.event.number }} |
| 129 | + git add . |
| 130 | + git commit -m "Clean up PR #${{ github.event.number }} preview" || exit 0 |
| 131 | + git push |
| 132 | + fi |
| 133 | +
|
| 134 | + - name: Comment PR cleanup |
| 135 | + uses: actions/github-script@v7 |
| 136 | + with: |
| 137 | + script: | |
| 138 | + const prNumber = context.payload.pull_request.number; |
| 139 | + const repoName = context.repo.repo; |
| 140 | + const repoOwner = context.repo.owner; |
| 141 | + |
| 142 | + // Find and update the bot comment |
| 143 | + const comments = await github.rest.issues.listComments({ |
| 144 | + owner: repoOwner, |
| 145 | + repo: repoName, |
| 146 | + issue_number: prNumber |
| 147 | + }); |
| 148 | + |
| 149 | + const botComment = comments.data.find(comment => |
| 150 | + comment.user.login === 'github-actions[bot]' && |
| 151 | + comment.body.includes('📖 Documentation Preview') |
| 152 | + ); |
| 153 | + |
| 154 | + if (botComment) { |
| 155 | + const updatedBody = botComment.body + '\n\n**✅ Preview cleaned up** - PR has been closed/merged.'; |
| 156 | + await github.rest.issues.updateComment({ |
| 157 | + owner: repoOwner, |
| 158 | + repo: repoName, |
| 159 | + comment_id: botComment.id, |
| 160 | + body: updatedBody |
| 161 | + }); |
| 162 | + } |
0 commit comments