准备

1. 创建公有仓库

公有仓库配置GitHub Pages,配置完成就ok了

2. 创建私有仓库

该仓库主要存放博客源码信息的

创建完成之后需要创建一个永不过期的account_token

创建完成后在当前仓库下,settings -> Secrets and variables -> Action下点击 New repository secret 创建一个token变量,名称随便填,如:BASE_TOKE,值则是上一步创建的token的值,然后保存就行

3. 创建action

在私有仓库下创建目录.github/workflows,在这个目录下创建一个xxx.yml的配置文件,例如:pages.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
name: Deploy

# on 定义的是执行该 Action 的时机,这里的配置是指:
# 当 main 分支的 push 事件被触发的时候执行该任务
on:
push:
branches: [main]
# jobs 是定义你要运行的具体任务,
# 每个 Action 可以有多个 Job
# 每个 Job 可以有多个 Step
# 每个 Step 又可以有多个 Run
jobs:
build:
runs-on: ubuntu-latest # 运行在 ubuntu 系统环境下
steps:
- name: 检查分支
uses: actions/checkout@v2 # uses 命令允许我们使用别人定义好的 Action,这里是从仓库检出分支

- name: 安装 Node
uses: actions/setup-node@v2 # 这里是设置 node 版本
with: # 这里相当于传递给上面指令的参数,告诉他使用 node@12.13.1 这个版本
node-version: '16.20.1'

- name: 安装 Hexo
run: |
export TZ='Asia/Shanghai'
npm install hexo-cli -g

- name: 缓存 Hexo
uses: actions/cache@v1
id: cache
with:
path: node_modules
key: ${{runner.OS}}-${{hashFiles('**/package-lock.json')}}

- name: 安装依赖 # name 定义步骤的名称,这个可以在该 Action 运行起来之后,在 Github Actions Tab 下看到
if: steps.cache.outputs.cache-hit != 'true'
run: | # `run: |` 的方式,允许我们一次定义多个要执行的命令
npm i gulp-cli -g --registry=https://registry.npm.taobao.org
npm i --registry=https://registry.npm.taobao.org

- name: 生成静态文件
run: |
hexo clean
hexo generate
hexo deploy
gulp

- name: 部署
working-directory: ./public # 指定下面指令运行的目录
run: |
git init
git checkout -b main
git add -A
# 这里把下面所有 `<YOUR_NAME>/<YOUR_EMAIL>/<YOUR_TOKEN>` 替换成你自己的就可以了
git -c user.name='YOUR_NAME' -c user.email='YOUR_EMAIL' commit -m 'Updated By Github Actions'
# 这里需要一个 共开仓库 的 secrets 配置,这里的 secrets 是一个环境变量,可以直接拿过来直接用
git remote add origin https://${{secrets.YOUR_TOKEN}}@github.com/YOUR_NAME/YOUR_NAME.github.io.git
git push origin main -f -q

然后提交就可以自动部署的公共仓库了。。。