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