deploy.yml
· 2.3 KiB · YAML
Raw
name: 🚀 自动部署 Hexo 博客
on:
push:
branches:
- main # 🌿 监听 main 分支的推送
release:
types:
- published # 🎉 监听新版本发布
workflow_dispatch: # ⏯️ 支持手动触发
env:
TZ: Asia/Shanghai # 🕰️ 设置时区为中国标准时间
jobs:
deploy:
runs-on: ubuntu-latest
steps:
# 1️⃣ 检出 main 分支代码
- name: 🔍 检查分支 & 拉取代码
uses: actions/checkout@v4
with:
ref: main
# 注意:Hexo 源码在 main,public 输出后推送到 page
# 2️⃣ 缓存 node_modules 以加速安装
- name: 💾 缓存 Node.js 依赖
id: cache-node-modules
uses: actions/cache@v4
with:
path: node_modules
key: ${{ runner.os }}-nodeModules-${{ hashFiles('package-lock.json') }}
restore-keys: |
${{ runner.os }}-nodeModules-
# 3️⃣ 安装匹配的 Node.js 版本(支持 v22)
- name: 🟨 安装 Node.js
uses: actions/setup-node@v4
with:
node-version: "22.x" # ✅ 匹配你本地 v22.17.1,避免版本不一致问题
cache: 'npm'
# 4️⃣ 全局安装 hexo-cli(仅一次)
- name: 🛠️ 安装 Hexo CLI
run: npm install -g hexo-cli
# 5️⃣ 安装项目依赖(跳过缓存命中时)
- name: 📦 安装项目依赖
if: steps.cache-node-modules.outputs.cache-hit != 'true'
run: npm ci # ✅ 推荐使用 npm ci 替代 npm install,更稳定、更快
# 6️⃣ 清理旧生成文件
- name: 🧹 清理旧文件
run: npm run clean
# 7️⃣ 生成静态文件(含压缩)
- name: 🏗️ 生成静态文件并压缩
run: npm run build
# 8️⃣ 部署到 page 分支
- name: 🚀 部署到 GitHub Pages
run: |
cd ./public
git init
git config user.name "${{ github.actor }}"
git config user.email "${{ github.actor }}@users.noreply.github.com"
git add .
git commit -m "🤖 自动部署 Hexo 博客: ${{ github.event.head_commit.message }} ·· [$(date +'%Z %Y-%m-%d %A %H:%M:%S')]"
git push --force --quiet "https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git" master:page
| 1 | name: 🚀 自动部署 Hexo 博客 |
| 2 | |
| 3 | on: |
| 4 | push: |
| 5 | branches: |
| 6 | - main # 🌿 监听 main 分支的推送 |
| 7 | release: |
| 8 | types: |
| 9 | - published # 🎉 监听新版本发布 |
| 10 | workflow_dispatch: # ⏯️ 支持手动触发 |
| 11 | |
| 12 | env: |
| 13 | TZ: Asia/Shanghai # 🕰️ 设置时区为中国标准时间 |
| 14 | |
| 15 | jobs: |
| 16 | deploy: |
| 17 | runs-on: ubuntu-latest |
| 18 | steps: |
| 19 | # 1️⃣ 检出 main 分支代码 |
| 20 | - name: 🔍 检查分支 & 拉取代码 |
| 21 | uses: actions/checkout@v4 |
| 22 | with: |
| 23 | ref: main |
| 24 | # 注意:Hexo 源码在 main,public 输出后推送到 page |
| 25 | |
| 26 | # 2️⃣ 缓存 node_modules 以加速安装 |
| 27 | - name: 💾 缓存 Node.js 依赖 |
| 28 | id: cache-node-modules |
| 29 | uses: actions/cache@v4 |
| 30 | with: |
| 31 | path: node_modules |
| 32 | key: ${{ runner.os }}-nodeModules-${{ hashFiles('package-lock.json') }} |
| 33 | restore-keys: | |
| 34 | ${{ runner.os }}-nodeModules- |
| 35 | |
| 36 | # 3️⃣ 安装匹配的 Node.js 版本(支持 v22) |
| 37 | - name: 🟨 安装 Node.js |
| 38 | uses: actions/setup-node@v4 |
| 39 | with: |
| 40 | node-version: "22.x" # ✅ 匹配你本地 v22.17.1,避免版本不一致问题 |
| 41 | cache: 'npm' |
| 42 | |
| 43 | # 4️⃣ 全局安装 hexo-cli(仅一次) |
| 44 | - name: 🛠️ 安装 Hexo CLI |
| 45 | run: npm install -g hexo-cli |
| 46 | |
| 47 | # 5️⃣ 安装项目依赖(跳过缓存命中时) |
| 48 | - name: 📦 安装项目依赖 |
| 49 | if: steps.cache-node-modules.outputs.cache-hit != 'true' |
| 50 | run: npm ci # ✅ 推荐使用 npm ci 替代 npm install,更稳定、更快 |
| 51 | |
| 52 | # 6️⃣ 清理旧生成文件 |
| 53 | - name: 🧹 清理旧文件 |
| 54 | run: npm run clean |
| 55 | |
| 56 | # 7️⃣ 生成静态文件(含压缩) |
| 57 | - name: 🏗️ 生成静态文件并压缩 |
| 58 | run: npm run build |
| 59 | |
| 60 | # 8️⃣ 部署到 page 分支 |
| 61 | - name: 🚀 部署到 GitHub Pages |
| 62 | run: | |
| 63 | cd ./public |
| 64 | git init |
| 65 | git config user.name "${{ github.actor }}" |
| 66 | git config user.email "${{ github.actor }}@users.noreply.github.com" |
| 67 | git add . |
| 68 | git commit -m "🤖 自动部署 Hexo 博客: ${{ github.event.head_commit.message }} ·· [$(date +'%Z %Y-%m-%d %A %H:%M:%S')]" |
| 69 | git push --force --quiet "https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git" master:page |
package.json
· 87 B · Binary
Raw
This file can't be rendered. View the full file.
update-deploy.yml
· 2.7 KiB · YAML
Raw
name: 🚀 自动部署 Hexo 博客
on:
push:
branches:
- main # 🌿 监听 main 分支的推送
release:
types:
- published # 🎉 监听新版本发布
workflow_dispatch: # ⏯️ 支持手动触发
env:
TZ: Asia/Shanghai # 🕰️ 设置时区为中国标准时间
jobs:
deploy:
runs-on: ubuntu-latest
steps:
# 1️⃣ 检出 main 分支源码
- name: 🔍 检查分支 & 拉取代码
uses: actions/checkout@v4
with:
ref: main
# 2️⃣ 缓存 node_modules,加速依赖安装
- name: 💾 缓存 Node.js 依赖
id: cache-node-modules
uses: actions/cache@v4
with:
path: node_modules
key: ${{ runner.os }}-nodeModules-${{ hashFiles('package-lock.json') }}
restore-keys: |
${{ runner.os }}-nodeModules-
# 3️⃣ 安装与本地一致的 Node.js 版本(v22.x)
- name: 🟨 安装 Node.js
uses: actions/setup-node@v4
with:
node-version: "22.x"
cache: 'npm' # 🔁 同时缓存 npm 缓存目录,进一步提速
# 4️⃣ 全局安装 Hexo CLI
- name: 🛠️ 安装 Hexo CLI
run: npm install -g hexo-cli
# 5️⃣ 仅当缓存未命中时安装依赖(避免重复安装)
- name: 📦 安装项目依赖
if: steps.cache-node-modules.outputs.cache-hit != 'true'
run: npm ci # ✅ 更快、更稳定,适合 CI 环境
# 6️⃣ 清理旧的 public 和 .deploy_git 文件
- name: 🧹 清理旧文件
run: npm run clean
# 7️⃣ 生成静态文件(含 HTML/CSS/JS 压缩)
- name: 🏗️ 生成静态文件并压缩
run: npm run build
# 8️⃣ 部署到 GitHub Pages 的 page 分支
- name: 🚀 部署到 GitHub Pages
run: |
cd ./public
git init
git config user.name "${{ github.actor }}"
git config user.email "${{ github.actor }}@users.noreply.github.com"
git add .
# 🔍 检查是否有文件变更(避免空提交)
if git diff --cached --quiet; then
echo "🟩 $(date +'%H:%M:%S') | 🎉 无文件变更,跳过部署"
exit 0
else
echo "🟦 $(date +'%H:%M:%S') | 📄 检测到变更,提交以下文件:"
git diff --cached --name-only
git commit -m "🤖 自动部署: ${{ github.event.head_commit.message }} ·· [$(date +'%Z %Y-%m-%d %A %H:%M:%S')]"
git push --force --quiet "https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git" master:page
echo "💚 $(date +'%H:%M:%S') | 🚀 部署成功"
fi
| 1 | name: 🚀 自动部署 Hexo 博客 |
| 2 | |
| 3 | on: |
| 4 | push: |
| 5 | branches: |
| 6 | - main # 🌿 监听 main 分支的推送 |
| 7 | release: |
| 8 | types: |
| 9 | - published # 🎉 监听新版本发布 |
| 10 | workflow_dispatch: # ⏯️ 支持手动触发 |
| 11 | |
| 12 | env: |
| 13 | TZ: Asia/Shanghai # 🕰️ 设置时区为中国标准时间 |
| 14 | |
| 15 | jobs: |
| 16 | deploy: |
| 17 | runs-on: ubuntu-latest |
| 18 | steps: |
| 19 | # 1️⃣ 检出 main 分支源码 |
| 20 | - name: 🔍 检查分支 & 拉取代码 |
| 21 | uses: actions/checkout@v4 |
| 22 | with: |
| 23 | ref: main |
| 24 | |
| 25 | # 2️⃣ 缓存 node_modules,加速依赖安装 |
| 26 | - name: 💾 缓存 Node.js 依赖 |
| 27 | id: cache-node-modules |
| 28 | uses: actions/cache@v4 |
| 29 | with: |
| 30 | path: node_modules |
| 31 | key: ${{ runner.os }}-nodeModules-${{ hashFiles('package-lock.json') }} |
| 32 | restore-keys: | |
| 33 | ${{ runner.os }}-nodeModules- |
| 34 | |
| 35 | # 3️⃣ 安装与本地一致的 Node.js 版本(v22.x) |
| 36 | - name: 🟨 安装 Node.js |
| 37 | uses: actions/setup-node@v4 |
| 38 | with: |
| 39 | node-version: "22.x" |
| 40 | cache: 'npm' # 🔁 同时缓存 npm 缓存目录,进一步提速 |
| 41 | |
| 42 | # 4️⃣ 全局安装 Hexo CLI |
| 43 | - name: 🛠️ 安装 Hexo CLI |
| 44 | run: npm install -g hexo-cli |
| 45 | |
| 46 | # 5️⃣ 仅当缓存未命中时安装依赖(避免重复安装) |
| 47 | - name: 📦 安装项目依赖 |
| 48 | if: steps.cache-node-modules.outputs.cache-hit != 'true' |
| 49 | run: npm ci # ✅ 更快、更稳定,适合 CI 环境 |
| 50 | |
| 51 | # 6️⃣ 清理旧的 public 和 .deploy_git 文件 |
| 52 | - name: 🧹 清理旧文件 |
| 53 | run: npm run clean |
| 54 | |
| 55 | # 7️⃣ 生成静态文件(含 HTML/CSS/JS 压缩) |
| 56 | - name: 🏗️ 生成静态文件并压缩 |
| 57 | run: npm run build |
| 58 | |
| 59 | # 8️⃣ 部署到 GitHub Pages 的 page 分支 |
| 60 | - name: 🚀 部署到 GitHub Pages |
| 61 | run: | |
| 62 | cd ./public |
| 63 | |
| 64 | git init |
| 65 | git config user.name "${{ github.actor }}" |
| 66 | git config user.email "${{ github.actor }}@users.noreply.github.com" |
| 67 | git add . |
| 68 | |
| 69 | # 🔍 检查是否有文件变更(避免空提交) |
| 70 | if git diff --cached --quiet; then |
| 71 | echo "🟩 $(date +'%H:%M:%S') | 🎉 无文件变更,跳过部署" |
| 72 | exit 0 |
| 73 | else |
| 74 | echo "🟦 $(date +'%H:%M:%S') | 📄 检测到变更,提交以下文件:" |
| 75 | git diff --cached --name-only |
| 76 | git commit -m "🤖 自动部署: ${{ github.event.head_commit.message }} ·· [$(date +'%Z %Y-%m-%d %A %H:%M:%S')]" |
| 77 | git push --force --quiet "https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git" master:page |
| 78 | echo "💚 $(date +'%H:%M:%S') | 🚀 部署成功" |
| 79 | fi |