在Github使用PAT方式跨仓库部署
Reverse Lv4

如果想直接用 Github Actions 的工作流自动化部署构建 Hexo 项目的话,需要编写 workflows 配置

创建 Token

首先去创建一个 Token

  1. 访问 https://github.com/settings/tokens
  2. 点击 “Fine-grained token”
  3. 选择权限 repo (将这几个启用:contentsactionsdeployments
  4. 有效期 随便多久都行,主要看你,我直接永久

然后他会生成一个 Token,这个 Token 只显示一次,一定要先记下

配置 Secrets

接下来进入你的构建目标仓库,依次选择: Settings > Secrets and variables > Actions

添加一个 secret,比如我这里变量名字叫 BLOG_TOKEN,然后值就是上面生成的 Token,填进去保存即可

配置 workflows

接着改改你的构建流程,把刚才创建的 BLOG_TOKEN 变量引用进去

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
name: Deploy Hexo to GitHub Pages

on:
push:
branches:
- main # 或者是 master,根据你的默认分支修改

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

steps:
- name: Checkout source code
uses: actions/checkout@v4

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: "18" # 根据你的项目需要修改 Node 版本
cache: "npm"

- name: Install dependencies
run: |
npm install -g hexo-cli
npm install

- name: Clean and generate Hexo site
run: |
hexo clean
hexo generate

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