引用:building-github-action-to-publish-hexo-post-from-github-issue

仓库搭建

这里我们使用两个仓库。

私有仓库yuan1/blog 用于存放hexo源文件,并在issue编写博客
公开仓库yuan1/yuan1.github.io

blog仓库配置

hexo 构建

主要通过hexo 官网 进行构建,构建完成后进行hexo 配置,这里不再赘述。

需要注意的是不需要配置deploy推送。

submodule

利用 git submodule 添加 yuan1/yuan1.github.io 到 public 文件夹。

1
2
git submodule add --force git@github.com:yuan1/yuan1.github.io.git public

注意:如果使用了hexo 构建会清空public 文件夹,所以不用使用hexo 构建命令,并且保证public 文件夹为submodule 并指向 yuan1/yuan1.github.io。

token配置

生成personal access token 在Token页面中,点击New personal access token,输入名字和有效期并选择Scope->repo勾选,并复制生成的token。

到blog仓库下,分别Settings->New repository secret -> 输入name为token,value粘贴token并保存 。这个token用于后续action对仓库进行拉取和推送操作。

Workflow

添加hexo-publish.yml流程文件到.github/workflows/,主要功能为hexo 构建到public 目录,并自动提交到yuan1/yuan1.github.io.git。

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
name: Hexo Publish
on:
push:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
persist-credentials: 'false'
submodules: 'true'
# if private repo
token: ${{ secrets.token }}
- uses: actions/setup-node@v3
with:
node-version: 16
# This submodule is a directory that contains generated Hexo blog
- name: Checkout submodule
run: |
cd public
git checkout main
- name: Build
run: |
npm install
npm run build
- name: Commit submodule
run: |
cd public
git add .
git config user.name "yuan1/blog bot"
git config user.email "<>"
git commit -m "Publish"
- name: Push submodule
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.token }}
directory: 'public'
repository: 'yuan1/yuan1.github.io'
# These actions commits and pushes the submodule to the main module
- name: Commit main module
run: |
git add .
git config user.name "yuan1/blog bot"
git config user.email "<>"
git commit -m "Publish"
- name: Push main module
uses: ad-m/github-push-action@master

添加issue-to-hexo.yml流程文件到.github/workflows/,主要功能为监听此仓库的github issue设置milestoned,利用gsongsong/issue-to-hexo@v1.0.0 action,将milestoned 为 publish的进行转换到 hexo markdown 文件并push。

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: Issue to Hexo
on:
issues:
# Sufficient to trigger this workflow when an issue is milestoned
types: [ milestoned ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
persist-credentials: 'false'
- uses: actions/setup-node@v3
with:
node-version: 16
- uses: zcong1993/setup-timezone@master
with:
timezone: Asia/Shanghai
- uses: gsongsong/issue-to-hexo@v1.0.0
with:
issue_url: ${{ github.event.issue.url }}
# Personal access token used to get information of Issue
token: ${{ secrets.token }}
# At this point, a markdown file is generated and untracked
# Take further action, e.g. generate (`hexo generate`), commit and push
- name: Commit post
run: |
git add .
git config user.name "issue-to-hexo bot"
git config user.email "<>"
git commit -m "Add a post"
- name: Push
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.token }}

提交并测试

提交blog 会触发 Hexo Publish 流程,自动发布到yuan1.github.io仓库。

在blog 仓库中提交issus 并设置milestoned为publish会触发 Issue to Hexo流程进行转换。

其他

如遇到问题或者又不懂的地方,可以进行留言。