name: 构建并部署到测试环境（无 SSH）

on:
  push:
    branches:
      - Iteration/7-11
      - dev
      - test

env:
  REGISTRY: registry.boss160.cn
  API_IMAGE: registry.boss160.cn/junhong/cmp-fiber-api
  WORKER_IMAGE: registry.boss160.cn/junhong/cmp-fiber-worker
  DEPLOY_DIR: /opt/junhong_cmp

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    
    steps:
      - name: 检出代码
        run: |
          # 添加 Nix 路径到 PATH（git 安装在这里）
          export PATH="$HOME/.nix-profile/bin:/usr/local/bin:/usr/bin:/bin:$PATH"
          # 跳过 SSL 验证（内网自签名证书）
          export GIT_SSL_NO_VERIFY=1
          git clone https://git.boss160.cn/csxj2026/junhong_cmp_fiber.git .
          git checkout ${{ github.sha }}

      - name: 设置镜像标签
        id: tag
        run: |
          if [ "${{ github.ref }}" = "refs/heads/Iteration/7-11" ]; then
            echo "tag=latest" >> $GITHUB_OUTPUT
          elif [ "${{ github.ref }}" = "refs/heads/dev" ]; then
            echo "tag=dev" >> $GITHUB_OUTPUT
          elif [ "${{ github.ref }}" = "refs/heads/test" ]; then
            echo "tag=test" >> $GITHUB_OUTPUT
          else
            echo "tag=unknown" >> $GITHUB_OUTPUT
          fi

      - name: 登录 Docker Registry
        run: |
          echo "${{ secrets.REGISTRY_PASSWORD }}" | docker login "${{ env.REGISTRY }}" --username "${{ secrets.REGISTRY_USERNAME }}" --password-stdin

      - name: 构建 API 镜像
        run: |
          docker build -f Dockerfile.api -t ${{ env.API_IMAGE }}:${{ steps.tag.outputs.tag }} .
          docker tag ${{ env.API_IMAGE }}:${{ steps.tag.outputs.tag }} ${{ env.API_IMAGE }}:${{ github.sha }}

      - name: 构建 Worker 镜像
        run: |
          docker build -f Dockerfile.worker -t ${{ env.WORKER_IMAGE }}:${{ steps.tag.outputs.tag }} .
          docker tag ${{ env.WORKER_IMAGE }}:${{ steps.tag.outputs.tag }} ${{ env.WORKER_IMAGE }}:${{ github.sha }}

      - name: 推送镜像到 Registry
        run: |
          docker push ${{ env.API_IMAGE }}:${{ steps.tag.outputs.tag }}
          docker push ${{ env.API_IMAGE }}:${{ github.sha }}
          docker push ${{ env.WORKER_IMAGE }}:${{ steps.tag.outputs.tag }}
          docker push ${{ env.WORKER_IMAGE }}:${{ github.sha }}

      - name: 部署到本地（仅 Iteration/7-11 分支）
        if: github.ref == 'refs/heads/Iteration/7-11'
        run: |
          # 确保部署目录存在（仅需日志目录，配置已嵌入二进制文件）
          mkdir -p ${{ env.DEPLOY_DIR }}/logs
          
          # 强制更新 docker-compose.prod.yml（确保使用最新配置）
          echo "📋 更新部署配置文件..."
          cp -v docker-compose.prod.yml ${{ env.DEPLOY_DIR }}/
          
          cd ${{ env.DEPLOY_DIR }}
          
          echo "📥 拉取最新镜像..."
          docker compose -f docker-compose.prod.yml pull
          
          echo "🚀 重启服务..."
          docker compose -f docker-compose.prod.yml up -d
          
          echo "⏳ 等待服务启动..."
          sleep 10
          
          echo "✅ 部署完成！"
          docker compose -f docker-compose.prod.yml ps

      - name: 构建结果通知
        if: always()
        run: |
          if [ "${{ job.status }}" = "success" ]; then
            echo "✅ 构建成功: ${{ steps.tag.outputs.tag }}"
            echo "📦 镜像标签: ${{ github.sha }}"
          else
            echo "❌ 构建失败"
          fi
