|
| 1 | +name: Deploy mdBook PR preview |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened, closed] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: write |
| 9 | + pull-requests: write |
| 10 | + |
| 11 | +concurrency: |
| 12 | + # One running preview job per PR number. If new commits arrive, cancel older runs. |
| 13 | + group: pr-preview-${{ github.event.pull_request.number }} |
| 14 | + cancel-in-progress: true |
| 15 | + |
| 16 | +jobs: |
| 17 | + deploy-preview: |
| 18 | + if: github.event.action != 'closed' && github.event.pull_request.head.repo.full_name == github.repository |
| 19 | + runs-on: ubuntu-latest |
| 20 | + env: |
| 21 | + CARGO_HOME: ${{ github.workspace }}/.cargo |
| 22 | + RUSTUP_HOME: ${{ github.workspace }}/.rustup |
| 23 | + PREVIEW_DIR: pr-${{ github.event.pull_request.number }} |
| 24 | + PREVIEW_URL: https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/pr-${{ github.event.pull_request.number }}/ |
| 25 | + steps: |
| 26 | + - uses: actions/checkout@v4 |
| 27 | + |
| 28 | + - name: Install mdBook |
| 29 | + run: bash scripts/install-mdbook.sh |
| 30 | + env: |
| 31 | + REPO_ROOT: ${{ github.workspace }} |
| 32 | + |
| 33 | + - name: Build with mdBook |
| 34 | + run: ${{ env.CARGO_HOME }}/bin/mdbook build |
| 35 | + |
| 36 | + - name: Deploy preview to gh-pages/pr-<id> |
| 37 | + # Important: we build from the PR branch, but publish static files to gh-pages. |
| 38 | + # GitHub Pages serves from a configured publishing branch (gh-pages here), |
| 39 | + # not from arbitrary PR branches. |
| 40 | + uses: peaceiris/actions-gh-pages@v4 |
| 41 | + with: |
| 42 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 43 | + publish_branch: gh-pages |
| 44 | + publish_dir: ./book |
| 45 | + # Keep each preview isolated under pr-<number>/ so multiple PR previews can coexist. |
| 46 | + destination_dir: ${{ env.PREVIEW_DIR }} |
| 47 | + keep_files: true |
| 48 | + |
| 49 | + - name: Comment preview URL on PR |
| 50 | + uses: actions/github-script@v7 |
| 51 | + with: |
| 52 | + # Keep workflow YAML concise: run comment logic from .github/workflows/mdbook/comment-preview-url.js. |
| 53 | + script: | |
| 54 | + const { commentPreviewUrl } = require('./.github/workflows/mdbook/comment-preview-url.js') |
| 55 | + await commentPreviewUrl({ |
| 56 | + github, |
| 57 | + context, |
| 58 | + previewUrl: process.env.PREVIEW_URL, |
| 59 | + }) |
| 60 | +
|
| 61 | + cleanup-preview: |
| 62 | + if: github.event.action == 'closed' && github.event.pull_request.head.repo.full_name == github.repository |
| 63 | + runs-on: ubuntu-latest |
| 64 | + env: |
| 65 | + PREVIEW_DIR: pr-${{ github.event.pull_request.number }} |
| 66 | + steps: |
| 67 | + - name: Checkout gh-pages |
| 68 | + # Cleanup edits the published site branch directly because previews live there. |
| 69 | + uses: actions/checkout@v4 |
| 70 | + with: |
| 71 | + ref: gh-pages |
| 72 | + fetch-depth: 0 |
| 73 | + |
| 74 | + - name: Remove preview directory |
| 75 | + run: | |
| 76 | + # Keep workflow YAML concise: run cleanup logic from .github/workflows/mdbook/cleanup-preview.sh. |
| 77 | + bash .github/workflows/mdbook/cleanup-preview.sh "${PREVIEW_DIR}" "${{ github.event.pull_request.number }}" |
0 commit comments