将写好的 mdBook 自动部署到 GitHub 仓库的 gh-pages 分支
关联问题
Publishing mdBook on Github Page
解决方法
Automated Deployment: GitHub Actions
笔者言
解决方法里有一些需要配置 Action, 可以按照这一小节Raw Example。
在 mdBook 根目录创建文件 .github/workflows/deploy.yml。每次修改推送后, GitHub 会在当前分支和 gh-pages 分支进行CI/CD, 部署成功后即可在路由 https://<GitHub用户名>.github.io/<仓库名>/ 看到写好的 mdBook !
YMAL File for Deploy
name: Deploy
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
permissions:
contents: write # To push a branch
pull-requests: write # To create a PR from that branch
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install latest mdbook
run: |
tag=$(curl 'https://api.github.com/repos/rust-lang/mdbook/releases/latest' | jq -r '.tag_name')
url="https://github.com/rust-lang/mdbook/releases/download/${tag}/mdbook-${tag}-x86_64-unknown-linux-gnu.tar.gz"
mkdir mdbook
curl -sSL $url | tar -xz --directory=./mdbook
echo `pwd`/mdbook >> $GITHUB_PATH
- name: Deploy GitHub Pages
run: |
# This assumes your book is in the root of your repository.
# Just add a `cd` here if you need to change to another directory.
mdbook build
git worktree add gh-pages
git config user.name "Deploy from CI"
git config user.email ""
cd gh-pages
# Delete the ref to avoid keeping history.
git update-ref -d refs/heads/gh-pages
rm -rf *
mv ../book/* .
git add .
git commit -m "Deploy $GITHUB_SHA to gh-pages"
git push --force --set-upstream origin gh-pages