|
| 1 | +name: Rustdoc PR Preview |
| 2 | + |
| 3 | +on: |
| 4 | + issue_comment: |
| 5 | + types: [created] |
| 6 | + pull_request: |
| 7 | + types: [closed] |
| 8 | + |
| 9 | +jobs: |
| 10 | + rustdoc-preview: |
| 11 | + # Only run on issue_comment, not on PR close |
| 12 | + if: github.event_name == 'issue_comment' && github.event.issue.pull_request && contains(github.event.comment.body, '/rustdoc-preview') |
| 13 | + runs-on: ubuntu-latest |
| 14 | + steps: |
| 15 | + - name: Check if commenter is a collaborator |
| 16 | + id: collaborator-check |
| 17 | + uses: actions/github-script@v7 |
| 18 | + with: |
| 19 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 20 | + script: | |
| 21 | + const commenter = context.payload.comment.user.login; |
| 22 | + const owner = context.repo.owner; |
| 23 | + const repo = context.repo.repo; |
| 24 | + try { |
| 25 | + await github.rest.repos.checkCollaborator({ |
| 26 | + owner, |
| 27 | + repo, |
| 28 | + username: commenter |
| 29 | + }); |
| 30 | + return true; |
| 31 | + } catch (e) { |
| 32 | + return false; |
| 33 | + } |
| 34 | + # Only continue if the check passes |
| 35 | + - name: Fail if not collaborator |
| 36 | + if: steps.collaborator-check.outputs.result != 'true' |
| 37 | + run: | |
| 38 | + echo "Commenter is not a collaborator. Skipping preview build." |
| 39 | + exit 1 |
| 40 | +
|
| 41 | + - name: Checkout PR branch |
| 42 | + uses: actions/checkout@v4 |
| 43 | + with: |
| 44 | + # Check out the PR's branch |
| 45 | + ref: ${{ github.event.pull_request.head.ref }} |
| 46 | + |
| 47 | + - name: Install Rust toolchain |
| 48 | + uses: actions-rs/toolchain@v1 |
| 49 | + with: |
| 50 | + toolchain: stable |
| 51 | + |
| 52 | + - name: Build rustdoc |
| 53 | + run: cargo rustdoc -- --cfg docsrs |
| 54 | + |
| 55 | + - name: Deploy rustdoc to gh-pages/pr-<PR_NUMBER> |
| 56 | + uses: peaceiris/actions-gh-pages@v3 |
| 57 | + with: |
| 58 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 59 | + publish_dir: ./target/doc |
| 60 | + # Publish to pr-<PR_NUMBER> subdir |
| 61 | + destination_dir: pr-${{ github.event.issue.number }} |
| 62 | + keep_files: true |
| 63 | + |
| 64 | + - name: Comment preview link on PR |
| 65 | + uses: actions/github-script@v7 |
| 66 | + with: |
| 67 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 68 | + script: | |
| 69 | + const pr_number = context.issue.number; |
| 70 | + const repo = context.repo.repo; |
| 71 | + const owner = context.repo.owner; |
| 72 | + const url = `https://${owner}.github.io/${repo}/pr-${pr_number}/`; |
| 73 | + github.rest.issues.createComment({ |
| 74 | + issue_number: pr_number, |
| 75 | + owner, |
| 76 | + repo, |
| 77 | + body: `📝 Rustdoc preview for this PR: [View docs](${url})` |
| 78 | + }); |
| 79 | +
|
| 80 | + rustdoc-preview-cleanup: |
| 81 | + # Only run on PR close/merge |
| 82 | + if: github.event_name == 'pull_request' && github.event.action == 'closed' |
| 83 | + runs-on: ubuntu-latest |
| 84 | + steps: |
| 85 | + - name: Checkout gh-pages branch |
| 86 | + uses: actions/checkout@v4 |
| 87 | + with: |
| 88 | + ref: gh-pages |
| 89 | + persist-credentials: true |
| 90 | + |
| 91 | + - name: Remove PR preview directory |
| 92 | + run: | |
| 93 | + rm -rf pr-${{ github.event.pull_request.number }} |
| 94 | +
|
| 95 | + - name: Commit and push removal |
| 96 | + run: | |
| 97 | + git config user.name "github-actions" |
| 98 | + git config user.email "github-actions@github.com" |
| 99 | + git add . |
| 100 | + git commit -m "Remove rustdoc preview for PR #${{ github.event.pull_request.number }}" || echo "Nothing to commit" |
| 101 | + git push |
0 commit comments