140 lines
4.0 KiB
YAML
140 lines
4.0 KiB
YAML
# .gitea/workflows/sync-wiki.yml
|
|
name: Sync solutions to Wiki
|
|
|
|
on:
|
|
push:
|
|
branches: [master]
|
|
paths:
|
|
- "solutions/**"
|
|
- ".gitea/workflows/sync-wiki.yml"
|
|
workflow_dispatch:
|
|
|
|
permissions:
|
|
contents: write
|
|
|
|
concurrency:
|
|
group: wiki-sync
|
|
cancel-in-progress: false
|
|
|
|
jobs:
|
|
sync:
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: Checkout
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Clone wiki repo
|
|
env:
|
|
WIKI_TOKEN: ${{ secrets.WIKI_TOKEN }}
|
|
SERVER_URL: ${{ github.server_url }}
|
|
run: |
|
|
set -euo pipefail
|
|
HOST="${SERVER_URL#https://}"
|
|
HOST="${HOST#http://}"
|
|
if ! git clone \
|
|
"https://oauth2:${WIKI_TOKEN}@${HOST}/${{ github.repository }}.wiki.git" \
|
|
wiki; then
|
|
echo "::error::Clone wiki failed. 检查: 1) wiki 是否已初始化 2) ENABLE_LOCAL_WIKI 是否开启 3) WIKI_TOKEN 是否有 write:repository 权限"
|
|
exit 1
|
|
fi
|
|
|
|
- name: Copy markdown files
|
|
shell: bash
|
|
run: |
|
|
set -euo pipefail
|
|
[ -d solutions ] || { echo "solutions/ not found"; exit 1; }
|
|
|
|
cd solutions
|
|
while IFS= read -r -d '' f; do
|
|
rel="${f#./}"
|
|
base="$(basename "$rel")"
|
|
|
|
# 不覆盖 wiki 的保留页面
|
|
case "$base" in
|
|
_Sidebar.md|_Footer.md|_Header.md) continue ;;
|
|
esac
|
|
|
|
# wiki 不支持子目录,把 a/b.md 展平为 a-b.md
|
|
target="../wiki/$(printf '%s' "$rel" | sed 's#/#-#g')"
|
|
cp "$f" "$target"
|
|
done < <(find . -type f -name '*.md' -print0)
|
|
|
|
cd ..
|
|
echo "---- wiki 目录内容 ----"
|
|
ls -la wiki
|
|
|
|
- name: Generate sidebar
|
|
shell: bash
|
|
run: |
|
|
set -euo pipefail
|
|
[ -d solutions ] || { echo "solutions/ not found"; exit 1; }
|
|
[ -d wiki ] || { echo "wiki/ not found"; exit 1; }
|
|
|
|
out=wiki/_Sidebar.md
|
|
|
|
# 收集 solutions 下所有 md(相对路径,去掉扩展名),按路径排序
|
|
mapfile -t items < <(
|
|
find solutions -type f -name '*.md' -printf '%P\n' \
|
|
| sed 's/\.md$//' \
|
|
| sort
|
|
)
|
|
|
|
{
|
|
echo "## 题解"
|
|
echo ""
|
|
echo "[🏠 返回仓库](${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY})"
|
|
echo ""
|
|
|
|
if [ "${#items[@]}" -eq 0 ]; then
|
|
echo "_暂无题解_"
|
|
else
|
|
current="__NONE__"
|
|
for rel in "${items[@]}"; do
|
|
base="$(basename "$rel")"
|
|
# 跳过 wiki 保留页面
|
|
case "$base" in
|
|
_Sidebar|_Footer|_Header) continue ;;
|
|
esac
|
|
|
|
dir="$(dirname "$rel")"
|
|
[[ "$dir" == "." ]] && dir=""
|
|
|
|
# 目录切换时输出分组标题
|
|
if [[ "$dir" != "$current" ]]; then
|
|
echo ""
|
|
if [[ -n "$dir" ]]; then
|
|
echo "### ${dir}"
|
|
else
|
|
echo "### 根目录"
|
|
fi
|
|
echo ""
|
|
current="$dir"
|
|
fi
|
|
|
|
# 与 Copy 步骤一致的展平规则:a/b -> a-b
|
|
flat="$(printf '%s' "$rel" | tr '/' '-')"
|
|
|
|
# Gitea wiki 链接语法:[[页面名|显示文本]]
|
|
echo "- [[${flat}|${base}]]"
|
|
done
|
|
fi
|
|
} > "$out"
|
|
|
|
echo "---- _Sidebar.md ----"
|
|
cat "$out"
|
|
|
|
- name: Commit & push
|
|
shell: bash
|
|
run: |
|
|
cd wiki
|
|
git config user.name "gitea-actions[bot]"
|
|
git config user.email "gitea-actions[bot]@noreply.${{ github.server_url }}"
|
|
|
|
git add -A
|
|
if git diff --cached --quiet; then
|
|
echo "No changes to sync."
|
|
exit 0
|
|
fi
|
|
|
|
git commit -m "Sync solutions from ${GITHUB_REPOSITORY}@${GITHUB_SHA:0:7}"
|
|
git push |