利用Github Action 自动部署hexo

首先是新建了一个私有的仓库用于存放源代码。然后将原先的源代码push到这个新的私有仓库中。(push 不需要node_modules)

然后在 私有仓库-> setting -> Secrets -> Action 下创建一个ACtion sercets
KEY的话需要使用私钥,位置在 “C:\Users\用户.ssh\id_rsa” ,全部粘贴过去。(注意这个私钥的名字 HEXO_DEPLOY_PRIVATE_KEY)

然后就是点击 Action -> set up a workflow -> 在 main.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: Deploy

# 这个地方就是什么时候脚本开始运行,设置的是当push到main上时候运行。
on:
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest
name: A job to deploy blog.
steps:
- name: Checkout
uses: actions/checkout@v1
with:
submodules: true # Checkout private submodules(themes or something else).

# Caching dependencies to speed up workflows. (GitHub will remove any cache entries that have not been accessed in over 7 days.)
- name: Cache node modules
uses: actions/cache@v1
id: cache
with:
path: node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: Install Dependencies
if: steps.cache.outputs.cache-hit != 'true'
run: npm ci

# Deploy hexo blog website.
- name: Deploy
id: deploy
uses: sma11black/hexo-action@v1.0.3
with:
deploy_key: ${{ secrets.HEXO_DEPLOY_PRIVATE_KEY }}
commit_msg: ${{ github.event.head_commit.message }} # (or delete this input setting to use hexo default settings)
# Use the output from the `deploy` step(use for test action)
- name: Get the output
run: |
echo "${{ steps.deploy.outputs.notify }}"

手机上添加测试

https://github.com/marketplace/actions/hexo-action

最后吐槽一下,网上很多代码都不能用

上面那个又不能用了:

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
name: Deploy
on:
push:
branches: [ "main" ]
jobs:
HexoDeploy:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.4]
steps:
- uses: actions/checkout@v3
- name: 使用 Node.js ${{ matrix.node-version }} 环境
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: 配置 SSH 环境和 Git 环境
env:
ACTION_DEPLOY_KEY: ${{ secrets.HEXO_DEPLOY_PRIVATE_KEY }}
run: |
mkdir -p ~/.ssh/
echo "$ACTION_DEPLOY_KEY" > ~/.ssh/id_rsa
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa
ssh-keyscan github.com >> ~/.ssh/known_hosts
git config --global user.email "laptype@163.com"
git config --global user.name "laptype"
- name: 配置 Hexo 环境
run: |
export TZ='Asia/Shanghai'
npm install hexo-cli -g
- name: 下载 Npm 依赖
run: |
npm install
- name: 部署博客
run: |
hexo d

https://blog.csdn.net/dzq198/article/details/136180622