Files
DeepTutor/.github/workflows/docs.yml
AleFlyX 397b630f72 docs: add a deployment runbook and pre-commit checks (#3017)
* docs: document deployment and add maintenance checks

Add Cloudflare build, deployment, and rollback steps, check locale pairs and Markdown before commits, and report unused public asset candidates. Refs #2942.

* docs: add a Wrangler configuration example

部署手册说明如何从现有 Worker 补齐样板,并核对域名、路由和绑定。

验证:TOML 解析、文档源检查、6 项文档测试和变更文件的 pre-commit 检查通过。

* ci: check the live docs surface on public docs PRs

按路径筛选目标为 feat/public-docs 的 PR,程序或快照变更时校验真实 CLI/API,纯文案跳过运行时依赖。

验证:实测 CLI 与文案分流、命令改名时旧快照失败、重新导出后通过。6 项文档测试和变更文件的 pre-commit 检查通过。

* ci: add a local docs preflight and reuse the surface filters

新增本地检查入口和中文流程说明。允许主动刷新命令清单或构建站点,本地和 CI 共用路径规则,构建后单独校验产物链接。

验证:模拟 CLI 改名和纯文案改动,验证旧示例失败、修复后 6 项测试通过。正常构建通过,坏链接返回 1 并报告具体路径。变更文件的 pre-commit 检查通过。

* docs: keep the preflight instructions in the existing README

* docs: explain how to trigger the first fork preview
2026-12-01 07:46:32 -05: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 links
- 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 links
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.`,
});
}