更新目录结构和提交历史 #19
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: # 允许手动触发 | |
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; | |
printed_tree = 0; | |
buffer = ""; | |
} | |
# 当遇到目录结构标题时 | |
/^## 目录结构/ { | |
if (!printed_tree) { | |
print $0; | |
print "最后更新时间:" date; | |
print ""; | |
print "```"; | |
print tree; | |
print "```"; | |
printed_tree = 1; | |
in_tree = 1; | |
next; | |
} | |
} | |
# 当遇到下一个二级标题时 | |
/^## / && !/^## 目录结构/ { | |
if (in_tree) { | |
in_tree = 0; | |
} | |
if (!printed_tree) { | |
print "## 目录结构"; | |
print "最后更新时间:" date; | |
print ""; | |
print "```"; | |
print tree; | |
print "```"; | |
printed_tree = 1; | |
} | |
print $0; | |
next; | |
} | |
# 如果在目录结构部分,跳过内容 | |
in_tree { next } | |
# 输出其他内容 | |
{ print $0 } | |
# 如果到文件末尾还没打印目录结构,就在末尾打印 | |
END { | |
if (!printed_tree) { | |
print "## 目录结构"; | |
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 }} |