name: 自动部署

on:
  push:
    branches:
      - main 

  release:
    types:
      - published

  workflow_dispatch:

env:
  TZ: Asia/Shanghai

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
    - name: 检查分支
      uses: actions/checkout@main
      with:
        ref: main

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

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

    - name: 安装 Hexo 及 压缩插件
      run: |
        npm install hexo-cli --global

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

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

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

    - name: 部署
      run: |
        cd ./public
        git init
        git config --global user.name '${{ secrets.GITHUBUSERNAME }}'
        git config --global user.email '${{ secrets.GITHUBEMAIL }}'
        git add .
        git commit -m "${{ github.event.head_commit.message }}··[$(date +"%Z %Y-%m-%d %A %H:%M:%S")]"
        git push --force --quiet "https://${{ secrets.GITHUBUSERNAME }}:${{ secrets.GITEETOKEN }}@gitee.com/${{ secrets.GITHUBUSERNAME }}/blog.git" master:master
        git push --force --quiet "https://${{ secrets.GITHUBUSERNAME }}:${{ secrets.GITHUBTOKEN }}@github.com/${{ secrets.GITHUBUSERNAME }}/liushen-blog.git" master:refs/heads/page