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