Hexo Github Actions

检测source分支改动,自动Deploy到master分支

步骤

在仓库设置中修改(全局配置)

  • 进入你的 GitHub 仓库。
  • 点击 Settings -> Actions -> General。
  • 滚动到页面底部的 Workflow permissions 部分。
  • 选择 Read and write permissions。
  • 点击 Save。

使用

/.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
42
43
name: Hexo Deploy

on:
push:
branches:
- source # 触发分支:当 source 分支有更新时执行

# 【关键点】必须赋予 GITHUB_TOKEN 写入权限,否则无法推送代码
permissions:
contents: write

jobs:
build:
runs-on: ubuntu-latest
name: auto deploy

steps:
- name: Checkout Repository
uses: actions/checkout@v4
with:
ref: source

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

- name: Generate Public Files
run: |
npm install
npx hexo clean
npx hexo generate

# 部署到当前仓库(使用 GITHUB_TOKEN)
- name: Deploy Hexo
uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_branch: master # 部署到当前仓库的 master 分支 (或者改为 gh-pages)
publish_dir: ./public
commit_message: ${{ github.event.head_commit.message }}
user_name: 'yourname'
user_email: '[email protected]'