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