使用 Hugo + GitHub Actions 自动生成数据,并通过 Cloudflare Pages 自动部署(含部署钩子)
静态站点生成器 Hugo 与 Cloudflare Pages 非常适配,但当你用 GitHub Actions 自动生成内容(比如数据文件)时,会遇到一个常见问题 —— Cloudflare Pages 跳过部署,显示「无可用部署」。
本文手把手教你如何解决这个问题,通过添加 部署钩子(Deploy Hook)让 Cloudflare 在 GitHub Actions 执行完后自动部署。
一问题场景
你使用 GitHub Actions 定时更新 Hugo 网站的数据(如每日热榜),然后推送到 GitHub 仓库。
但 Cloudflare Pages 显示:
「跳过部署」「无可用部署」
原因:
Cloudflare Pages 只会自动部署 main 分支的代码变更,而你在 GitHub Actions 中提交数据时添加了 [skip ci]
,Cloudflare 认为这次提交无需部署。
二解决方案总览
我们手动触发部署,让 Cloudflare 每次 Action 成功后强制部署。方法如下:
✅ 添加 Cloudflare Deploy Hook (部署挂钩)
三详细操作步骤
1. 在 Cloudflare Pages 添加「部署挂钩」
- 进入 Cloudflare Pages 项目控制台。
- 找到 “部署挂钩”(在中文界面中,直接搜索这个关键词)。
- 点击 “创建部署挂钩”。
- 输入名称,比如:
github-action-trigger
。 - 生成后,复制 Webhook URL,格式如下:
https://api.cloudflare.com/client/v4/pages/webhooks/deploy/xxxxxxxxxxxxxxxxxxxx
2. 添加 GitHub Secrets
-
进入 GitHub 仓库设置 > Secrets and variables > Actions。
-
添加新 Secret:
- 名称:
CF_PAGES_DEPLOY_HOOK
- 值:上一步复制的 Webhook URL
- 名称:
3. 修改 GitHub Actions 文件
name: Update Hot List
permissions:
contents: write
on:
schedule:
- cron: Ɔ */6 * * *' # 每6小时运行
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: Ƈ.22'
- name: Run Scraper
run: go run scripts/fetch_trending.go
- name: Commit & Push
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add data/
git diff --cached --quiet || git commit -m "Update trending data [skip ci]"
git push
- name: Deploy to Cloudflare Pages via Webhook
run: curl -X POST "${{ secrets.CF_PAGES_DEPLOY_HOOK }}"
四构建配置 (Cloudflare Pages)
确保你的 Cloudflare Pages 构建配置正确:
- 构建命令:
hugo --minify
- 输出目录:
public
- 生产分支:
main
- 构建系统版本:3
- 可选:启用缓存,提高构建速度。
五总结
通过 GitHub Actions 自动更新 Hugo 数据后,使用 Cloudflare Deploy Hook 可强制部署,即便你使用了 [skip ci]
。
这个方法特别适合:
- 新闻聚合站
- 热榜类网站
- 定时爬虫生成页面的网站
延伸阅读
- Hugo 官网:https://gohugo.io/
- Cloudflare Pages:https://pages.cloudflare.com/
- GitHub Actions 文档:https://docs.github.com/actions
本文作者: 永生
本文链接: https://yys.zone/detail/?id=447
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
评论列表 (0 条评论)