Files
DeepTutor/.github/workflows/docs.yml
AleFlyX cb737d4dca ci: watch the surface exporter in the docs workflow
The previous commit said the Docs workflow runs on changes to the surface
exporter, and the paths list didn't include it. So an edit to
scripts/export_docs_surface.py alone would have started no workflow at all:
tests.yml doesn't watch scripts/**, and nothing else does either.

Add the path to both the push and pull_request triggers so the claim holds.
2026-11-27 00:03:40 +08:00

467 lines
18 KiB
YAML

name: Docs
# Builds and verifies the public Astro/Starlight docs site.
#
# Previews on GitHub Pages (`gh-pages` branch):
# - Same-repo pull requests deploy an isolated preview at `pr-<number>/`,
# post/update a comment with the preview URL, and remove it when the PR closes.
# - Fork repositories (`repository.fork == true`) deploy branch previews on
# `push` (any branch except `gh-pages`) and `workflow_dispatch` at
# `branch-<slug>/` on the fork's own `gh-pages` branch.
#
# Repository setup (one-time, in upstream or in your fork):
# Settings -> Pages -> Build and deployment -> Deploy from a branch:
# branch `gh-pages`, folder `/ (root)`.
# Optional repository variable:
# DOCS_PREVIEW_SITE — custom origin (e.g. `https://docs.deeptutor.info`)
# if `gh-pages` serves a custom domain without a CNAME file.
on:
workflow_dispatch:
push:
branches-ignore:
- gh-pages
paths:
- "docs/**"
- "scripts/export_docs_surface.py"
- "deeptutor/modules/manifests/**"
- "deeptutor/api/router_index.json"
- "deeptutor/partners/channels/**"
- "deeptutor/partners/config/schema.py"
- "assets/figs/web-dev/**"
- ".github/workflows/docs.yml"
pull_request:
types: [opened, synchronize, reopened, closed]
paths:
- "docs/**"
- "scripts/export_docs_surface.py"
- "deeptutor/modules/manifests/**"
- "deeptutor/api/router_index.json"
- "deeptutor/partners/channels/**"
- "deeptutor/partners/config/schema.py"
- "assets/figs/web-dev/**"
- ".github/workflows/docs.yml"
permissions:
contents: read
jobs:
docs:
name: Build and check the docs site
if: >-
(github.event_name == 'pull_request' && github.event.action != 'closed') ||
github.event_name == 'workflow_dispatch' ||
(github.event_name == 'push' && (
github.event.repository.fork == true ||
github.ref_name == 'main' ||
github.ref_name == 'dev' ||
github.ref_name == 'preview'
))
runs-on: ubuntu-latest
concurrency:
group: docs-build-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
defaults:
run:
working-directory: docs
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Set up Node.js
uses: actions/setup-node@v6
with:
node-version: "22"
cache: npm
cache-dependency-path: docs/package-lock.json
- name: Check sources
working-directory: .
run: python3 docs/scripts/check_docs.py
- name: Set up Python for the contract tests
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Check pages against the exported CLI/API surface
working-directory: .
run: |
python -m pip install --upgrade pip
pip install pytest pytest-asyncio
pytest -q docs/tests
- name: Install dependencies
run: npm ci
env:
ASTRO_TELEMETRY_DISABLED: "1"
- name: Build
run: npm run build
env:
ASTRO_TELEMETRY_DISABLED: "1"
- name: Check built links
working-directory: .
run: python3 docs/scripts/check_docs.py dist
- name: Notify how to enable fork PR preview
if: >-
github.event_name == 'pull_request' &&
github.event.pull_request.head.repo.full_name != github.repository
working-directory: .
env:
FORK_FULL_NAME: ${{ github.event.pull_request.head.repo.full_name }}
FORK_OWNER: ${{ github.event.pull_request.head.repo.owner.login }}
FORK_REPO: ${{ github.event.pull_request.head.repo.name }}
HEAD_REF: ${{ github.event.pull_request.head.ref }}
run: |
set -euo pipefail
if [ -z "$FORK_FULL_NAME" ]; then
echo "::notice title=Fork PR Docs Preview::Docs preview deployment is skipped in the upstream repo for forked PRs. Enable GitHub Actions and GitHub Pages (branch gh-pages, folder / (root)) in your fork to deploy branch previews automatically."
exit 0
fi
owner_lower="$(printf '%s' "$FORK_OWNER" | tr '[:upper:]' '[:lower:]')"
branch_slug="$(printf '%s' "$HEAD_REF" | tr '[:upper:]' '[:lower:]' | sed -E 's/[^a-z0-9._-]+/-/g; s/\.{2,}/-/g; s/-{2,}/-/g; s/^[.-]+//; s/[.-]+$//')"
if [ -z "$branch_slug" ]; then
branch_slug="head"
fi
preview_dir="branch-${branch_slug}"
if [ "$(printf '%s' "$FORK_REPO" | tr '[:upper:]' '[:lower:]')" = "${owner_lower}.github.io" ]; then
preview_url="https://${owner_lower}.github.io/${preview_dir}/"
else
preview_url="https://${owner_lower}.github.io/${FORK_REPO}/${preview_dir}/"
fi
pages_settings_url="https://github.com/${FORK_FULL_NAME}/settings/pages"
actions_url="https://github.com/${FORK_FULL_NAME}/actions/workflows/docs.yml"
echo "::notice title=Fork PR Docs Preview::PR preview deployment is skipped in the upstream repo for forked PRs. To enable live docs previews, enable GitHub Actions and GitHub Pages (branch gh-pages, folder / (root)) in your fork (${pages_settings_url}). Pushing to '${HEAD_REF}' will publish to ${preview_url}"
{
echo "### Docs Preview for Forked PRs"
echo ""
echo "Automatic preview deployment in the upstream repository is skipped for PRs from forks."
echo "To enable a live docs preview for this PR, configure your fork \`${FORK_FULL_NAME}\`:"
echo ""
echo "1. Enable GitHub Actions in your fork ([Docs workflow](${actions_url}))."
echo "2. Enable GitHub Pages in [Settings -> Pages](${pages_settings_url}): **Deploy from a branch**, branch \`gh-pages\`, folder \`/ (root)\`."
echo "3. Push to \`${HEAD_REF}\` (or run the Docs workflow manually via \`workflow_dispatch\`)."
echo ""
echo "Once published, your preview will be available at:"
echo "- **English**: ${preview_url}"
echo "- **简体中文**: ${preview_url}zh-cn/"
} >> "$GITHUB_STEP_SUMMARY"
preview:
name: Deploy preview to GitHub Pages
needs: docs
if: >-
(github.event_name == 'pull_request' &&
github.event.action != 'closed' &&
github.event.pull_request.head.repo.full_name == github.repository) ||
((github.event_name == 'push' || github.event_name == 'workflow_dispatch') &&
github.event.repository.fork == true &&
github.ref_name != 'gh-pages')
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
concurrency:
group: docs-gh-pages
cancel-in-progress: false
defaults:
run:
working-directory: docs
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Set up Node.js
uses: actions/setup-node@v6
with:
node-version: "22"
cache: npm
cache-dependency-path: docs/package-lock.json
- name: Resolve preview URL and base path
id: meta
working-directory: .
env:
EVENT_NAME: ${{ github.event_name }}
PR_NUMBER: ${{ github.event.pull_request.number }}
REF_NAME: ${{ github.ref_name }}
REPO_OWNER: ${{ github.repository_owner }}
REPO_NAME: ${{ github.event.repository.name }}
CUSTOM_SITE: ${{ vars.DOCS_PREVIEW_SITE }}
run: |
set -euo pipefail
owner_lower="$(printf '%s' "$REPO_OWNER" | tr '[:upper:]' '[:lower:]')"
if [ "$EVENT_NAME" = "pull_request" ]; then
preview_dir="pr-${PR_NUMBER}"
preview_label="PR #${PR_NUMBER}"
else
branch_slug="$(printf '%s' "$REF_NAME" | tr '[:upper:]' '[:lower:]' | sed -E 's/[^a-z0-9._-]+/-/g; s/\.{2,}/-/g; s/-{2,}/-/g; s/^[.-]+//; s/[.-]+$//')"
if [ -z "$branch_slug" ]; then
branch_slug="head"
fi
preview_dir="branch-${branch_slug}"
preview_label="branch ${REF_NAME}"
fi
cname=""
if git fetch --depth=1 origin gh-pages 2>/dev/null; then
if git cat-file -e FETCH_HEAD:CNAME 2>/dev/null; then
cname="$(git show FETCH_HEAD:CNAME | tr -d '[:space:]')"
fi
fi
if [ -n "$CUSTOM_SITE" ]; then
docs_site="${CUSTOM_SITE%/}"
docs_base="/${preview_dir}"
elif [ -n "$cname" ]; then
docs_site="https://${cname}"
docs_base="/${preview_dir}"
elif [ "$(printf '%s' "$REPO_NAME" | tr '[:upper:]' '[:lower:]')" = "${owner_lower}.github.io" ]; then
docs_site="https://${owner_lower}.github.io"
docs_base="/${preview_dir}"
else
docs_site="https://${owner_lower}.github.io"
docs_base="/${REPO_NAME}/${preview_dir}"
fi
preview_url="${docs_site}${docs_base}/"
{
echo "docs_site=${docs_site}"
echo "docs_base=${docs_base}"
echo "preview_dir=${preview_dir}"
echo "preview_label=${preview_label}"
echo "preview_url=${preview_url}"
} >> "$GITHUB_OUTPUT"
- name: Install dependencies
run: npm ci
env:
ASTRO_TELEMETRY_DISABLED: "1"
- name: Build preview site
run: npm run build
env:
ASTRO_TELEMETRY_DISABLED: "1"
DOCS_SITE: ${{ steps.meta.outputs.docs_site }}
DOCS_BASE: ${{ steps.meta.outputs.docs_base }}
DOCS_NOINDEX: "1"
- name: Check preview links
working-directory: .
run: python3 docs/scripts/check_docs.py dist
env:
DOCS_BASE: ${{ steps.meta.outputs.docs_base }}
- name: Publish preview to gh-pages
working-directory: .
env:
EVENT_NAME: ${{ github.event_name }}
REPO_FULL_NAME: ${{ github.repository }}
HEAD_SHA: ${{ github.event.pull_request.head.sha || github.sha }}
PREVIEW_DIR: ${{ steps.meta.outputs.preview_dir }}
PREVIEW_LABEL: ${{ steps.meta.outputs.preview_label }}
PREVIEW_URL: ${{ steps.meta.outputs.preview_url }}
run: |
set -euo pipefail
worktree="$(mktemp -d)"
trap 'git worktree remove --force "$worktree" 2>/dev/null || rm -rf "$worktree"' EXIT
if git ls-remote --exit-code --heads origin gh-pages >/dev/null 2>&1; then
git fetch --depth=1 origin gh-pages
git worktree add --detach "$worktree" FETCH_HEAD
else
git worktree add --orphan -b gh-pages "$worktree"
fi
touch "${worktree}/.nojekyll"
rm -rf "${worktree:?}/${PREVIEW_DIR}"
mkdir -p "${worktree}/${PREVIEW_DIR}"
cp -R docs/dist/. "${worktree}/${PREVIEW_DIR}/"
git -C "$worktree" config user.name "github-actions[bot]"
git -C "$worktree" config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git -C "$worktree" add --all -- ".nojekyll" "${PREVIEW_DIR}"
if git -C "$worktree" diff --cached --quiet; then
echo "Preview content unchanged; skipping push."
else
short_sha="${HEAD_SHA:0:7}"
git -C "$worktree" commit -m "chore(docs): update preview for ${PREVIEW_LABEL} (${short_sha})"
for attempt in 1 2 3; do
if git -C "$worktree" push origin HEAD:refs/heads/gh-pages; then
break
fi
if [ "$attempt" -eq 3 ]; then
exit 1
fi
git -C "$worktree" fetch --depth=1 origin gh-pages
git -C "$worktree" rebase FETCH_HEAD
done
fi
{
echo "### Docs Preview (${PREVIEW_LABEL})"
echo ""
echo "- English: ${PREVIEW_URL}"
echo "- 简体中文: ${PREVIEW_URL}zh-cn/"
echo "- Commit: \`${HEAD_SHA:0:7}\`"
} >> "$GITHUB_STEP_SUMMARY"
if [ "$EVENT_NAME" != "pull_request" ]; then
pages_settings_url="https://github.com/${REPO_FULL_NAME}/settings/pages"
echo "::notice title=Fork Docs Preview Published::Preview pushed to gh-pages (${PREVIEW_URL}). If this is your first time, enable GitHub Pages (branch gh-pages, folder / (root)) at ${pages_settings_url}."
{
echo ""
echo "> **Note**: If the link returns 404, enable GitHub Pages for this fork at [Settings -> Pages](${pages_settings_url}) (**Deploy from a branch** -> \`gh-pages\` -> \`/ (root)\`)."
} >> "$GITHUB_STEP_SUMMARY"
fi
- name: Comment preview URL on PR
if: github.event_name == 'pull_request'
uses: actions/github-script@v8
env:
PREVIEW_URL: ${{ steps.meta.outputs.preview_url }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
with:
script: |
const marker = "<!-- deeptutor-docs-preview -->";
const previewUrl = process.env.PREVIEW_URL;
const shortSha = (process.env.HEAD_SHA || "").slice(0, 7);
const body = [
marker,
"### Docs Preview",
"",
`- **English**: ${previewUrl}`,
`- **简体中文**: ${previewUrl}zh-cn/`,
"",
`Built from commit \`${shortSha}\`. GitHub Pages may take a minute to publish after this push. The preview directory is removed automatically when this PR closes.`,
].join("\n");
const { owner, repo } = context.repo;
const issue_number = context.issue.number;
const comments = await github.paginate(github.rest.issues.listComments, {
owner,
repo,
issue_number,
per_page: 100,
});
const existing = comments.find(
(comment) =>
comment.user?.type === "Bot" &&
typeof comment.body === "string" &&
comment.body.includes(marker),
);
if (existing) {
await github.rest.issues.updateComment({
owner,
repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner,
repo,
issue_number,
body,
});
}
cleanup-preview:
name: Clean up PR preview from GitHub Pages
if: >-
github.event_name == 'pull_request' &&
github.event.action == 'closed' &&
github.event.pull_request.head.repo.full_name == github.repository
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
concurrency:
group: docs-gh-pages
cancel-in-progress: false
steps:
- name: Checkout repository
uses: actions/checkout@v6
with:
fetch-depth: 1
- name: Remove preview directory from gh-pages
id: cleanup
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
set -euo pipefail
preview_dir="pr-${PR_NUMBER}"
if ! git ls-remote --exit-code --heads origin gh-pages >/dev/null 2>&1; then
echo "removed=false" >> "$GITHUB_OUTPUT"
exit 0
fi
git fetch --depth=1 origin gh-pages
worktree="$(mktemp -d)"
trap 'git worktree remove --force "$worktree" 2>/dev/null || rm -rf "$worktree"' EXIT
git worktree add --detach "$worktree" FETCH_HEAD
if [ ! -d "${worktree}/${preview_dir}" ]; then
echo "removed=false" >> "$GITHUB_OUTPUT"
exit 0
fi
rm -rf "${worktree:?}/${preview_dir}"
git -C "$worktree" config user.name "github-actions[bot]"
git -C "$worktree" config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git -C "$worktree" add --all -- "${preview_dir}"
git -C "$worktree" commit -m "chore(docs): remove preview for PR #${PR_NUMBER}"
for attempt in 1 2 3; do
if git -C "$worktree" push origin HEAD:refs/heads/gh-pages; then
break
fi
if [ "$attempt" -eq 3 ]; then
exit 1
fi
git -C "$worktree" fetch --depth=1 origin gh-pages
git -C "$worktree" rebase FETCH_HEAD
done
echo "removed=true" >> "$GITHUB_OUTPUT"
- name: Update preview comment on PR
if: steps.cleanup.outputs.removed == 'true'
uses: actions/github-script@v8
with:
script: |
const marker = "<!-- deeptutor-docs-preview -->";
const { owner, repo } = context.repo;
const issue_number = context.issue.number;
const comments = await github.paginate(github.rest.issues.listComments, {
owner,
repo,
issue_number,
per_page: 100,
});
const existing = comments.find(
(comment) =>
comment.user?.type === "Bot" &&
typeof comment.body === "string" &&
comment.body.includes(marker),
);
if (existing) {
await github.rest.issues.updateComment({
owner,
repo,
comment_id: existing.id,
body: `${marker}\n### Docs Preview\n\nThis PR is closed, so the GitHub Pages preview has been removed.`,
});
}