name: Build and Deploy
on:
  push:
    branches:
      - main

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: https://gitea.cn/actions/checkout@v4
      
      - id: tool-cache
        uses: https://gitee.com/kongxiangyiren/gitea-tool-cache@v5
        with:
          # 只有node支持版本号别名
          node-version: 18
      - uses: https://gitea.cn/actions/setup-node@v4
        with:
          # gitea-tool-cache导出 node 具体版本
          node-version: ${{ steps.tool-cache.outputs.node-version }}
          registry-url: https://registry.npmmirror.com

      - name: 缓存
        uses: https://gitea.cn/actions/cache@v4
        id: cache
        with:
          path: node_modules
          key: ${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}

      - name: 判断yarn是否安装,如果未安装则安装
        run: |
          if ! command -v yarn &> /dev/null; then
            echo "yarn 未安装,开始安装"
            npm install -g yarn
          else
            echo "yarn 已安装"
          fi
          
      - name: Install and Build # 下载依赖 打包项目
        run: |
          yarn install
          yarn build
      - name: Appleboy
        uses: https://gitee.com/liushuai05/scp-action@v0.1.7
        with:
          host: ${{ secrets.USER_HOST }} # 服务器地址: xxx.xxx.xxx.xxx
          username: ${{ secrets.USER_NAME }} # 服务器名字 一般是root
          # key: ${{ secrets.SERVER_SSH_KEY }} # 服务器连接密钥
          password: ${{ secrets.SERVER_PASSWORD }} # 服务器连接密钥
          source: './dist/' # 拷贝文件目录
          target: ${{ secrets.USER_TARGET }} # 服务器目标目录
          strip_components: 2