-- 0% read
阅读地图

    Notes & Logs 自动更新教程(GitHub Actions)

    2026-02-19 · #notes #workflow #github-actions #sitemap #automation

    这篇笔记记录我目前对 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. 触发策略说明

    1. push + paths:只在笔记 HTML 或索引脚本变更时触发,避免无关提交触发。
    2. schedule:每天北京时间 03:30 定时跑一次,作为兜底同步。
    3. workflow_dispatch:需要时可在 GitHub Actions 页面手动触发。

    4. 日常写作步骤(最实用)

    1. notes/ 新建一篇 *.html
    2. 写好 <title><meta name="note-date">
    3. 提交并 push 到 master
    4. 等待 Actions 自动更新 notes/notes-index.json

    5. 实际建议