更新于 

自动部署

自动部署

使用 GitHub Actions 实现自动部署,每次推送代码自动构建并发布博客。

GitHub Actions 概述

GitHub Actions 是 GitHub 提供的 CI/CD 服务,可以自动化构建、测试和部署工作流。

配置工作流

在博客根目录创建 .github/workflows/deploy.yml

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
name: Deploy Hexo Blog

on:
push:
branches:
- main # 推送到 main 分支时触发

jobs:
build-and-deploy:
runs-on: ubuntu-latest

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

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Cache dependencies
uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-

- name: Install dependencies
run: npm install

- name: Build
run: hexo generate

- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./public

启用 GitHub Pages

  1. 进入仓库 Settings → Pages
  2. Source 选择 Deploy from a branch
  3. Branch 选择 gh-pages,目录选择 / (root)
  4. 保存

使用 Vercel 自动部署

如果使用 Vercel,不需要配置 GitHub Actions。Vercel 会自动检测推送并部署:

1
2
3
4
5
# 只需要正常推送代码
git add .
git commit -m "new post"
git push origin main
# Vercel 自动部署

使用 Netlify 自动部署

  1. 登录 netlify.com
  2. 导入 GitHub 仓库
  3. 配置:
    • Build command: hexo generate
    • Publish directory: public
  4. 保存

每次推送代码,Netlify 会自动构建和部署。

写作工作流

配置好自动部署后,写作流程变得非常简单:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 1. 新建文章
hexo new "我的新文章"

# 2. 编辑文章
# 用你喜欢的编辑器编辑 source/_posts/我的新文章.md

# 3. 本地预览(可选)
hexo server

# 4. 提交并推送
git add .
git commit -m "post: 我的新文章"
git push origin main

# 5. 等待自动部署完成(通常 1-3 分钟)

多分支部署

可以为不同分支部署不同版本:

1
2
3
4
5
on:
push:
branches:
- main
- dev

定时部署

使用 cron 表达式定时触发部署:

1
2
3
4
5
6
on:
schedule:
- cron: '0 0 * * 1' # 每周一 UTC 00:00
push:
branches:
- main

💡 提示:推荐使用 Vercel 或 Netlify 自动部署,比 GitHub Actions 更简单,配置更少。


本站由 Moriefy 使用 Stellar1.22.1 主题创建
使用Hexo Github Vercel 强力驱动
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。

发表了 16 篇文章 · 总计 18.5k 字