|
1 | 1 | # Workflow for building and deploying a mdBook site to GitHub Pages. |
2 | | -# Serves the main book at / and PR previews at /pr-<number>/ by merging |
3 | | -# pr-* directories from the gh-pages branch (pushed by mdbook-pr-preview.yml) |
4 | | -# into the deployed artifact. Works with Pages source "GitHub Actions". |
| 2 | +# - On push to main: deploys to the root of gh-pages. |
| 3 | +# - On pull_request: deploys a preview under pr-preview/pr-<number>/ on gh-pages, |
| 4 | +# comments the preview URL, and cleans up when the PR closes. |
| 5 | +# |
| 6 | +# Repo Settings required: |
| 7 | +# Pages -> Source: "Deploy from a branch" -> gh-pages / (root) |
| 8 | +# Actions -> General -> Workflow permissions: "Read and write permissions" |
5 | 9 | # |
6 | 10 | # See: https://rust-lang.github.io/mdBook/index.html |
7 | 11 | name: Deploy mdBook site to Pages |
8 | 12 |
|
9 | 13 | on: |
10 | 14 | push: |
11 | 15 | branches: ["main"] |
| 16 | + pull_request: |
| 17 | + types: [opened, synchronize, reopened, closed] |
12 | 18 | workflow_dispatch: |
13 | 19 |
|
14 | 20 | permissions: |
15 | | - contents: read |
16 | | - pages: write |
17 | | - id-token: write |
18 | | - |
19 | | -concurrency: |
20 | | - group: "pages" |
21 | | - cancel-in-progress: false |
| 21 | + contents: write |
| 22 | + pull-requests: write |
22 | 23 |
|
23 | 24 | jobs: |
24 | | - build: |
| 25 | + deploy: |
| 26 | + if: github.event_name == 'push' || github.event_name == 'workflow_dispatch' |
25 | 27 | runs-on: ubuntu-latest |
| 28 | + concurrency: |
| 29 | + group: pages |
| 30 | + cancel-in-progress: false |
26 | 31 | env: |
27 | 32 | CARGO_HOME: ${{ github.workspace }}/.cargo |
28 | 33 | RUSTUP_HOME: ${{ github.workspace }}/.rustup |
@@ -71,40 +76,73 @@ jobs: |
71 | 76 | - name: Build with mdBook |
72 | 77 | run: ${{ env.CARGO_HOME }}/bin/mdbook build |
73 | 78 |
|
74 | | - - name: Merge PR previews from gh-pages into artifact |
75 | | - run: | |
76 | | - set -e |
77 | | - echo "Fetching gh-pages branch..." |
78 | | - git fetch origin gh-pages 2>/dev/null || true |
79 | | - if ! git rev-parse -q origin/gh-pages >/dev/null 2>&1; then |
80 | | - echo "No gh-pages branch found; skipping PR preview merge." |
81 | | - exit 0 |
82 | | - fi |
83 | | - PR_DIRS=$(git ls-tree -d --name-only origin/gh-pages 2>/dev/null | grep -E '^pr-[0-9]+$' || true) |
84 | | - if [ -z "$PR_DIRS" ]; then |
85 | | - echo "No pr-* directories on gh-pages; skipping." |
86 | | - exit 0 |
87 | | - fi |
88 | | - echo "Merging PR preview dirs: $PR_DIRS" |
89 | | - for d in $PR_DIRS; do |
90 | | - echo " -> merging $d" |
91 | | - git archive origin/gh-pages "$d" | tar -x -C book |
92 | | - done |
93 | | - echo "Done. Contents of book/ after merge:" |
94 | | - ls -la book/ | head -30 |
95 | | -
|
96 | | - - name: Upload artifact |
97 | | - uses: actions/upload-pages-artifact@v3 |
| 79 | + - name: Deploy to GitHub Pages |
| 80 | + uses: peaceiris/actions-gh-pages@v4 |
98 | 81 | with: |
99 | | - path: ./book |
| 82 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 83 | + publish_dir: ./book |
| 84 | + keep_files: true |
100 | 85 |
|
101 | | - deploy: |
102 | | - environment: |
103 | | - name: github-pages |
104 | | - url: ${{ steps.deployment.outputs.page_url }} |
| 86 | + preview: |
| 87 | + if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository |
105 | 88 | runs-on: ubuntu-latest |
106 | | - needs: build |
| 89 | + concurrency: preview-${{ github.ref }} |
| 90 | + env: |
| 91 | + CARGO_HOME: ${{ github.workspace }}/.cargo |
| 92 | + RUSTUP_HOME: ${{ github.workspace }}/.rustup |
107 | 93 | steps: |
108 | | - - name: Deploy to GitHub Pages |
109 | | - id: deployment |
110 | | - uses: actions/deploy-pages@v4 |
| 94 | + - uses: actions/checkout@v4 |
| 95 | + |
| 96 | + - name: Cache Cargo registry and git index |
| 97 | + if: github.event.action != 'closed' |
| 98 | + uses: actions/cache@v4 |
| 99 | + with: |
| 100 | + path: | |
| 101 | + ${{ env.CARGO_HOME }}/registry |
| 102 | + ${{ env.CARGO_HOME }}/git |
| 103 | + key: ${{ runner.os }}-cargo-registry-${{ hashFiles('scripts/install-mdbook.sh') }} |
| 104 | + restore-keys: | |
| 105 | + ${{ runner.os }}-cargo-registry- |
| 106 | +
|
| 107 | + - name: Cache Rust toolchains |
| 108 | + if: github.event.action != 'closed' |
| 109 | + uses: actions/cache@v4 |
| 110 | + with: |
| 111 | + path: | |
| 112 | + ${{ env.RUSTUP_HOME }}/toolchains |
| 113 | + ${{ env.RUSTUP_HOME }}/update-hashes |
| 114 | + key: ${{ runner.os }}-rust-toolchain-${{ hashFiles('scripts/install-mdbook.sh') }} |
| 115 | + restore-keys: | |
| 116 | + ${{ runner.os }}-rust-toolchain- |
| 117 | +
|
| 118 | + - name: Cache installed mdBook binaries |
| 119 | + if: github.event.action != 'closed' |
| 120 | + id: mdbook-cache |
| 121 | + uses: actions/cache@v4 |
| 122 | + with: |
| 123 | + path: | |
| 124 | + ${{ env.CARGO_HOME }}/bin |
| 125 | + key: ${{ runner.os }}-mdbook-bin-${{ hashFiles('scripts/install-mdbook.sh') }} |
| 126 | + restore-keys: | |
| 127 | + ${{ runner.os }}-mdbook-bin- |
| 128 | +
|
| 129 | + - name: Install mdBook |
| 130 | + if: github.event.action != 'closed' && steps.mdbook-cache.outputs.cache-hit != 'true' |
| 131 | + run: bash scripts/install-mdbook.sh |
| 132 | + env: |
| 133 | + REPO_ROOT: ${{ github.workspace }} |
| 134 | + |
| 135 | + - name: Add Cargo bin to PATH |
| 136 | + if: github.event.action != 'closed' |
| 137 | + run: echo "${CARGO_HOME}/bin" >> "$GITHUB_PATH" |
| 138 | + |
| 139 | + - name: Build with mdBook |
| 140 | + if: github.event.action != 'closed' |
| 141 | + run: ${{ env.CARGO_HOME }}/bin/mdbook build |
| 142 | + |
| 143 | + - name: Deploy PR preview |
| 144 | + uses: rossjrw/pr-preview-action@v1 |
| 145 | + with: |
| 146 | + source-dir: ./book |
| 147 | + preview-branch: gh-pages |
| 148 | + umbrella-dir: pr-preview |
0 commit comments