|
1 | | -# Workflow for building and deploying a mdBook site to GitHub Pages |
2 | | -# |
3 | | -# To get started with mdBook see: https://rust-lang.github.io/mdBook/index.html |
| 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". |
4 | 5 | # |
| 6 | +# See: https://rust-lang.github.io/mdBook/index.html |
5 | 7 | name: Deploy mdBook site to Pages |
6 | 8 |
|
7 | 9 | on: |
8 | | - # Runs on pushes targeting the default branch |
9 | 10 | push: |
10 | 11 | branches: ["main"] |
11 | | - |
12 | | - # Allows you to run this workflow manually from the Actions tab |
13 | 12 | workflow_dispatch: |
14 | 13 |
|
15 | | -# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages |
16 | 14 | permissions: |
17 | 15 | contents: read |
18 | 16 | pages: write |
19 | 17 | id-token: write |
20 | 18 |
|
21 | | -# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued. |
22 | | -# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete. |
23 | 19 | concurrency: |
24 | 20 | group: "pages" |
25 | 21 | cancel-in-progress: false |
26 | 22 |
|
27 | 23 | jobs: |
28 | | - # Build job |
29 | 24 | build: |
30 | 25 | runs-on: ubuntu-latest |
31 | 26 | env: |
32 | | - MDBOOK_VERSION: 0.4.52 |
33 | | - MDBOOK_MERMAID_VERSION: 0.10.0 |
34 | 27 | CARGO_HOME: ${{ github.workspace }}/.cargo |
35 | 28 | RUSTUP_HOME: ${{ github.workspace }}/.rustup |
36 | 29 | steps: |
37 | 30 | - uses: actions/checkout@v4 |
| 31 | + |
| 32 | + - name: Cache Cargo registry and git index |
| 33 | + uses: actions/cache@v4 |
| 34 | + with: |
| 35 | + path: | |
| 36 | + ${{ env.CARGO_HOME }}/registry |
| 37 | + ${{ env.CARGO_HOME }}/git |
| 38 | + key: ${{ runner.os }}-cargo-registry-${{ hashFiles('scripts/install-mdbook.sh') }} |
| 39 | + restore-keys: | |
| 40 | + ${{ runner.os }}-cargo-registry- |
| 41 | +
|
| 42 | + - name: Cache Rust toolchains |
| 43 | + uses: actions/cache@v4 |
| 44 | + with: |
| 45 | + path: | |
| 46 | + ${{ env.RUSTUP_HOME }}/toolchains |
| 47 | + ${{ env.RUSTUP_HOME }}/update-hashes |
| 48 | + key: ${{ runner.os }}-rust-toolchain-${{ hashFiles('scripts/install-mdbook.sh') }} |
| 49 | + restore-keys: | |
| 50 | + ${{ runner.os }}-rust-toolchain- |
| 51 | +
|
| 52 | + - name: Cache installed mdBook binaries |
| 53 | + id: mdbook-cache |
| 54 | + uses: actions/cache@v4 |
| 55 | + with: |
| 56 | + path: | |
| 57 | + ${{ env.CARGO_HOME }}/bin |
| 58 | + key: ${{ runner.os }}-mdbook-bin-${{ hashFiles('scripts/install-mdbook.sh') }} |
| 59 | + restore-keys: | |
| 60 | + ${{ runner.os }}-mdbook-bin- |
| 61 | +
|
38 | 62 | - name: Install mdBook |
| 63 | + if: steps.mdbook-cache.outputs.cache-hit != 'true' |
39 | 64 | run: bash scripts/install-mdbook.sh |
40 | 65 | env: |
41 | 66 | REPO_ROOT: ${{ github.workspace }} |
42 | | - MDBOOK_VERSION: ${{ env.MDBOOK_VERSION }} |
43 | | - MDBOOK_MERMAID_VERSION: ${{ env.MDBOOK_MERMAID_VERSION }} |
44 | | - - name: Setup Pages |
45 | | - id: pages |
46 | | - uses: actions/configure-pages@v5 |
| 67 | + |
| 68 | + - name: Add Cargo bin to PATH |
| 69 | + run: echo "${CARGO_HOME}/bin" >> "$GITHUB_PATH" |
| 70 | + |
47 | 71 | - name: Build with mdBook |
48 | 72 | run: ${{ env.CARGO_HOME }}/bin/mdbook build |
| 73 | + |
| 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 | +
|
49 | 96 | - name: Upload artifact |
| 97 | + if: ${{ !env.ACT }} |
50 | 98 | uses: actions/upload-pages-artifact@v3 |
51 | 99 | with: |
52 | 100 | path: ./book |
53 | 101 |
|
54 | | - # Deployment job |
| 102 | + - name: Confirm local act build |
| 103 | + if: ${{ env.ACT }} |
| 104 | + run: echo "Local act run: mdBook build completed; skipping upload." |
| 105 | + |
55 | 106 | deploy: |
| 107 | + if: ${{ !env.ACT }} |
56 | 108 | environment: |
57 | 109 | name: github-pages |
58 | 110 | url: ${{ steps.deployment.outputs.page_url }} |
|
0 commit comments