hugo-theme-dooz-social-preview

Hugo 源码放在私有仓库,每写完一篇就得本地构建、打包,再想办法把 public/ 传到公网——更新越频繁,这套重复动作越磨人。

这套方案解决的就是这个问题:私有仓库更新后,如何一键部署到公网。把构建和发布交给 GitHub Actions,push 源码后自动生成静态页并发布到 yourname.github.io,不用本地构建,也不用碰服务器。

下面从零配置一次;之后每次更新,只需要 git push。

一、最终结构

mermaid
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
flowchart LR
  A["私有仓库<br/>my-hugo-blog"] -->|git push| B["GitHub Actions<br/>hugo build"]
  B --> C["public/<br/>静态文件"]
  C -->|Deploy Key| D["公开仓库<br/>yourname.github.io"]
  D --> E["GitHub Pages<br/>yourname.github.io"]

  classDef source fill:#F3E8FF,stroke:#7C3AED,stroke-width:1.5px,color:#3B0764
  classDef ci fill:#FFF7ED,stroke:#F59E0B,stroke-width:1.5px,color:#7C2D12
  classDef build fill:#ECFDF5,stroke:#10B981,stroke-width:1.5px,color:#064E3B
  classDef publish fill:#E0F2FE,stroke:#0EA5E9,stroke-width:1.5px,color:#0C4A6E
  classDef page fill:#FDF2F8,stroke:#EC4899,stroke-width:1.5px,color:#831843

  class A source
  class B ci
  class C build
  class D publish
  class E page

两个仓库职责分开:

  • 私有仓库:文章、配置、主题、草稿
  • 公开仓库:只放 public/ 生成结果,供 Pages 读取

不要把生成目录塞回源码仓库,也不要把它做成 submodule。产物和源码分开,后续维护最简单。

二、准备仓库

下文用 yourname 表示你的 GitHub 用户名,my-hugo-blog 表示私有源码仓库名;实际操作时换成自己的即可。

1. 源码仓库

yourname/my-hugo-blog,私有即可,Hugo 项目和主题子模块都放在这里。

2. 发布仓库

新建公开仓库,名字必须是:

text
1
yourname/yourname.github.io

GitHub 的用户站靠这个命名识别根域名。仓库里的旧内容会被首次发布覆盖;工作流里的 force_orphan 会让它每次只保留一个提交。

发布成功后,在仓库 Settings → Pages 设置:

  • Source:Deploy from a branch
  • Branch:master
  • 目录:/ (root)

如果是全新空仓库,先等第一次 Actions 创建 master,再回来设置。

三、配置 Deploy Key

Actions 自带的 GITHUB_TOKEN 只能操作当前仓库,跨仓库推送要用 Deploy Key。

生成一对专用密钥:

bash
1
2
ssh-keygen -t ed25519 -C "my-blog-actions" \
  -f ~/.ssh/blog_pages_deploy -N ""

不要设置密码,CI 无法交互输入。

复制公钥和私钥:

bash
1
2
pbcopy < ~/.ssh/blog_pages_deploy.pub
pbcopy < ~/.ssh/blog_pages_deploy

分别配置:

  1. 公钥 → yourname/yourname.github.io → Settings → Deploy keys → Add deploy key,勾选 Allow write access
  2. 私钥 → yourname/my-hugo-blog → Settings → Secrets and variables → Actions → New repository secret

Secret 名称固定为:

text
1
ACTIONS_DEPLOY_KEY

私钥只保存在本地和 GitHub Secret 中,不要提交到仓库。

四、添加工作流

在源码仓库新建:

text
1
.github/workflows/deploy-pages.yml

内容:

yaml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
name: Deploy to GitHub Pages

on:
  push:
    branches: [master]
  workflow_dispatch:

permissions:
  contents: read

concurrency:
  group: pages-deploy
  cancel-in-progress: false

jobs:
  deploy:
    runs-on: ubuntu-latest
    timeout-minutes: 15

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          submodules: false

      - name: Rewrite submodule URLs to HTTPS
        run: |
          git config --global url."https://github.com/".insteadOf "git@github.com:"
          git config --global url."https://gitee.com/".insteadOf "git@gitee.com:"

      - name: Init submodules
        run: git submodule update --init --recursive

      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v3
        with:
          hugo-version: "0.169.0"
          extended: false

      - name: Build site
        run: |
          hugo --gc --minify --baseURL "https://yourname.github.io/"
          touch public/.nojekyll

      - name: Deploy to yourname.github.io
        uses: peaceiris/actions-gh-pages@v4
        with:
          deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }}
          external_repository: yourname/yourname.github.io
          publish_branch: master
          publish_dir: ./public
          force_orphan: true
          user_name: github-actions[bot]
          user_email: 41898282+github-actions[bot]@users.noreply.github.com

需要按自己的情况改的只有三处:

位置改成
external_repository你的用户名/你的用户名.github.io
--baseURLhttps://你的用户名.github.io/
hugo-version你本地使用的 Hugo 版本

其余保持默认即可。

关键点:

  • 子模块地址多为 SSH 形式,CI 没有你的本地密钥,所以先改写成 HTTPS;公开仓库可以直接读取。如果子模块是私有仓库,需要另外给 CI 配读权限。
  • baseURL 末尾必须有 /,否则生成的链接会拼接错误。
  • touch public/.nojekyll 关闭 GitHub Pages 的 Jekyll 处理。
  • force_orphan 让发布仓库每次只保留一个提交,适合纯产物仓库。

五、第一次发布

提交并推送工作流:

bash
1
2
3
git add .github/workflows/deploy-pages.yml
git commit -m "ci: deploy site to GitHub Pages"
git push origin master

打开源码仓库的 Actions 页面,等待 Deploy to GitHub Pages 变绿。正常流程是:

  1. 拉取源码与子模块
  2. 安装 Hugo
  3. 构建 public/
  4. 推送目标仓库
  5. Pages 自动重建

大约 1–2 分钟。成功后访问:

text
1
https://yourname.github.io

如果页面没更新,依次确认:

  • 目标仓库 master 是否有新提交
  • Pages 的 Source 是否为 master / / (root)
  • Actions 日志停在哪个步骤

需要重新发布时,也可以在 Actions 页面点 Run workflow 手动触发。

六、日常写作流程

之后每次发布只有三步:

bash
1
2
3
git add .
git commit -m "post: 新文章"
git push

等 CI 跑完,站点自动更新。草稿不会进入构建,本地预览仍然用:

bash
1
hugo server

或项目里的:

bash
1
make run

七、上线检查清单

首次发布前确认:

  • 发布仓库名为 用户名.github.io,且是公开仓库
  • Deploy Key 已添加,并勾选 Allow write access
  • Secret 名称为 ACTIONS_DEPLOY_KEY
  • 工作流在 master 分支
  • baseURL 与最终域名一致,末尾带 /
  • Pages 的 Source 为 master / / (root)

全部确认后,这套流程就可以长期使用。公开站点的 Pages 托管免费;私有源码仓库的 Actions 使用每月免费额度,普通博客更新完全够用。


「真诚赞赏,手留余香」

Hollson码夫庄园

嗯,我的梦想是被读者的稿费包养~

使用微信扫描二维码完成支付

Hollson

评论