Skip to content

Commit 269b4b3

Browse files
committed
fix: adjust approach with gh-pages
1 parent b0d6f8a commit 269b4b3

1 file changed

Lines changed: 38 additions & 18 deletions

File tree

.github/workflows/mdbook.yml

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
11
# 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".
55
#
66
# See: https://rust-lang.github.io/mdBook/index.html
77
name: Deploy mdBook site to Pages
88

99
on:
10-
# Runs on pushes targeting the default branch
1110
push:
1211
branches: ["main"]
13-
14-
# Allows you to run this workflow manually from the Actions tab
1512
workflow_dispatch:
1613

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).
1914
permissions:
20-
contents: write
15+
contents: read
16+
pages: write
17+
id-token: write
2118

2219
concurrency:
2320
group: "pages"
2421
cancel-in-progress: false
2522

2623
jobs:
27-
build-and-deploy:
24+
build:
2825
runs-on: ubuntu-latest
2926
env:
3027
CARGO_HOME: ${{ github.workspace }}/.cargo
3128
RUSTUP_HOME: ${{ github.workspace }}/.rustup
3229
steps:
3330
- uses: actions/checkout@v4
31+
3432
- name: Cache Cargo registry and git index
3533
uses: actions/cache@v4
3634
with:
@@ -60,24 +58,46 @@ jobs:
6058
key: ${{ runner.os }}-mdbook-bin-${{ hashFiles('scripts/install-mdbook.sh') }}
6159
restore-keys: |
6260
${{ runner.os }}-mdbook-bin-
61+
6362
- name: Install mdBook
6463
if: steps.mdbook-cache.outputs.cache-hit != 'true'
6564
run: bash scripts/install-mdbook.sh
6665
env:
6766
REPO_ROOT: ${{ github.workspace }}
67+
6868
- name: Add Cargo bin to PATH
6969
run: echo "${CARGO_HOME}/bin" >> "$GITHUB_PATH"
70+
7071
- name: Build with mdBook
7172
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
7384
if: ${{ !env.ACT }}
74-
uses: peaceiris/actions-gh-pages@v4
85+
uses: actions/upload-pages-artifact@v3
7586
with:
76-
github_token: ${{ secrets.GITHUB_TOKEN }}
77-
publish_branch: gh-pages
78-
publish_dir: ./book
79-
keep_files: true
87+
path: ./book
88+
8089
- name: Confirm local act build
8190
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

Comments
 (0)