参考: https://zellwk.com/blog/github-actions-deploy/

首先博客如果使用hexo搭建, 且借助hexo-deployer-git依赖进行远程上传的话, 则需要将repo设置为ssh方式, https方式是不支持的, 如下

1
2
3
4
5
deploy:
type: git
# Windows: https://github.com/wang-xiaowu/wang-xiaowu.github.io.git
repo: git@github.com:wang-xiaowu/wang-xiaowu.github.io.git
branch: master

当前样例为, 一个blog_source库(未公开)存储未编译md文档, 走cicd流程之后, 将编译好的资源利用hexo d的方式部署到另一个一个github源仓库(开源库)

首先配置ssh/private key

1
ssh-keygen -t rsa -b 4096 -C "943915349@qq.com" -f key

邮箱修改为本人邮箱

复制publickey内容到github源仓库(也就是hexo中远程推送repo的地址)

打开浏览器登录你的github,点击右上角带黄色的头像-->settings-->(personal settings中的)SSH and GPG keys-->new SSH key-->title自己取名,key中粘贴publickey-->Add SSH key-->成功!

在blog_source库中进行操作, 创建一个名为SSH_PRIVATE_KEY的secret,将privatekey内容复制进去,如下

创建一个名为SSH_HOST的secret, 将github.com写入进去. 我们会在之后的操作中将它copy到~/.ssh/known_hosts

创建actions

我们点击configure后, 进入编辑, 编辑内容如下

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
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
# 如果希望是周期性执行, 可以使用schedule, 然后将push和pull_request去掉
# schedule:
# - cron: "0 0 * * *"
# workflow_dispatch:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
node-version: [17.5.0]
# 这里我们选用node 17.5.0, See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout@v3
# 如果theme是通过submodule整合进来的,可以配置如下方式
with:
submodules: recursive
- name: Install SSH Key
uses: shimataro/ssh-key-action@v2
with:
key: ${{ secrets.SSH_PRIVATE_KEY }}
known_hosts: 'just-a-placeholder-so-we-dont-get-errors'
- name: Adding Known Hosts
run: ssh-keyscan -H ${{ secrets.SSH_HOST }} >> ~/.ssh/known_hosts
# 这里我们配置下时区
- name: Restore Timestamps
uses: chetan/git-restore-mtime-action@v2
- name: Set Timezone
run: |
sudo timedatectl set-timezone Asia/Shanghai
timedatectl
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: |-
git config --global user.name 'github-actions[bot]'
git config --global user.email '<改成你自己的邮箱>+github-actions[bot]@users.noreply.github.com'
npm install hexo-cli -g
npm install
hexo clean && hexo g && hexo d

然后提交, 就会触发ci, 将内容发布到源仓库