kind: pipeline
type: docker
name: yd-yunxing-server # 服务名成，可与jar包名保持一致

steps:

  - name: maven-build
    image: maven:3.8.5-openjdk-17
    volumes:
      - name: maven-cache
        path: /root/.m2
      - name: build-output
        path: /home/app/build
    commands:
      - cd /drone/src
      - mvn clean package -DskipTests=true -pl yudao-gateway,yudao-module-system/yudao-module-system-biz,yudao-module-infra/yudao-module-infra-biz -am

  - name: copy-jars
    image: alpine
    volumes:
      - name: build-output
        path: /home/app/build
    commands:
      - mkdir -p /home/app/build/gateway
      - mkdir -p /home/app/build/system
      - mkdir -p /home/app/build/infra
      - ls -l yudao-gateway/target/ # 检查源文件是否存在
      - cp yudao-gateway/target/yudao-gateway.jar /home/app/build/gateway/app.jar || echo "Failed to copy gateway jar"
      - cp yudao-module-system/yudao-module-system-biz/target/yudao-module-system-biz.jar /home/app/build/system/app.jar || echo "Failed to copy system biz jar"
      - cp yudao-module-infra/yudao-module-infra-biz/target/yudao-module-infra-biz.jar /home/app/build/infra/app.jar || echo "Failed to copy infra biz jar"
      - cp Dockerfile /home/app/build/
      - cp docker-compose.yml /home/app/build/

  - name: build-docker-images
    image: docker
    volumes:
      - name: build-output
        path: /home/app/build
      - name: docker
        path: /var/run/docker.sock
    commands:
      - # 停止容器（如果存在）
      - docker inspect --type=container yudao-gateway >/dev/null 2>&1 && docker stop yudao-gateway || true
      - docker inspect --type=container yudao-module-system >/dev/null 2>&1 && docker stop yudao-module-system || true
      - docker inspect --type=container yudao-module-infra >/dev/null 2>&1 && docker stop yudao-module-infra || true

      - # 删除容器（如果存在）
      - docker inspect --type=container yudao-gateway >/dev/null 2>&1 && docker rm yudao-gateway || true
      - docker inspect --type=container yudao-module-system >/dev/null 2>&1 && docker rm yudao-module-system || true
      - docker inspect --type=container yudao-module-infra >/dev/null 2>&1 && docker rm yudao-module-infra || true

      - # 删除镜像（如果存在）
      - docker inspect --type=image yudao-gateway >/dev/null 2>&1 && docker rmi yudao-gateway || true
      - docker inspect --type=image yudao-module-system >/dev/null 2>&1 && docker rmi yudao-module-system || true
      - docker inspect --type=image yudao-module-infra >/dev/null 2>&1 && docker rmi yudao-module-infra || true

      - cd /home/app/build
      - cd gateway && docker build -t yudao-gateway:v1.0 -f ../Dockerfile .
      - cd ../system && docker build -t yudao-module-system:v1.0 -f ../Dockerfile .
      - cd ../infra && docker build -t yudao-module-infra:v1.0 -f ../Dockerfile .

  - name: deploy-with-compose
    image: docker/compose:1.29.2
    environment:
      APP_VERSION: v1.0
    volumes:
      - name: build-output
        path: /home/app/build
      - name: docker
        path: /var/run/docker.sock
    commands:
      - cd /home/app/build
      - sed -i "s/v1.0/v1.0/g" docker-compose.yml
      - docker-compose -f docker-compose.yml up -d

volumes:
  - name: maven-cache
    host:
      path: /home/data/maven/cache
  - name: build-output
    host:
      path: /home/data/maven/build
  - name: docker
    host:
      path: /var/run/docker.sock