|
| 1 | +name: Documentation Preview |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + paths: |
| 6 | + - 'docs-site/**' |
| 7 | + - 'docs/**' |
| 8 | + - '*.md' |
| 9 | + - '.github/workflows/docs-preview.yml' |
| 10 | + |
| 11 | +permissions: |
| 12 | + contents: read |
| 13 | + pull-requests: write |
| 14 | + |
| 15 | +concurrency: |
| 16 | + group: docs-preview-${{ github.event.pull_request.number }} |
| 17 | + cancel-in-progress: true |
| 18 | + |
| 19 | +jobs: |
| 20 | + build-preview: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + steps: |
| 23 | + - name: Checkout |
| 24 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v4 |
| 25 | + |
| 26 | + - name: Setup Node.js |
| 27 | + uses: actions/setup-node@6044e13b5dc448c55e2357c09f80417699197238 # v6.2.0 |
| 28 | + with: |
| 29 | + node-version: '20' |
| 30 | + cache: 'npm' |
| 31 | + cache-dependency-path: docs-site/package-lock.json |
| 32 | + |
| 33 | + - name: Install dependencies |
| 34 | + run: | |
| 35 | + cd docs-site |
| 36 | + npm ci |
| 37 | +
|
| 38 | + - name: Build documentation |
| 39 | + id: build |
| 40 | + continue-on-error: true |
| 41 | + run: | |
| 42 | + cd docs-site |
| 43 | + npm run build |
| 44 | +
|
| 45 | + - name: Upload preview artifact |
| 46 | + if: steps.build.outcome == 'success' |
| 47 | + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 |
| 48 | + with: |
| 49 | + name: docs-preview-pr-${{ github.event.pull_request.number }} |
| 50 | + path: docs-site/dist |
| 51 | + retention-days: 7 |
| 52 | + |
| 53 | + - name: Comment on PR |
| 54 | + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7 |
| 55 | + with: |
| 56 | + script: | |
| 57 | + const artifactUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`; |
| 58 | + const buildOutcome = '${{ steps.build.outcome }}'; |
| 59 | + const body = buildOutcome === 'success' |
| 60 | + ? [ |
| 61 | + '## Documentation Preview', |
| 62 | + '', |
| 63 | + `Documentation has been built for this PR.`, |
| 64 | + '', |
| 65 | + `**[Download preview artifact](${artifactUrl})**`, |
| 66 | + '', |
| 67 | + 'To view locally:', |
| 68 | + '1. Download the `docs-preview-pr-${{ github.event.pull_request.number }}` artifact from the workflow run', |
| 69 | + '2. Unzip and open `index.html` in your browser', |
| 70 | + '', |
| 71 | + `_Built from commit ${context.sha.substring(0, 7)}_`, |
| 72 | + ].join('\n') |
| 73 | + : [ |
| 74 | + '## Documentation Preview', |
| 75 | + '', |
| 76 | + `Documentation build failed for this PR. [View logs](${artifactUrl}).`, |
| 77 | + '', |
| 78 | + `_Built from commit ${context.sha.substring(0, 7)}_`, |
| 79 | + ].join('\n'); |
| 80 | +
|
| 81 | + // Find existing comment |
| 82 | + const { data: comments } = await github.rest.issues.listComments({ |
| 83 | + owner: context.repo.owner, |
| 84 | + repo: context.repo.repo, |
| 85 | + issue_number: context.issue.number, |
| 86 | + }); |
| 87 | +
|
| 88 | + const botComment = comments.find(c => |
| 89 | + c.user.type === 'Bot' && c.body.includes('Documentation Preview') |
| 90 | + ); |
| 91 | +
|
| 92 | + if (botComment) { |
| 93 | + await github.rest.issues.updateComment({ |
| 94 | + owner: context.repo.owner, |
| 95 | + repo: context.repo.repo, |
| 96 | + comment_id: botComment.id, |
| 97 | + body, |
| 98 | + }); |
| 99 | + } else { |
| 100 | + await github.rest.issues.createComment({ |
| 101 | + owner: context.repo.owner, |
| 102 | + repo: context.repo.repo, |
| 103 | + issue_number: context.issue.number, |
| 104 | + body, |
| 105 | + }); |
| 106 | + } |
0 commit comments