站点部署自动化

随手使用 github action 自动化部署,懒得手动去执行 npx hexo deploy --generate

仓库结构

1
2
3
4
5
6
7
$ tree hexo      > origin/hexo
hexo
├── _config.yml
├── package.json
├── README.md
├── scaffolds
└── source > origin/post (branch as submodule)

YAML 配置 on origin/hexo

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
name: Depoly

on:
push:
branches: [ hexo ]
pull_request:
branches: [ hexo ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Init hexo and post markdown
uses: actions/checkout@v2
with:
submodules: true
ref: "hexo"

- name: Setup Node.js environment
uses: actions/setup-node@v2
with:
node-version: '16'

- name: Add SSH key
run: |
mkdir -p ${HOME}/.ssh
chmod 700 ${HOME}/.ssh
echo "${{ secrets.SSH_KEY }}" > ${HOME}/.ssh/id_rsa
chmod 600 ${HOME}/.ssh/id_rsa

- name: Setup git config
run: |
git config --global user.email "your email"
git config --global user.name "your name"
git config --global core.sshCommand 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'

- name: Build and depoly
working-directory: ${{runner.workspace}}/your_repo
run: |
npm install --force
npx hexo deploy --generate

- name: Clean
run: |
rm -f ${HOME}/.ssh/id_rsa

YAML 配置 on origin/post

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: Depoly

on:
push:
branches: [ post ]
pull_request:
branches: [ post ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Add SSH key
run: |
mkdir -p ${HOME}/.ssh
chmod 700 ${HOME}/.ssh
echo "${{ secrets.SSH_KEY }}" > ${HOME}/.ssh/id_rsa
chmod 600 ${HOME}/.ssh/id_rsa

- name: Setup git config
run: |
git config --global user.email "your email"
git config --global user.name "your name"
git config --global core.sshCommand 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'

- name: Update branch hexo
working-directory: ${{runner.workspace}}
run: |
git clone git@github.com:YourName/your_repo
cd your_repo
git checkout hexo
git submodule update --init --recursive
cd source
git checkout post
git pull
cd ..
git add .
git commit -m "ci: auto update post by $GITHUB_WORKFLOW #$GITHUB_RUN_NUMBER"
git push

- name: Clean
run: |
rm -f ${HOME}/.ssh/id_rsa

by WLOP