name: Deploy

on:
  push:
    branches:
      - main

jobs:
  test:
    runs-on: macmini
    steps:
      - name: Clone Repository
        run: |
          echo "开始执行任务..."
          echo "当前项目名称: ${{ gitea.repository }}"
          echo "当前项目所有者: ${{ gitea.repository_owner }}"
          REPO_NAME=$(echo ${{ gitea.repository }} | cut -d'/' -f2)
          echo "仓库名称: $REPO_NAME"
          echo "准备执行SSH命令..."
          ssh nicolas@host.docker.internal << EOF
            echo "SSH连接成功"
            cd /Users/nicolas/dockercompose/gitea/repository
            echo "当前目录: \$(pwd)"
            ls -la
            if [ ! -d "${{ gitea.repository }}" ]; then
              echo "开始克隆仓库..."
              git clone ssh://git@localhost:2222/${{ gitea.repository }}.git
              # git clone https://nicowebgl.cn/macmini/gitea/${{ gitea.repository }}.git
            else
              echo "仓库已存在"
            fi
            echo $REPO_NAME
            if [ -d "$REPO_NAME" ]; then
              echo "仓库已存在"
            else
              echo "仓库不存在"
              exit 1
            fi
            cd $REPO_NAME
            echo "进入项目目录: \$(pwd)"
            echo "开始拉取最新代码..."
            git pull
            echo "开始安装依赖..."
            # 加载环境变量
            source ~/.zshrc
            # 检查 npm 命令是否存在
            if ! command -v npm &> /dev/null; then
              echo "npm命令不存在"
              exit 0
              # curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
              # export NVM_DIR="$HOME/.nvm"
              # [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
              # nvm install 18
              # nvm use 18
            fi
            echo "npm 版本: \$(npm -v)"
            npm install
            echo "开始构建项目..."
            npm run build:production
            echo "开始移动文件..."
            mv dist $REPO_NAME
            cp -r $REPO_NAME /Users/nicolas/docker/nginx/html/macmini/project/
            rm -rf $REPO_NAME
            echo "所有操作完成"
            exit 0
          EOF
          echo "SSH命令执行完成"



