更新目录结构和提交历史 #12
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 }} # 使用个人访问令牌 | |
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 | |
{ | |
# 如果文件存在且包含目录结构部分,则保留其他部分 | |
if [ -f README.md ] && grep -q "## 目录结构" README.md; then | |
# 提取目录结构部分之前的内容 | |
sed -n '1,/## 目录结构/p' README.md | head -n -1 | |
# 添加目录结构标题和更新时间 | |
echo "## 目录结构" | |
echo "最后更新时间:$(date '+%Y年%m月%d日 %H:%M:%S')" | |
echo "" | |
echo "\`\`\`" | |
echo "$TREE_OUTPUT" | |
echo "\`\`\`" | |
# 提取目录结构部分之后的内容(如果有) | |
sed -n '/^```$/,$ p' README.md | tail -n +2 | |
else | |
# 如果文件不存在或没有目录结构部分,创建新的内容 | |
echo "# 个人笔记" | |
echo "" | |
echo "## 目录结构" | |
echo "最后更新时间:$(date '+%Y年%m月%d日 %H:%M:%S')" | |
echo "" | |
echo "\`\`\`" | |
echo "$TREE_OUTPUT" | |
echo "\`\`\`" | |
fi | |
} > "$TEMP_FILE" | |
# 替换原文件 | |
mv "$TEMP_FILE" README.md | |
- name: Create Pull Request | |
id: cpr | |
uses: peter-evans/create-pull-request@v5 | |
with: | |
token: ${{ secrets.PAT_TOKEN }} | |
commit-message: "docs: 更新目录结构" | |
title: "docs: 自动更新目录结构" | |
body: | | |
自动更新 README.md 中的目录结构 | |
- 更新时间:$(date '+%Y年%m月%d日 %H:%M:%S') | |
- 由 GitHub Actions 自动创建 | |
branch: directory-tree-updates | |
delete-branch: true | |
base: master | |
- name: Enable Pull Request Automerge | |
if: steps.cpr.outputs.pull-request-operation == 'created' | |
run: | | |
gh auth login --with-token <<< "${{ secrets.PAT_TOKEN }}" | |
gh pr merge --auto --merge "${{ steps.cpr.outputs.pull-request-number }}" |