4 Ways to Deploy Hexo

Hexo 四种部署方式

4 Ways to Deploy Hexo

🌐 快速安装

1
2
3
4
5
6
7
8
9
10
# 安装 Node.js 版本需不低于 10.13
# 建议使用 Node.js 12.0 及以上版本
# 安装 Git
# npm 安装 Hexo
npm install -g hexo-cli

# Hexo 初始化
hexo init <folder>
cd <folder>
npm install

1️⃣ 部署本仓库 GitHub Actions (官方推荐)

在 GitHub Pages 上部署 Hexo

  • .gitignore 文件中包含一行 public/
  • 自定义域名需要在 source/ 文件夹中新增 CNAME
  • 建立 .github/workflows/pages.yml
  • 本地检查 node --version,替换版本
  • 将 main 分支 push 到远程仓库
  • 远程仓库 Settings -> Pages -> Source -> GitHub Actions

官方版本

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
.github/workflows/pages.yml
name: Pages

on:
push:
branches:
- main # default branch

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
# If your repository depends on submodule, please see: https://github.com/actions/checkout
submodules: recursive
- name: Use Node.js 20
uses: actions/setup-node@v4
with:
# Examples: 20, 18.19, >=16.20.2, lts/Iron, lts/Hydrogen, *, latest, current, node
# Ref: https://github.com/actions/setup-node#supported-version-syntax
node-version: \"20\"
- name: Cache NPM dependencies
uses: actions/cache@v4
with:
path: node_modules
key: ${{ runner.OS }}-npm-cache
restore-keys: |
${{ runner.OS }}-npm-cache
- name: Install Dependencies
run: npm install
- name: Build
run: npm run build
- name: Upload Pages artifact
uses: actions/upload-pages-artifact@v3
with:
path: ./public
deploy:
needs: build
permissions:
pages: write
id-token: write
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4

简化版

name: simple-hexo-deploy-actions

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
# .github/workflows/simple-hexo-deploy-actions.yml
name: simple-hexo-deploy-actions

on:
push:
branches: [ main ]
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- uses: actions/setup-node@v4
with:
node-version: \"20\"
- uses: actions/cache@v4
with:
path: node_modules
key: ${{ runner.OS }}-npm-cache
- run: npm install
- run: npm run build
- uses: actions/upload-pages-artifact@v3
with:
path: ./public

deploy:
needs: build
permissions:
pages: write
id-token: write
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- uses: actions/deploy-pages@v4
id: deployment

2️⃣ 一键部署

一键部署

  • 安装 hexo-deployer-git
1
npm install hexo-deployer-git --save
  • 编辑 _config.yml (示例值)
1
2
3
4
5
deploy:
type: git
repo: <repository url> # https://bitbucket.org/JohnSmith/johnsmith.bitbucket.io
branch: [branch]
message: [message]

3️⃣ 部署本仓库 gh-pages

使用 Personal Access Token (PAT)

  • 步骤1:创建 Personal Access Token
    点击 GitHub 右上角头像 → Settings,左侧菜单选择 Developer settingsPersonal access tokensTokens (classic),点击 Generate new token (classic)

设置:
Note: 填写名称(如 "gh-pages-deploy")
Expiration: 选择合适的时间
Scopes: 勾选 repo(完整权限)
生成并复制 token(只显示一次,务必保存)

  • 步骤2:在仓库中添加 Secret
    进入你的仓库 SettingsSecrets and variablesActions,点击 New repository secret
    Name: PERSONAL_TOKEN
    Secret: 粘贴你复制的 token

  • 步骤3:修改 workflow 文件

1
2
3
4
5
6
7
8
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v4
with:
# 使用 PAT 而不是 GITHUB_TOKEN
personal_token: ${{ secrets.PERSONAL_TOKEN }}
# 或者你的构建输出目录
publish_dir: ./build
publish_branch: gh-pages

简化版

name: simple-hexo-deploy-pages

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
55
56
57
# .github/workflows/simple-hexo-deploy-pages.yml
name: simple-hexo-deploy-pages

on:
push:
branches: [ main ]
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- uses: actions/setup-node@v4
with:
node-version: \"20\"
- uses: actions/cache@v4
with:
path: node_modules
key: ${{ runner.OS }}-npm-cache
- run: npm install
- run: npm run build
- uses: actions/upload-artifact@v4
with:
name: public
path: ${{ github.workspace }}/public
retention-days: 1

deploy:
needs: build
permissions:
pages: write
id-token: write
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
with:
name: public
path: ./public
- uses: peaceiris/actions-gh-pages@v4
with:
personal_token: ${{ secrets.PERSONAL_TOKEN }}
external_repository: ${{ github.respository }}
publish_branch: gh-pages
publish_dir: ./public
keep_files: false
force_orphan: false
commit_message: '🚀 Deploy static pages to gh-pages'
allow_empty_commit: false
cname: ${{ github.respository_owner }}.github.io/${{ github.event.respository.name }}
user_name: \"github-actions[bot]\"
user_email: \"github-actions[bot]@users.noreply.github.com\"

4️⃣ 部署其他仓库 gh-pages

使用 Personal Access Token (PAT) 方法同上

简化版

name: simple-hexo-deploy-other

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
55
56
57
# .github/workflows/simple-hexo-deploy-other.yml
name: simple-hexo-deploy-other

on:
push:
branches: [ main ]
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- uses: actions/setup-node@v4
with:
node-version: \"20\"
- uses: actions/cache@v4
with:
path: node_modules
key: ${{ runner.OS }}-npm-cache
- run: npm install
- run: npm run build
- uses: actions/upload-artifact@v4
with:
name: public
path: ${{ github.workspace }}/public
retention-days: 1

deploy:
needs: build
permissions:
pages: write
id-token: write
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
with:
name: public
path: ./public
- uses: peaceiris/actions-gh-pages@v4
with:
personal_token: ${{ secrets.PERSONAL_TOKEN }}
external_repository: # 用户名/其他仓库
publish_branch: gh-pages
publish_dir: ./public
keep_files: false
force_orphan: false
commit_message: '🚀 Deploy static pages to gh-pages'
allow_empty_commit: false
cname: # 自定义
user_name: \"github-actions[bot]\"
user_email: \"github-actions[bot]@users.noreply.github.com\"