kind: pipeline
type: docker
name: deploy-test

# 测试环境：test 分支触发，部署到 kpl
trigger:
  branch:
    - test
  event:
    - push

steps:
  - name: sync-code
    image: appleboy/drone-ssh
    settings:
      host: 120.79.247.16
      username: root
      password:
        from_secret: prod_ssh_password
      port: 22
      command_timeout: 5m
      script:
        - echo "=== [测试环境] 同步代码 ==="
        - cd /root/aiedu
        - git fetch origin
        - git checkout test 2>/dev/null || git checkout -b test origin/test
        - git reset --hard origin/test
        - echo "代码同步完成"

  - name: build-frontend-test
    image: appleboy/drone-ssh
    settings:
      host: 120.79.247.16
      username: root
      password:
        from_secret: prod_ssh_password
      port: 22
      command_timeout: 10m
      script:
        - echo "=== [测试环境] 编译前端到 dist-test ==="
        - cd /root/aiedu/kaopeilian-frontend
        - npm install --silent
        - npm run build
        - rm -rf /root/aiedu/dist-test/*
        - cp -r dist/* /root/aiedu/dist-test/
        - echo "前端编译完成 -> dist-test"

  - name: restart-backend
    image: appleboy/drone-ssh
    settings:
      host: 120.79.247.16
      username: root
      password:
        from_secret: prod_ssh_password
      port: 22
      command_timeout: 5m
      script:
        - echo "=== [测试环境] 重启后端 ==="
        - docker restart kpl-backend-dev
        - sleep 5
        - docker ps | grep kpl-
        - echo "=== 测试环境部署完成: https://kpl.ireborn.com.cn ==="

---
kind: pipeline
type: docker
name: deploy-staging

# 预生产环境：staging 分支触发，部署到 aiedu
trigger:
  branch:
    - staging
  event:
    - push

steps:
  - name: sync-code
    image: appleboy/drone-ssh
    settings:
      host: 120.79.247.16
      username: root
      password:
        from_secret: prod_ssh_password
      port: 22
      command_timeout: 5m
      script:
        - echo "=== [预生产] 同步代码 ==="
        - cd /root/aiedu
        - git fetch origin
        - git checkout staging 2>/dev/null || git checkout -b staging origin/staging
        - git reset --hard origin/staging
        - echo "代码同步完成"

  - name: build-frontend-staging
    image: appleboy/drone-ssh
    settings:
      host: 120.79.247.16
      username: root
      password:
        from_secret: prod_ssh_password
      port: 22
      command_timeout: 10m
      script:
        - echo "=== [预生产] 编译前端到 dist-staging ==="
        - cd /root/aiedu/kaopeilian-frontend
        - npm install --silent
        - npm run build
        - rm -rf /root/aiedu/dist-staging/*
        - cp -r dist/* /root/aiedu/dist-staging/
        - echo "前端编译完成 -> dist-staging"

  - name: restart-backend
    image: appleboy/drone-ssh
    settings:
      host: 120.79.247.16
      username: root
      password:
        from_secret: prod_ssh_password
      port: 22
      command_timeout: 5m
      script:
        - echo "=== [预生产] 重启后端 ==="
        - docker restart kaopeilian-backend
        - sleep 5
        - docker ps | grep kaopeilian-
        - echo "=== 预生产部署完成: https://aiedu.ireborn.com.cn ==="

---
kind: pipeline
type: docker
name: deploy-prod

# 生产环境：main 分支触发，部署到所有租户
# 
# 使用方法：
#   git commit -m "feat: xxx [all]"        - 部署所有租户
#   git commit -m "feat: xxx [hua]"        - 仅部署 hua
#   git commit -m "feat: xxx [cxw,yy,hl]"  - 部署指定多个租户
#   git commit -m "feat: xxx"              - 默认部署所有租户
#
trigger:
  branch:
    - main
  event:
    - push

steps:
  - name: sync-code-to-server
    image: appleboy/drone-ssh
    settings:
      host: 120.79.247.16
      username: root
      password:
        from_secret: prod_ssh_password
      port: 22
      command_timeout: 10m
      script:
        - echo "=== [生产] 同步代码 ==="
        - cd /root/aiedu
        - git fetch origin
        - git reset --hard origin/main
        - echo "代码同步完成"

  - name: build-frontend-prod
    image: appleboy/drone-ssh
    settings:
      host: 120.79.247.16
      username: root
      password:
        from_secret: prod_ssh_password
      port: 22
      command_timeout: 10m
      script:
        - echo "=== [生产] 编译前端到 dist-prod ==="
        - cd /root/aiedu/kaopeilian-frontend
        - npm install --silent
        - npm run build
        - rm -rf /root/aiedu/dist-prod/*
        - cp -r dist/* /root/aiedu/dist-prod/
        - echo "前端编译完成 -> dist-prod，所有生产租户已更新"

  - name: deploy-tenants
    image: appleboy/drone-ssh
    settings:
      host: 120.79.247.16
      username: root
      password:
        from_secret: prod_ssh_password
      port: 22
      command_timeout: 15m
      script:
        - echo "=== [生产] 部署租户后端 ==="
        - cd /root/aiedu
        - |
          # 获取 commit message
          COMMIT_MSG="${DRONE_COMMIT_MESSAGE}"
          echo "Commit: $COMMIT_MSG"
          
          # 所有可用租户
          ALL_TENANTS="hua yy hl xy fw ex cxw"
          
          # 解析要部署的租户
          if echo "$COMMIT_MSG" | grep -q '\[all\]'; then
            TENANTS="$ALL_TENANTS"
            echo "部署所有租户: $TENANTS"
          elif echo "$COMMIT_MSG" | grep -oP '\[\K[a-z,]+(?=\])' > /tmp/tenants.txt 2>/dev/null; then
            TENANTS=$(cat /tmp/tenants.txt | tr ',' ' ')
            echo "部署指定租户: $TENANTS"
          else
            TENANTS="$ALL_TENANTS"
            echo "默认部署所有租户: $TENANTS"
          fi
          
          # 构建 docker compose 命令
          BACKEND_SERVICES=""
          for t in $TENANTS; do
            BACKEND_SERVICES="$BACKEND_SERVICES ${t}-backend"
          done
          
          echo "重建后端服务: $BACKEND_SERVICES"
          docker compose -f docker-compose.prod-multi.yml up -d --build --no-deps $BACKEND_SERVICES
          
          sleep 10
          docker ps | grep backend
          echo "=== [生产] 部署完成 ==="
          echo "已更新租户: $TENANTS"

volumes:
  - name: docker-sock
    host:
      path: /var/run/docker.sock

---
kind: pipeline
type: docker
name: code-check

trigger:
  event:
    - push
    - pull_request

steps:
  - name: python-lint
    image: python:3.9-slim
    commands:
      - cd backend
      - pip install flake8 -q
      - flake8 app --count --select=E9,F63,F7,F82 --show-source --statistics || true
      - echo "Python lint completed"

  - name: frontend-check
    image: node:18-alpine
    commands:
      - cd frontend
      - echo "Frontend check completed"
