name: 自动部署

on:
  push:
    branches:
      - master

  release:
    types:
      - published

  workflow_dispatch:

env:
  TZ: Asia/Shanghai

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
    - name: 检查分支
      uses: actions/checkout@v5
      with:
        ref: master
        token: ${{ secrets.GITHUB_TOKEN }}

    - name: 缓存项目 npm 包
      id: cache-node-modules
      uses: actions/cache@v4
      with:
        path: node_modules
        key: ${{ runner.os }}-nodeModules-${{ hashFiles('package-lock.json') }}-${{ hashFiles('package.json') }}
        restore-keys: |
          ${{ runner.os }}-nodeModules-

    - name: 安装 Node
      uses: actions/setup-node@v4
      with:
        node-version: "22.x"

    - name: 安装 Hexo
      run: |
        npm install hexo-cli --global

    - name: 安装依赖
      if: steps.cache-node-modules.outputs.cache-hit != 'true'
      run: |
        npm install

    - name: 清理文件树
      run: |
        npm run clean

    - name: 生成静态文件并压缩
      run: |
        npm run build

    - name: 部署
      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 "${{ 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
        
    - name: 服务器执行拉取命令
      uses: appleboy/ssh-action@v1
      with:
        host: ${{ secrets.SERVER_IP }}
        username: ${{ secrets.USERNAME }}
        key: ${{ secrets.KEY }}
        port: ${{ secrets.PORT }}
        script: |
          cd /opt/www/sites/blog/index/
          git config --global --add safe.directory "$(pwd)"
          git fetch --all --depth=1
          git reset --hard origin/page
          git pull --depth=1
          echo "✅ 已拉取 page 分支最新内容"
