Zuletzt aktiv 3 months ago

🛠️ 基于 GitHub Actions 的 Hexo 静态博客自动化部署流程。

Änderung 300a5fa7ed4a528331d5ca8ca76113530542db9f

deploy.yml Originalformat
1name: 🚀 自动部署
2
3on:
4 # 🌿 当推送到 main 分支时触发
5 push:
6 branches:
7 - main
8
9 # 🎉 当发布新版本(Release)时触发
10 release:
11 types:
12 - published
13
14 # ⏯️ 手动触发(通过 Actions 页面)
15 workflow_dispatch:
16
17# 🕎 设置时区为中国上海
18env:
19 TZ: Asia/Shanghai
20
21jobs:
22 deploy:
23 runs-on: ubuntu-latest
24 steps:
25 # 🔍 检出代码(默认是触发的分支,这里明确指定 main)
26 - name: 🔎 检查分支
27 uses: actions/checkout@v3
28 with:
29 ref: main
30
31 # 📦 缓存 node_modules 以加速构建
32 - name: 🧠 缓存项目 npm 包
33 id: cache-node-modules
34 uses: actions/cache@v3
35 with:
36 path: node_modules
37 key: ${{ runner.os }}-nodeModules-${{ hashFiles('package-lock.json', 'package.json') }}
38 restore-keys: |
39 ${{ runner.os }}-nodeModules-
40
41 # 🟩 安装 Node.js 环境
42 - name: 🏗️ 安装 Node
43 uses: actions/setup-node@v3
44 with:
45 node-version: "20.x"
46
47 # 🛠️ 全局安装 Hexo CLI(用于生成静态文件)
48 - name: 📥 安装 Hexo
49 run: npm install -g hexo-cli
50
51 # 📥 安装项目依赖(仅当缓存未命中时)
52 - name: ⚙️ 安装依赖
53 if: steps.cache-node-modules.outputs.cache-hit != 'true'
54 run: npm ci # ✅ 推荐使用 npm ci 替代 npm install,更稳定快速
55
56 # 🧹 清理旧的生成文件
57 - name: 🧹 清理文件树
58 run: npm run clean
59
60 # 🏗️ 生成静态文件并压缩
61 - name: 🏗️ 生成静态文件并压缩
62 run: npm run build
63
64 # 🚀 部署到 GitHub Pages(推送到 page 分支)
65 - name: 🚀 部署
66 run: |
67 cd ./public
68 git init
69 git config user.name "${{ github.actor }}"
70 git config user.email "${{ github.actor }}@users.noreply.github.com"
71 git add .
72
73 # 📝 提交信息:原始 commit + 带时区的时间戳
74 local_time=$(date +"%Z %Y-%m-%d %A %H:%M:%S")
75 git commit -m "${{ github.event.head_commit.message }} ·· [$local_time]" || echo "No changes to commit"
76
77 # 🔁 强制推送到 page 分支(用于 GitHub Pages)
78 git push --force --quiet "https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git" master:page
update-deploy.yml Originalformat
1name: 🚀 自动部署 Hexo 博客
2
3on:
4 # 🌿 推送到 main 分支时触发
5 push:
6 branches:
7 - main
8
9 # 🎉 发布新版本时触发
10 release:
11 types:
12 - published
13
14 # ⏯️ 手动触发(Actions 页面点击运行)
15 workflow_dispatch:
16
17# 🕎 设置时区为中国标准时间
18env:
19 TZ: Asia/Shanghai
20
21jobs:
22 deploy:
23 runs-on: ubuntu-latest
24 steps:
25 # 🔁 检出代码(使用最新 v4,更安全高效)
26 - name: 🔎 检查分支
27 uses: actions/checkout@v4
28
29 # 🧠 缓存 node_modules(v4 支持更优压缩)
30 - name: 🧠 缓存项目依赖
31 uses: actions/cache@v4
32 with:
33 path: node_modules
34 key: ${{ runner.os }}-nodeModules-${{ hashFiles('package-lock.json', 'package.json') }}
35 restore-keys: |
36 ${{ runner.os }}-nodeModules-
37
38 # 🏗️ 安装 Node.js 并自动缓存 npm 全局工具
39 - name: 🛠️ 安装 Node.js 环境
40 uses: actions/setup-node@v4
41 with:
42 node-version: "20.x"
43 cache: "npm" # ✅ 自动缓存 npm,加速全局包安装
44
45 # 📥 全局安装 Hexo CLI(锁定版本,避免意外升级)
46 - name: 📥 安装 Hexo CLI
47 run: npm install -g hexo-cli@latest # 推荐生产环境使用固定版本如 @6.3.0
48
49 # ⚙️ 安装项目依赖(仅当缓存未命中)
50 - name: ⚙️ 安装项目依赖
51 if: steps.cache.outputs.cache-hit != 'true'
52 run: npm ci # ✅ CI 环境最佳选择:快速、一致、可靠
53
54 # 🧹 清理旧构建文件
55 - name: 🧹 清理文件树
56 run: npm run clean
57
58 # 🏗️ 生成静态文件(含压缩)
59 - name: 🏗️ 构建静态站点
60 run: npm run build
61
62 # 🚀 部署到 GitHub Pages(推送到 page 分支)
63 - name: 🚀 部署到 GitHub Pages
64 run: |
65 # 确保 public 目录存在
66 mkdir -p public
67 cd public
68
69 # 初始化 Git
70 git init
71 git config user.name "${{ github.actor }}"
72 git config user.email "${{ github.actor }}@users.noreply.github.com"
73
74 # 添加所有文件
75 git add .
76
77 # 提交更改(若无变更则跳过)
78 local_time=$(date +"%Z %Y-%m-%d %A %H:%M:%S")
79 commit_msg="${{ github.event.head_commit.message }} ·· [$local_time]"
80 git commit -m "$commit_msg" || echo "➡️ 无变更,跳过提交"
81
82 # 强制推送到远程 page 分支
83 git remote add origin "https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git"
84 git push --force --quiet origin main:page
85
86 env:
87 # 确保 GITHUB_TOKEN 权限足够(默认 actions 有写权限)
88 GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}