deploy.yml
· 2.4 KiB · YAML
Originalformat
name: 🚀 自动部署
on:
# 🌿 当推送到 main 分支时触发
push:
branches:
- main
# 🎉 当发布新版本(Release)时触发
release:
types:
- published
# ⏯️ 手动触发(通过 Actions 页面)
workflow_dispatch:
# 🕎 设置时区为中国上海
env:
TZ: Asia/Shanghai
jobs:
deploy:
runs-on: ubuntu-latest
steps:
# 🔍 检出代码(默认是触发的分支,这里明确指定 main)
- name: 🔎 检查分支
uses: actions/checkout@v3
with:
ref: main
# 📦 缓存 node_modules 以加速构建
- name: 🧠 缓存项目 npm 包
id: cache-node-modules
uses: actions/cache@v3
with:
path: node_modules
key: ${{ runner.os }}-nodeModules-${{ hashFiles('package-lock.json', 'package.json') }}
restore-keys: |
${{ runner.os }}-nodeModules-
# 🟩 安装 Node.js 环境
- name: 🏗️ 安装 Node
uses: actions/setup-node@v3
with:
node-version: "20.x"
# 🛠️ 全局安装 Hexo CLI(用于生成静态文件)
- name: 📥 安装 Hexo
run: npm install -g hexo-cli
# 📥 安装项目依赖(仅当缓存未命中时)
- name: ⚙️ 安装依赖
if: steps.cache-node-modules.outputs.cache-hit != 'true'
run: npm ci # ✅ 推荐使用 npm ci 替代 npm install,更稳定快速
# 🧹 清理旧的生成文件
- name: 🧹 清理文件树
run: npm run clean
# 🏗️ 生成静态文件并压缩
- name: 🏗️ 生成静态文件并压缩
run: npm run build
# 🚀 部署到 GitHub Pages(推送到 page 分支)
- name: 🚀 部署
run: |
cd ./public
git init
git config user.name "${{ github.actor }}"
git config user.email "${{ github.actor }}@users.noreply.github.com"
git add .
# 📝 提交信息:原始 commit + 带时区的时间戳
local_time=$(date +"%Z %Y-%m-%d %A %H:%M:%S")
git commit -m "${{ github.event.head_commit.message }} ·· [$local_time]" || echo "No changes to commit"
# 🔁 强制推送到 page 分支(用于 GitHub Pages)
git push --force --quiet "https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git" master:page
| 1 | name: 🚀 自动部署 |
| 2 | |
| 3 | on: |
| 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 | # 🕎 设置时区为中国上海 |
| 18 | env: |
| 19 | TZ: Asia/Shanghai |
| 20 | |
| 21 | jobs: |
| 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
· 3.5 KiB · YAML
Originalformat
name: 🚀 自动部署 Hexo 博客
on:
# 🌿 推送到 main 分支时自动触发
push:
branches:
- main
# 🎉 发布新版本时触发(可用于强制重建)
release:
types:
- published
# ⏯️ 支持在 GitHub Actions 页面手动点击运行
workflow_dispatch:
# 🕎 设置系统时区为中国标准时间(UTC+8)
env:
TZ: Asia/Shanghai
jobs:
deploy:
runs-on: ubuntu-latest
steps:
# 🔍 检出源码(使用最新 v4,支持更好的性能和安全)
- name: 🔎 检出源码
uses: actions/checkout@v4
with:
ref: main # ✅ 明确指定分支,避免意外
# 🧠 缓存 node_modules 以大幅提升安装速度
- name: 🧠 缓存项目依赖
id: cache-node-modules
uses: actions/cache@v4
with:
path: node_modules
# 🔑 基于 lock 文件生成缓存键,确保一致性
key: ${{ runner.os }}-nodeModules-${{ hashFiles('package-lock.json') }}
# 🔁 缓存未命中时尝试恢复旧缓存(提高命中率)
restore-keys: |
${{ runner.os }}-nodeModules-
# 🛠️ 安装指定版本的 Node.js 环境
- name: 🛠️ 安装 Node.js 环境
uses: actions/setup-node@v4
with:
node-version: "22.x" # ✅ 匹配你本地的 v22.17.1
cache: "npm" # 💡 自动缓存 npm 全局包(如 hexo-cli),加速后续步骤
# 📦 全局安装 Hexo CLI(锁定版本,避免意外升级)
- name: 📦 安装 Hexo CLI
run: |
npm install -g hexo-cli@4.3.2 # ✅ 推荐生产环境锁定版本(你当前也是 4.3.2)
# ⚙️ 安装项目依赖(仅当缓存未命中时执行)
- name: ⚙️ 安装项目依赖
if: steps.cache-node-modules.outputs.cache-hit != 'true'
run: |
npm ci # ✅ CI/CD 最佳实践:快速、一致、可重现
# 🧹 清理上一次构建的残留文件
- name: 🧹 清理旧构建文件
run: |
npm run clean # 👉 确保 package.json 中有 "clean": "hexo clean"
# 🏗️ 生成静态站点(含压缩)
- name: 🏗️ 构建静态站点
run: |
npm run build # 👉 确保 package.json 中有 "build": "hexo generate"
# 🚀 部署到 GitHub Pages(推送到 page 分支)
- name: 🚀 部署到 GitHub Pages
run: |
# 检查 public 目录是否存在
cd public || { echo "❌ public 目录不存在,请检查构建是否成功"; exit 1; }
# 初始化 Git 仓库
git init
git config user.name "${{ github.actor }}"
git config user.email "${{ github.actor }}@users.noreply.github.com"
# 🔍 检查是否有变更,避免空提交
if git diff --cached --quiet; then
echo "➡️ 无文件变更,跳过部署"
exit 0
fi
# 📝 生成提交信息(原始 commit + 时间戳)
local_time=$(date +"%Z %Y-%m-%d %A %H:%M:%S")
commit_msg="${{ github.event.head_commit.message }} ·· [$local_time]"
git add .
git commit -m "$commit_msg"
# 🌐 推送到远程仓库的 page 分支
git remote add origin "https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git"
git push --force --quiet origin HEAD:page # ✅ 使用 HEAD 而非 master/main
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
| 1 | name: 🚀 自动部署 Hexo 博客 |
| 2 | |
| 3 | on: |
| 4 | # 🌿 推送到 main 分支时自动触发 |
| 5 | push: |
| 6 | branches: |
| 7 | - main |
| 8 | |
| 9 | # 🎉 发布新版本时触发(可用于强制重建) |
| 10 | release: |
| 11 | types: |
| 12 | - published |
| 13 | |
| 14 | # ⏯️ 支持在 GitHub Actions 页面手动点击运行 |
| 15 | workflow_dispatch: |
| 16 | |
| 17 | # 🕎 设置系统时区为中国标准时间(UTC+8) |
| 18 | env: |
| 19 | TZ: Asia/Shanghai |
| 20 | |
| 21 | jobs: |
| 22 | deploy: |
| 23 | runs-on: ubuntu-latest |
| 24 | steps: |
| 25 | # 🔍 检出源码(使用最新 v4,支持更好的性能和安全) |
| 26 | - name: 🔎 检出源码 |
| 27 | uses: actions/checkout@v4 |
| 28 | with: |
| 29 | ref: main # ✅ 明确指定分支,避免意外 |
| 30 | |
| 31 | # 🧠 缓存 node_modules 以大幅提升安装速度 |
| 32 | - name: 🧠 缓存项目依赖 |
| 33 | id: cache-node-modules |
| 34 | uses: actions/cache@v4 |
| 35 | with: |
| 36 | path: node_modules |
| 37 | # 🔑 基于 lock 文件生成缓存键,确保一致性 |
| 38 | key: ${{ runner.os }}-nodeModules-${{ hashFiles('package-lock.json') }} |
| 39 | # 🔁 缓存未命中时尝试恢复旧缓存(提高命中率) |
| 40 | restore-keys: | |
| 41 | ${{ runner.os }}-nodeModules- |
| 42 | |
| 43 | # 🛠️ 安装指定版本的 Node.js 环境 |
| 44 | - name: 🛠️ 安装 Node.js 环境 |
| 45 | uses: actions/setup-node@v4 |
| 46 | with: |
| 47 | node-version: "22.x" # ✅ 匹配你本地的 v22.17.1 |
| 48 | cache: "npm" # 💡 自动缓存 npm 全局包(如 hexo-cli),加速后续步骤 |
| 49 | |
| 50 | # 📦 全局安装 Hexo CLI(锁定版本,避免意外升级) |
| 51 | - name: 📦 安装 Hexo CLI |
| 52 | run: | |
| 53 | npm install -g hexo-cli@4.3.2 # ✅ 推荐生产环境锁定版本(你当前也是 4.3.2) |
| 54 | |
| 55 | # ⚙️ 安装项目依赖(仅当缓存未命中时执行) |
| 56 | - name: ⚙️ 安装项目依赖 |
| 57 | if: steps.cache-node-modules.outputs.cache-hit != 'true' |
| 58 | run: | |
| 59 | npm ci # ✅ CI/CD 最佳实践:快速、一致、可重现 |
| 60 | |
| 61 | # 🧹 清理上一次构建的残留文件 |
| 62 | - name: 🧹 清理旧构建文件 |
| 63 | run: | |
| 64 | npm run clean # 👉 确保 package.json 中有 "clean": "hexo clean" |
| 65 | |
| 66 | # 🏗️ 生成静态站点(含压缩) |
| 67 | - name: 🏗️ 构建静态站点 |
| 68 | run: | |
| 69 | npm run build # 👉 确保 package.json 中有 "build": "hexo generate" |
| 70 | |
| 71 | # 🚀 部署到 GitHub Pages(推送到 page 分支) |
| 72 | - name: 🚀 部署到 GitHub Pages |
| 73 | run: | |
| 74 | # 检查 public 目录是否存在 |
| 75 | cd public || { echo "❌ public 目录不存在,请检查构建是否成功"; exit 1; } |
| 76 | |
| 77 | # 初始化 Git 仓库 |
| 78 | git init |
| 79 | git config user.name "${{ github.actor }}" |
| 80 | git config user.email "${{ github.actor }}@users.noreply.github.com" |
| 81 | |
| 82 | # 🔍 检查是否有变更,避免空提交 |
| 83 | if git diff --cached --quiet; then |
| 84 | echo "➡️ 无文件变更,跳过部署" |
| 85 | exit 0 |
| 86 | fi |
| 87 | |
| 88 | # 📝 生成提交信息(原始 commit + 时间戳) |
| 89 | local_time=$(date +"%Z %Y-%m-%d %A %H:%M:%S") |
| 90 | commit_msg="${{ github.event.head_commit.message }} ·· [$local_time]" |
| 91 | git add . |
| 92 | git commit -m "$commit_msg" |
| 93 | |
| 94 | # 🌐 推送到远程仓库的 page 分支 |
| 95 | git remote add origin "https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git" |
| 96 | git push --force --quiet origin HEAD:page # ✅ 使用 HEAD 而非 master/main |
| 97 | |
| 98 | env: |
| 99 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |