部署
本页讲解博客的发布与部署:静态产物如何托管、Vercel 生产部署与测试域流程、Docker 构建四模式,以及"改代码 vs 改内容"的发布选择。部署链路依赖 Git 子模块(themes/matery/ 等),提交时需双仓库一起提交(见第 6 节)。
1. 静态产物部署(任意静态托管)
用途:Hexo 生成的是纯静态文件,public/ 目录可以丢到任何静态托管(Nginx、GitHub Pages、Cloudflare Pages、OSS 等)直接上线。
npm run build # hexo generate,产物在 public/
npm run clean # hexo clean,清 public/ 与缓存- 部署即同步
public/:拷到 Web 服务器根目录即可;域名指向哪里由托管平台决定 - 根 URL 必须一致:
_config.yml的url决定 canonical/sitemap/RSS 里的绝对链接,换托管域名要同步改(见「全局配置」篇) - 本项目生产用的是 Vercel(见下节),GitHub Pages 旧部署已停用
2. Vercel 生产部署
用途:生产环境 https://blog.17lai.site。Vercel 关联 GitHub 仓库 github.com:appotry/hexo.git(main 分支),push 后自动同步构建,无需手动触发。
# 推送到 hexo.git(生产仓库)
tools/cicd.sh -r "特性说明" # 构建 + 版本递增 + git tag + push- 构建在 Vercel 云端执行(
npm run build),产物直接上 CDN - 不要在生产仓库外另建部署仓库;测试用独立的
hexoback.git(下节) - 发布后按发版铁律跑
tools/tests/release-test.sh验证生产环境(20 页面 + 评论区 + 版本一致性)
3. 测试域流程(hexoback → blog2.17lai.site)
用途:正式发布前先在测试域验证,避免坏改动直接上生产。Vercel 上有第二个项目监听 hexoback.git(测试仓库),部署到 https://blog2.17lai.site。
# 推测试仓库(Vercel 自动部署到测试域)
tools/cicd.sh -t "说明" # 推测试仓库(Vercel 部署测试域)
# 在测试域跑 PWA 冒烟 + 交互验证
make test-pwa # 7 页 SW 冒烟 + 交互验证测试通过后才推生产:
tools/cicd.sh -r "特性" # 正式发布(递增版本号 + CDN + git tag)
tools/tests/release-test.sh # 【发版铁律】全量 layout 自动化测试两仓库完全分开,测试不污染生产。小改动(仅内容)可跳过测试域直接 -c 推送。
4. Docker 构建(tools/cicd.sh 四模式)
用途:tools/cicd.sh 是完整的 CI/CD 入口(代理 → 版本管理 → 子仓库同步 → 配置注入 → 构建 → PWA 版本 → 部署推送),支持四种模式:
| 参数 | 用途 | 版本处理 | 压缩 | CDN |
|---|---|---|---|---|
-d / --debug | 本地调试 | 不递增 | 关闭 | 清空 |
-c "说明" / --continue | 持续集成(仅更新文章) | 使用旧版本 | 开启 | 置空 |
-r "特性" / --release | 正式发布 | 递增 + git tag | 开启 | jsdelivr |
-t / --test | 本地编译测试 | 使用旧版本 | 开启 | 置空 |
Docker 测试环境(与 NAS 生产相同的 bloodstar/hexo:latest 镜像):
docker compose -f docker-compose.test.yml up -d
docker compose -f docker-compose.test.yml exec hexo bash
# 容器内:tools/cicd.sh -d (本地调试构建)
exit
docker compose -f docker-compose.test.yml down本机直接构建等价:tools/test.sh build 或 npx hexo server -p 4100(避免与 Docker 预览的 4000 端口冲突)。
5. 发布流程选择(改代码 vs 改内容)
核心原则:改代码用 -r,改内容用 -c/-d。
| 改动类型 | 使用命令 | 原因 |
|---|---|---|
JS/CSS/主题代码(themes/matery/ 下) | tools/cicd.sh -r "特性" | 需递增版本号 + git tag,浏览器/PWA 才拉新版(版本号不变会命中缓存) |
仅文章/内容(source/_posts/ 等) | -d(本地预览)或 -c "说明"(推送) | 内容更新无需版本号,增量发布 |
| 修改 JS 版本号 | 必须 -r(版本号同步 3 处后发布) | 否则生产缓存旧版 |
判断方法:
git status --short themes/matery/ # 有改动 → 代码改动 → -r
git status --short source/ # 仅内容改动 → -d / -c-r 会读取 tools/.VERSION 递增并创建 git tag,-c 复用旧版本号。
6. 双仓库提交(子模块)
主题在子模块 themes/matery/ 中,改主题后两个仓库都要提交:
# 1. 提交子模块
git -C themes/matery add <files>
git -C themes/matery commit -m "[✨] feat(scope): 说明"
# 2. 提交父仓库(更新子模块指针)
git add themes/matery
git commit -m "[🔧] chore: 更新matery子模块指针"本地提交规范:[emoji] type(scope): 描述(✨ feat / 🐛 fix / 📖 docs / 🔧 chore 等),一个提交只做一件事。
附:部署速查表
| 场景 | 命令 |
|---|---|
| 本地预览 | tools/cicd.sh -d 或 npx hexo server -p 4100 |
| 仅内容推送 | tools/cicd.sh -c "说明" |
| 测试域验证 | tools/cicd.sh -t "说明" + make test-pwa |
| 正式发布 | tools/cicd.sh -r "特性" + tools/tests/release-test.sh |
| 本机快速构建 | tools/test.sh build |
| Docker 测试 | docker compose -f docker-compose.test.yml up -d && ... exec hexo bash |