首页Home 产品Products 服务Services 案例Work 知识库Blog 关于About 联系Contact
KNOWLEDGE · 2026-08

GitHub Actions 自动化内容流水线:从零搭建

GitHub Actions content pipeline from scratch

#GitHub Actions #自动化 #SEO #CI/CD

为什么需要自动化流水线

静态网站最大的敌人是「死掉」:内容不更新,搜索引擎爬虫来的次数越来越少, 访客看到三个月前的时间戳也会失去信任。解决方案是让内容自己保持新鲜—— 用 GitHub Actions 定时任务自动维护。

核心思路:三个自动化

  • 定时时间戳:每周自动更新站点的 last-updated 标记,提交回仓库,触发 Pages 重新部署。
  • IndexNow 通知:内容变更后立即 POST 到 Bing 的 IndexNow 接口,加速收录,不需要等爬虫自己来。
  • 海外 runner:本机在大陆网络环境,GitHub Actions 的 ubuntu-latest runner 在海外,天然绕过网络限制,可以跑本机跑不了的定时任务。

完整 workflow 配置

一个文件搞定,放在 .github/workflows/freshness.yml

name: Weekly content freshness

on:
  schedule:
    - cron: "0 3 * * 1"   # 每周一 03:00 UTC
  workflow_dispatch:

permissions:
  contents: write

jobs:
  update-timestamp:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          persist-credentials: true

      - name: Update last-updated marker
        run: |
          echo "Updated: $(date -u +'%Y-%m-%d %H:%M UTC')" > docs/.last-updated
          git config user.name "xuanjing-bot"
          git config user.email "bot@toolgen.xyz"
          git add docs/.last-updated
          if git diff --cached --quiet; then
            echo "no changes"
          else
            git commit -m "chore: weekly freshness update [skip ci]"
            git push
          fi

      - name: Ping Bing IndexNow
        run: |
          curl -s -m 30 -X POST "https://www.bing.com/indexnow"             -H "Content-Type: application/json"             -d '{"host":"toolgen.xyz","key":"YOUR_KEY","keyLocation":"https://toolgen.xyz/ix-key.txt","urlList":["https://toolgen.xyz/"]}'
          echo ""

关键细节与坑

  • persist-credentials:checkout 时默认不带写权限,必须显式开启,否则 push 会 403。
  • [skip ci] 提交信息:自动提交如果不带 skip-ci 标记,会触发新的 workflow 运行,形成无限循环。
  • 幂等提交:先 diff --cached --quiet 判断是否有变化,没变化就不 commit 不 push,避免无意义的构建。
  • IndexNow key 文件:key 必须同时放在服务器上(keyLocation),Bing 会验证。

验证与排错

用 API 手动触发 workflow 验证:POST /repos/{owner}/{repo}/actions/workflows/{file}/dispatches, 然后轮询 runs 接口看状态。我们实测从 queued 到 completed success 大约 30 秒。 如果失败,去 Actions 页面看日志——最常见的坑是 git push 需要配置 user.name/user.email。

效果

这套机制上线后:网站每周自动更新一次,Bing 收录从「待爬取」变成「已受理」, Google 也开始出现 site 搜索痕迹。自动化维护的意义不是省那几分钟, 而是让搜索引擎和访客都看到「这个站是活的」。