|
1 | 1 | # Workflow for building and deploying a mdBook site to GitHub Pages. |
2 | | -# Publishes to the gh-pages branch so PR previews (pr-<number>/) from |
3 | | -# mdbook-pr-preview.yml are served from the same branch. Set Pages source |
4 | | -# to "Deploy from a branch" → gh-pages, folder / (root). |
| 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". |
5 | 5 | # |
6 | 6 | # See: https://rust-lang.github.io/mdBook/index.html |
7 | 7 | name: Deploy mdBook site to Pages |
8 | 8 |
|
9 | 9 | on: |
10 | | - # Runs on pushes targeting the default branch |
11 | 10 | push: |
12 | 11 | branches: ["main"] |
13 | | - |
14 | | - # Allows you to run this workflow manually from the Actions tab |
15 | 12 | workflow_dispatch: |
16 | 13 |
|
17 | | -# Push to gh-pages so PR previews (mdbook-pr-preview) are served from the same branch. |
18 | | -# Repository Settings → Pages → Source: Deploy from a branch → gh-pages / (root). |
19 | 14 | permissions: |
20 | | - contents: write |
| 15 | + contents: read |
| 16 | + pages: write |
| 17 | + id-token: write |
21 | 18 |
|
22 | 19 | concurrency: |
23 | 20 | group: "pages" |
24 | 21 | cancel-in-progress: false |
25 | 22 |
|
26 | 23 | jobs: |
27 | | - build-and-deploy: |
| 24 | + build: |
28 | 25 | runs-on: ubuntu-latest |
29 | 26 | env: |
30 | 27 | CARGO_HOME: ${{ github.workspace }}/.cargo |
31 | 28 | RUSTUP_HOME: ${{ github.workspace }}/.rustup |
32 | 29 | steps: |
33 | 30 | - uses: actions/checkout@v4 |
| 31 | + |
34 | 32 | - name: Cache Cargo registry and git index |
35 | 33 | uses: actions/cache@v4 |
36 | 34 | with: |
@@ -60,24 +58,46 @@ jobs: |
60 | 58 | key: ${{ runner.os }}-mdbook-bin-${{ hashFiles('scripts/install-mdbook.sh') }} |
61 | 59 | restore-keys: | |
62 | 60 | ${{ runner.os }}-mdbook-bin- |
| 61 | +
|
63 | 62 | - name: Install mdBook |
64 | 63 | if: steps.mdbook-cache.outputs.cache-hit != 'true' |
65 | 64 | run: bash scripts/install-mdbook.sh |
66 | 65 | env: |
67 | 66 | REPO_ROOT: ${{ github.workspace }} |
| 67 | + |
68 | 68 | - name: Add Cargo bin to PATH |
69 | 69 | run: echo "${CARGO_HOME}/bin" >> "$GITHUB_PATH" |
| 70 | + |
70 | 71 | - name: Build with mdBook |
71 | 72 | run: ${{ env.CARGO_HOME }}/bin/mdbook build |
72 | | - - name: Deploy to gh-pages (root) |
| 73 | + |
| 74 | + - name: Merge PR previews from gh-pages into artifact |
| 75 | + run: | |
| 76 | + git fetch origin gh-pages 2>/dev/null || true |
| 77 | + if git rev-parse -q origin/gh-pages >/dev/null 2>&1; then |
| 78 | + for d in $(git ls-tree -d --name-only origin/gh-pages 2>/dev/null | grep -E '^pr-[0-9]+$'); do |
| 79 | + git archive origin/gh-pages "$d" | tar -x -C book |
| 80 | + done |
| 81 | + fi |
| 82 | +
|
| 83 | + - name: Upload artifact |
73 | 84 | if: ${{ !env.ACT }} |
74 | | - uses: peaceiris/actions-gh-pages@v4 |
| 85 | + uses: actions/upload-pages-artifact@v3 |
75 | 86 | with: |
76 | | - github_token: ${{ secrets.GITHUB_TOKEN }} |
77 | | - publish_branch: gh-pages |
78 | | - publish_dir: ./book |
79 | | - keep_files: true |
| 87 | + path: ./book |
| 88 | + |
80 | 89 | - name: Confirm local act build |
81 | 90 | if: ${{ env.ACT }} |
82 | | - run: | |
83 | | - echo "Local act run: mdBook build completed; skipping gh-pages deploy." |
| 91 | + run: echo "Local act run: mdBook build completed; skipping upload." |
| 92 | + |
| 93 | + deploy: |
| 94 | + if: ${{ !env.ACT }} |
| 95 | + environment: |
| 96 | + name: github-pages |
| 97 | + url: ${{ steps.deployment.outputs.page_url }} |
| 98 | + runs-on: ubuntu-latest |
| 99 | + needs: build |
| 100 | + steps: |
| 101 | + - name: Deploy to GitHub Pages |
| 102 | + id: deployment |
| 103 | + uses: actions/deploy-pages@v4 |
0 commit comments