Skip to content

Commit 09dd868

Browse files
authored
fix(pages): merge pr-* from gh-pages into deploy artifact so PR previews are served (#108)
Made-with: Cursor
1 parent d4c469d commit 09dd868

File tree

1 file changed

+70
-18
lines changed

1 file changed

+70
-18
lines changed

.github/workflows/mdbook.yml

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,110 @@
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".
45
#
6+
# See: https://rust-lang.github.io/mdBook/index.html
57
name: Deploy mdBook site to Pages
68

79
on:
8-
# Runs on pushes targeting the default branch
910
push:
1011
branches: ["main"]
11-
12-
# Allows you to run this workflow manually from the Actions tab
1312
workflow_dispatch:
1413

15-
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
1614
permissions:
1715
contents: read
1816
pages: write
1917
id-token: write
2018

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.
2319
concurrency:
2420
group: "pages"
2521
cancel-in-progress: false
2622

2723
jobs:
28-
# Build job
2924
build:
3025
runs-on: ubuntu-latest
3126
env:
32-
MDBOOK_VERSION: 0.4.52
33-
MDBOOK_MERMAID_VERSION: 0.10.0
3427
CARGO_HOME: ${{ github.workspace }}/.cargo
3528
RUSTUP_HOME: ${{ github.workspace }}/.rustup
3629
steps:
3730
- 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+
3862
- name: Install mdBook
63+
if: steps.mdbook-cache.outputs.cache-hit != 'true'
3964
run: bash scripts/install-mdbook.sh
4065
env:
4166
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+
4771
- name: Build with mdBook
4872
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+
4996
- name: Upload artifact
97+
if: ${{ !env.ACT }}
5098
uses: actions/upload-pages-artifact@v3
5199
with:
52100
path: ./book
53101

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+
55106
deploy:
107+
if: ${{ !env.ACT }}
56108
environment:
57109
name: github-pages
58110
url: ${{ steps.deployment.outputs.page_url }}

0 commit comments

Comments
 (0)