In Huntersxy

Hi, nice to meet you.

  1. 1. 一、自动部署(deply.yml)
    1. 1.1. 流程说明
  2. 2. 二、主题同步(sync-theme.yml)
    1. 2.1. 流程说明

记录博客的 CI/CD 配置。当前使用两个工作流:推送自动部署 + 主题自动同步。

一、自动部署(deply.yml)

推送到 main 分支或发布 Release 时,自动构建并部署到 GitHub Pages。

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
60
61
62
63
64
65
66
67
68
69
70
71
name: 自动部署
on:
push:
branches:
- main
release:
types:
- published

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: 检查分支
uses: actions/checkout@v4
with:
ref: main

- name: 安装 Node
uses: actions/setup-node@v4
with:
node-version: '22.x'

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

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

- name: 安装依赖
if: steps.cache.outputs.cache-hit != 'true'
run: |
npm install --save

- name: 缓存 Suka
uses: actions/cache@v4
id: suka
with:
path: themes/suka-bulie/node_modules
key: ${{runner.OS}}-${{hashFiles('**/themes/suka-bulie/package.json')}}

- name: 安装依赖 suka
if: steps.suka.outputs.cache-hit != 'true'
run: |
cd themes/suka-bulie
npm i --production

- name: 配置 Git 凭据
run: |
git config --global credential.helper store
echo "https://x-access-token:${DEPLOY_TOKEN}@github.com" > ~/.git-credentials
chmod 600 ~/.git-credentials
env:
DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}

- name: Prepare git env
run: |
git config --global user.email 1833505972@qq.com
git config --global user.name huntersxy

- name: Deploy Blog
run: |
hexo clean
hexo generate -f
hexo deploy

流程说明

  1. 推送到 main 或发布 Release 时触发
  2. 安装 Node.js 22.x 和 Hexo CLI
  3. 分别缓存站点依赖和主题依赖(主题只装生产依赖 --production
  4. 通过 PAT 凭据(DEPLOY_TOKEN)连接 GitHub
  5. 执行 hexo clean → generate -f → deploy 完成构建和部署

二、主题同步(sync-theme.yml)

博客 push main 时,自动将 themes/suka-bulie/ 同步到独立的主题仓库 hexo-theme-suka-bulie

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: 同步主题到 Fork 仓库
on:
push:
branches:
- main

jobs:
sync-theme:
runs-on: ubuntu-latest
steps:
- name: Checkout blog
uses: actions/checkout@v4
with:
ref: main
path: blog

- name: Checkout theme repo
uses: actions/checkout@v4
with:
repository: huntersxy/hexo-theme-suka-bulie
ref: master
token: ${{ secrets.THEME_REPO_TOKEN }}
path: theme

- name: Sync theme files
run: |
cd theme
git rm -rf . 2>/dev/null || true
cd ..
rsync -av --delete \
--exclude='.git' \
--exclude='node_modules' \
--exclude='_config.yml' \
blog/themes/suka-bulie/ theme/

- name: Commit and push if changed
env:
COMMIT_MSG: ${{ github.event.head_commit.message }}
run: |
cd theme
git config user.email "1833505972@qq.com"
git config user.name "huntersxy"
git add -A
if git diff --cached --quiet; then
echo "No changes to commit"
else
git commit -m "$COMMIT_MSG"
git push origin master
fi

流程说明

  1. 博客 push main 时触发
  2. 同时 checkout 博客仓库和主题仓库
  3. rsync --delete 同步主题目录(排除 .gitnode_modules_config.yml
  4. 有变更时以博客的 commit message 自动提交并推送

本文作者 : Huntersxy
本文使用 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 协议
本文链接 : https://blog.xiey.work/2022/10/03/blog-ci/

本文最后更新于 天前,文中所描述的信息可能已发生改变