Notes & Logs 自动更新教程(GitHub Actions)
这篇笔记记录我目前对 notes-index.json 的自动维护方案。目标很直接:只要我在
notes/ 下新增或修改文章,首页与 notes/index.html 的列表就能自动同步,不再手动执行脚本。
1. 工作流文件位置
在仓库中新增文件:
.github/workflows/update-notes-index.yml
2. 完整 YAML 示例
name: Update Notes Index
on:
push:
branches:
- master
paths:
- "notes/**/*.html"
- "tools/build-notes-index.mjs"
- ".github/workflows/update-notes-index.yml"
- "!notes/index.html"
- "!notes/_template.html"
schedule:
# UTC 19:30 = Beijing Time (UTC+8) 03:30 (next day)
- cron: "30 19 * * *"
workflow_dispatch:
permissions:
contents: write
jobs:
build-notes-index:
if: github.actor != 'github-actions[bot]'
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
- name: Rebuild notes index
run: node tools/build-notes-index.mjs
- name: Commit and push if changed
run: |
if git diff --quiet -- notes/notes-index.json; then
echo "No changes in notes/notes-index.json"
exit 0
fi
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add notes/notes-index.json
git commit -m "Auto-update notes index"
git push
3. 触发策略说明
push + paths:只在笔记 HTML 或索引脚本变更时触发,避免无关提交触发。schedule:每天北京时间 03:30 定时跑一次,作为兜底同步。workflow_dispatch:需要时可在 GitHub Actions 页面手动触发。
4. 日常写作步骤(最实用)
- 在
notes/新建一篇*.html。 - 写好
<title>和<meta name="note-date">。 - 提交并 push 到
master。 - 等待 Actions 自动更新
notes/notes-index.json。
5. 实际建议
- 文件名建议带日期前缀,后续检索更清晰。
- 文章只负责内容,索引交给脚本生成,维护成本最低。
- 如果当天没更新,定时任务也会兜底把索引修正。