更新目录结构和提交历史 #16
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: | |
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 } | |
/^## 目录结构/ { | |
if (!printed_tree) { | |
print $0; | |
print "最后更新时间:" date; | |
print ""; | |
print "```"; | |
print tree; | |
print "```"; | |
printed_tree = 1; | |
in_tree = 1; | |
next; | |
} | |
} | |
in_tree && /^```/ { in_tree = 0; next } | |
!in_tree { print $0 } | |
' README.md > "$TEMP_FILE" | |
# 替换原文件 | |
mv "$TEMP_FILE" README.md | |
- name: Create Pull Request | |
id: cpr | |
uses: peter-evans/create-pull-request@v5 | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
commit-message: "docs: 更新目录结构" | |
title: "docs: 自动更新目录结构" | |
body: | | |
自动更新 README.md 中的目录结构 | |
- 更新时间:$(TZ='Asia/Shanghai' date '+%Y年%m月%d日 %H:%M:%S') | |
- 由 GitHub Actions 自动创建 | |
branch: directory-tree-updates | |
delete-branch: true | |
base: master | |
- name: Merge Pull Request | |
if: steps.cpr.outputs.pull-request-operation == 'created' | |
env: | |
GH_TOKEN: ${{ secrets.PAT_TOKEN }} | |
run: | | |
# 等待几秒钟确保 PR 已经完全创建 | |
sleep 5 | |
# 使用你的 PAT 合并 PR | |
gh pr merge ${{ steps.cpr.outputs.pull-request-number }} --merge --delete-branch |