name: Cargo Docs
on:
push:
branches:
- main
workflow_dispatch:
permissions:
contents: write
concurrency:
group: cargo-docs-branch
cancel-in-progress: true
jobs:
publish-docs:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
- name: Cache Cargo artifacts
uses: Swatinem/rust-cache@v2
- name: Build Cargo documentation
run: cargo doc --workspace --all-features --no-deps
- name: Prepare documentation site
run: |
rm -rf site
mkdir -p site
cp -R target/doc/. site/
touch site/.nojekyll
cat > site/index.html <<'EOF'
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="refresh" content="0; url=./spider_lib/index.html" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>spider-lib docs</title>
</head>
<body>
<p>Redirecting to <a href="./spider_lib/index.html">spider-lib documentation</a>...</p>
</body>
</html>
EOF
- name: Publish docs to gh-pages branch
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set -eu
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git fetch origin gh-pages || true
if git show-ref --verify --quiet refs/remotes/origin/gh-pages; then
git worktree add --force ../gh-pages origin/gh-pages
else
git worktree add --force --detach ../gh-pages
cd ../gh-pages
git checkout --orphan gh-pages
git rm -rf . >/dev/null 2>&1 || true
cd "$GITHUB_WORKSPACE"
fi
find ../gh-pages -mindepth 1 -maxdepth 1 \
! -name '.git' \
! -name '.github' \
-exec rm -rf {} +
cp -R site/. ../gh-pages/
cd ../gh-pages
git add -A
if git diff --cached --quiet; then
echo "No documentation changes to commit."
exit 0
fi
git commit -m "docs: update cargo docs [skip ci]"
git push "https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" HEAD:gh-pages