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