|
| 1 | +# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +name: Deploy PR Documentation Preview |
| 16 | + |
| 17 | +on: |
| 18 | + pull_request: |
| 19 | + branches: |
| 20 | + - dev |
| 21 | + # - main # Uncomment after dev is merged into main |
| 22 | + paths: |
| 23 | + - "docs/**" |
| 24 | + - "pyproject.toml" |
| 25 | + - ".github/workflows/docs-preview.yml" |
| 26 | + types: [opened, synchronize, reopened, closed] |
| 27 | + |
| 28 | +concurrency: |
| 29 | + group: docs-preview-${{ github.event.pull_request.number }} |
| 30 | + cancel-in-progress: true |
| 31 | + |
| 32 | +permissions: |
| 33 | + contents: write |
| 34 | + pull-requests: write |
| 35 | + |
| 36 | +jobs: |
| 37 | + build-and-deploy-preview: |
| 38 | + if: github.event.action != 'closed' |
| 39 | + runs-on: ubuntu-latest |
| 40 | + steps: |
| 41 | + - name: Checkout repository |
| 42 | + uses: actions/checkout@v4 |
| 43 | + |
| 44 | + - name: Install uv |
| 45 | + uses: astral-sh/setup-uv@v7 |
| 46 | + with: |
| 47 | + version: "0.9.5" |
| 48 | + enable-cache: true |
| 49 | + cache-dependency-glob: "pyproject.toml" |
| 50 | + |
| 51 | + - name: Build documentation |
| 52 | + run: uv run --group docs sphinx-build docs docs/_build/html |
| 53 | + |
| 54 | + - name: Deploy PR preview |
| 55 | + uses: peaceiris/actions-gh-pages@v4 |
| 56 | + with: |
| 57 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 58 | + publish_dir: ./docs/_build/html |
| 59 | + publish_branch: gh-pages |
| 60 | + destination_dir: pr-${{ github.event.pull_request.number }} |
| 61 | + keep_files: true |
| 62 | + user_name: "github-actions[bot]" |
| 63 | + user_email: "github-actions[bot]@users.noreply.github.com" |
| 64 | + commit_message: "docs: preview for PR #${{ github.event.pull_request.number }}" |
| 65 | + |
| 66 | + - name: Comment PR with preview link |
| 67 | + uses: actions/github-script@v7 |
| 68 | + with: |
| 69 | + script: | |
| 70 | + const prNumber = context.payload.pull_request.number; |
| 71 | + const owner = context.repo.owner; |
| 72 | + const repo = context.repo.repo; |
| 73 | + const previewUrl = `https://${owner}.github.io/${repo}/pr-${prNumber}/`; |
| 74 | +
|
| 75 | + const comments = await github.rest.issues.listComments({ |
| 76 | + owner, |
| 77 | + repo, |
| 78 | + issue_number: prNumber, |
| 79 | + }); |
| 80 | +
|
| 81 | + const botComment = comments.data.find( |
| 82 | + (c) => c.user.type === "Bot" && c.body.includes("Documentation Preview") |
| 83 | + ); |
| 84 | +
|
| 85 | + const body = `## Documentation Preview |
| 86 | +
|
| 87 | +:book: **Preview URL:** ${previewUrl} |
| 88 | + |
| 89 | +_Built from commit ${context.sha.substring(0, 7)}_`; |
| 90 | + |
| 91 | + if (botComment) { |
| 92 | + await github.rest.issues.updateComment({ |
| 93 | + owner, |
| 94 | + repo, |
| 95 | + comment_id: botComment.id, |
| 96 | + body, |
| 97 | + }); |
| 98 | + } else { |
| 99 | + await github.rest.issues.createComment({ |
| 100 | + owner, |
| 101 | + repo, |
| 102 | + issue_number: prNumber, |
| 103 | + body, |
| 104 | + }); |
| 105 | + } |
| 106 | + |
| 107 | + cleanup-preview: |
| 108 | + if: github.event.action == 'closed' |
| 109 | + runs-on: ubuntu-latest |
| 110 | + steps: |
| 111 | + - name: Checkout gh-pages branch |
| 112 | + uses: actions/checkout@v4 |
| 113 | + with: |
| 114 | + ref: gh-pages |
| 115 | + fetch-depth: 0 |
| 116 | + |
| 117 | + - name: Delete preview directory |
| 118 | + run: | |
| 119 | + PR_DIR="pr-${{ github.event.pull_request.number }}" |
| 120 | + if [ -d "$PR_DIR" ]; then |
| 121 | + git config user.name "github-actions[bot]" |
| 122 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 123 | + git rm -rf "$PR_DIR" |
| 124 | + git commit -m "docs: cleanup preview for PR #${{ github.event.pull_request.number }}" |
| 125 | + git push origin gh-pages |
| 126 | + else |
| 127 | + echo "Preview directory $PR_DIR does not exist, skipping cleanup" |
| 128 | + fi |
0 commit comments