feat: ✨ 添加目录结构和Markdown批量生成目录 #28
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: 更新目录结构 | |
on: | |
schedule: | |
- cron: '0 0 * * *' # 每天 UTC 00:00 运行 | |
workflow_dispatch: # 允许手动触发 | |
push: | |
branches: | |
- master # 监听 master 分支上的推送事件 | |
jobs: | |
update-directory-tree: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
pull-requests: write | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
with: | |
token: ${{ secrets.PAT_TOKEN }} # 使用 PAT 进行 checkout | |
fetch-depth: 1 | |
- name: Setup Git | |
run: | | |
git config --global user.email "github-actions[bot]@users.noreply.github.com" | |
git config --global user.name "github-actions[bot]" | |
- name: Install tree | |
run: sudo apt-get install -y tree | |
- name: Update Directory Structure | |
run: | | |
# 生成目录结构 | |
TREE_OUTPUT=$(tree -L 2 -I '.git|.github|node_modules' --charset=utf8) | |
# 创建临时文件 | |
TEMP_FILE=$(mktemp) | |
# 处理 README.md | |
awk -v tree="$TREE_OUTPUT" -v date="$(TZ='Asia/Shanghai' date '+%Y年%m月%d日 %H:%M:%S')" ' | |
BEGIN { | |
in_tree = 0; | |
found_tree = 0; | |
} | |
# 当找到目录结构标题时 | |
/^## 目录结构/ { | |
if (!found_tree) { | |
print $0; # 打印标题 | |
print "最后更新时间:" date; | |
print ""; | |
print "```"; | |
print tree; | |
print "```"; | |
found_tree = 1; | |
in_tree = 1; | |
next; | |
} | |
} | |
# 当在目录结构部分时,跳过直到找到下一个标题或文件结束 | |
in_tree && /^##/ { | |
in_tree = 0; | |
print $0; | |
next; | |
} | |
# 跳过目录结构部分的内容 | |
in_tree { next } | |
# 打印其他所有内容 | |
{ print $0 } | |
# 如果没有找到目录结构部分,在文件末尾添加 | |
END { | |
if (!found_tree) { | |
print "\n## 目录结构"; | |
print "最后更新时间:" date; | |
print ""; | |
print "```"; | |
print tree; | |
print "```"; | |
} | |
} | |
' README.md > "$TEMP_FILE" | |
# 替换原文件 | |
mv "$TEMP_FILE" README.md | |
- name: Create Pull Request | |
run: | | |
# 检查是否有更改 | |
if [[ -n "$(git status --porcelain)" ]]; then | |
# 获取当前时间作为分支名 | |
BRANCH_NAME="directory-tree-updates-$(TZ='Asia/Shanghai' date +%Y%m%d-%H%M%S)" | |
# 创建新分支 | |
git checkout -b "$BRANCH_NAME" | |
# 提交更改 | |
git add README.md | |
git commit -m "docs: 更新目录结构 | |
- 更新时间:$(TZ='Asia/Shanghai' date '+%Y年%m月%d日 %H:%M:%S') | |
- 由 GitHub Actions 自动创建" | |
# 推送到远程 | |
git push origin "$BRANCH_NAME" | |
# 创建 PR | |
gh pr create \ | |
--title "docs: 自动更新目录结构" \ | |
--body "自动更新 README.md 中的目录结构 | |
- 更新时间:$(TZ='Asia/Shanghai' date '+%Y年%m月%d日 %H:%M:%S') | |
- 由 GitHub Actions 自动创建" \ | |
--base master \ | |
--head "$BRANCH_NAME" | |
else | |
echo "No changes to commit" | |
fi | |
env: | |
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }} |