这篇文章记录从环境准备到发布到 GitHub 的完整流程,适合以后照着复用。

1. 安装 Node.js

  • 访问 https://nodejs.org 下载 LTS 版本并安装(会附带 npm)。
  • 安装完后在终端验证:
1
2
node -v
npm -v

2. 克隆博客仓库

1
2
git clone https://github.com/fenghb342/blog_hexo.git
cd blog_hexo

如果在 Windows,建议在项目根目录打开终端。

3. 安装依赖

首次进入项目先装依赖:

1
npm install

之后如需清理可用 npm run clean

4. 本地开发常用命令

  • 生成静态文件:
1
npm run build   # 等价 hexo generate
  • 启动本地预览:
1
npx hexo server -p 4001

若端口占用可换其它端口,例如 4002。

  • 清理缓存和旧的 public:
1
npx hexo clean

5. 新建文章

1
npx hexo new "文章标题"

会在 source/_posts 下生成 Markdown 文件,编辑后保存即可。

6. 部署到 GitHub

  1. 设置部署仓库(只需一次):编辑 _config.yml 的 deploy 段,填入 GitHub 仓库地址,例如:
1
2
3
4
deploy:
type: git
repo: https://github.com/fenghb342/blog_hexo.git
branch: master
  1. 每次发布执行:
1
2
3
npx hexo clean
npx hexo generate
npx hexo deploy

Hexo 会把生成的 public 推送到指定分支。若用 Cloudflare Pages,可改为推送源码到 GitHub,让 Pages 自动执行 npm run build 并发布。

7. 提交源码改动

除了发布静态文件,也别忘了把源文件提交到自己的仓库备份:

1
2
3
git add .
git commit -m "update blog content"
git push origin master

这样下次换电脑或回滚都有记录。

8. 更新 Butterfly 主题

需要从官方仓库拉取最新主题版本时:

1
2
3
rm -rf themes/butterfly
git clone -b master https://github.com/jerryc127/hexo-theme-butterfly.git themes/butterfly
npx hexo generate

主题的自定义配置请修改 themes外面的_config.butterfly.yml(而不是站点根目录的 _config.yml,也不是主题里面的_config.yml,但是更新主题的时候可能里面的_config.yml跟外面的_config.butterfly.yml不同)。

9. 部署到 Cloudflare(Wrangler)

若使用 Workers/Pages 静态资产模式发布:

1
2
3
npx hexo clean
npm run build # 生成 public/
npx wrangler deploy # 或 npm run deploy

提前在本机或 CI 执行一次 wrangler login,并确保 wrangler.jsonc 中 assets.directory 指向 public。凭证在 CI 需用 CLOUDFLARE_API_TOKEN/ACCOUNT_ID 配置。更新后重复上述清理、构建、部署三步即可。