kind: pipeline
type: docker
name: build-and-push-test

# 测试环境：test 分支触发，构建并推送到 ACR
trigger:
  branch:
    - test
  event:
    - push

steps:
  # 构建并推送后端镜像
  - name: build-push-backend
    image: docker:dind
    volumes:
      - name: docker-sock
        path: /var/run/docker.sock
    environment:
      DOCKER_REGISTRY:
        from_secret: docker_registry
      DOCKER_USERNAME:
        from_secret: docker_username
      DOCKER_PASSWORD:
        from_secret: docker_password
    commands:
      - echo "$DOCKER_PASSWORD" | docker login "$DOCKER_REGISTRY" -u "$DOCKER_USERNAME" --password-stdin
      - cd backend
      - docker build -t $DOCKER_REGISTRY/ireborn/kaopeilian-backend:test -f Dockerfile .
      - docker tag $DOCKER_REGISTRY/ireborn/kaopeilian-backend:test $DOCKER_REGISTRY/ireborn/kaopeilian-backend:${DRONE_COMMIT_SHA:0:8}
      - docker push $DOCKER_REGISTRY/ireborn/kaopeilian-backend:test
      - docker push $DOCKER_REGISTRY/ireborn/kaopeilian-backend:${DRONE_COMMIT_SHA:0:8}
      - echo "后端镜像推送完成"

  # 构建并推送前端镜像
  - name: build-push-frontend
    image: docker:dind
    volumes:
      - name: docker-sock
        path: /var/run/docker.sock
    environment:
      DOCKER_REGISTRY:
        from_secret: docker_registry
      DOCKER_USERNAME:
        from_secret: docker_username
      DOCKER_PASSWORD:
        from_secret: docker_password
    commands:
      - echo "$DOCKER_PASSWORD" | docker login "$DOCKER_REGISTRY" -u "$DOCKER_USERNAME" --password-stdin
      - cd frontend
      - docker build -t $DOCKER_REGISTRY/ireborn/kaopeilian-frontend:test -f Dockerfile --target production --build-arg VITE_API_BASE_URL=https://kpl.ireborn.com.cn .
      - docker tag $DOCKER_REGISTRY/ireborn/kaopeilian-frontend:test $DOCKER_REGISTRY/ireborn/kaopeilian-frontend:${DRONE_COMMIT_SHA:0:8}
      - docker push $DOCKER_REGISTRY/ireborn/kaopeilian-frontend:test
      - docker push $DOCKER_REGISTRY/ireborn/kaopeilian-frontend:${DRONE_COMMIT_SHA:0:8}
      - echo "前端镜像推送完成"

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

---
kind: pipeline
type: docker
name: build-and-push-staging

# 预生产环境：staging 分支触发
trigger:
  branch:
    - staging
  event:
    - push

steps:
  - name: build-push-backend
    image: docker:dind
    volumes:
      - name: docker-sock
        path: /var/run/docker.sock
    environment:
      DOCKER_REGISTRY:
        from_secret: docker_registry
      DOCKER_USERNAME:
        from_secret: docker_username
      DOCKER_PASSWORD:
        from_secret: docker_password
    commands:
      - echo "$DOCKER_PASSWORD" | docker login "$DOCKER_REGISTRY" -u "$DOCKER_USERNAME" --password-stdin
      - cd backend
      - docker build -t $DOCKER_REGISTRY/ireborn/kaopeilian-backend:staging -f Dockerfile .
      - docker push $DOCKER_REGISTRY/ireborn/kaopeilian-backend:staging

  - name: build-push-frontend
    image: docker:dind
    volumes:
      - name: docker-sock
        path: /var/run/docker.sock
    environment:
      DOCKER_REGISTRY:
        from_secret: docker_registry
      DOCKER_USERNAME:
        from_secret: docker_username
      DOCKER_PASSWORD:
        from_secret: docker_password
    commands:
      - echo "$DOCKER_PASSWORD" | docker login "$DOCKER_REGISTRY" -u "$DOCKER_USERNAME" --password-stdin
      - cd frontend
      - docker build -t $DOCKER_REGISTRY/ireborn/kaopeilian-frontend:staging -f Dockerfile --target production --build-arg VITE_API_BASE_URL=https://aiedu.ireborn.com.cn .
      - docker push $DOCKER_REGISTRY/ireborn/kaopeilian-frontend:staging

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

---
kind: pipeline
type: docker
name: build-and-push-prod

# 生产环境：main 分支触发
trigger:
  branch:
    - main
  event:
    - push

steps:
  - name: build-push-backend
    image: docker:dind
    volumes:
      - name: docker-sock
        path: /var/run/docker.sock
    environment:
      DOCKER_REGISTRY:
        from_secret: docker_registry
      DOCKER_USERNAME:
        from_secret: docker_username
      DOCKER_PASSWORD:
        from_secret: docker_password
    commands:
      - echo "$DOCKER_PASSWORD" | docker login "$DOCKER_REGISTRY" -u "$DOCKER_USERNAME" --password-stdin
      - cd backend
      - docker build -t $DOCKER_REGISTRY/ireborn/kaopeilian-backend:main -f Dockerfile .
      - docker tag $DOCKER_REGISTRY/ireborn/kaopeilian-backend:main $DOCKER_REGISTRY/ireborn/kaopeilian-backend:latest
      - docker push $DOCKER_REGISTRY/ireborn/kaopeilian-backend:main
      - docker push $DOCKER_REGISTRY/ireborn/kaopeilian-backend:latest

  - name: build-push-frontend
    image: docker:dind
    volumes:
      - name: docker-sock
        path: /var/run/docker.sock
    environment:
      DOCKER_REGISTRY:
        from_secret: docker_registry
      DOCKER_USERNAME:
        from_secret: docker_username
      DOCKER_PASSWORD:
        from_secret: docker_password
    commands:
      - echo "$DOCKER_PASSWORD" | docker login "$DOCKER_REGISTRY" -u "$DOCKER_USERNAME" --password-stdin
      - cd frontend
      - docker build -t $DOCKER_REGISTRY/ireborn/kaopeilian-frontend:main -f Dockerfile --target production --build-arg VITE_API_BASE_URL=https://hua.ireborn.com.cn .
      - docker tag $DOCKER_REGISTRY/ireborn/kaopeilian-frontend:main $DOCKER_REGISTRY/ireborn/kaopeilian-frontend:latest
      - docker push $DOCKER_REGISTRY/ireborn/kaopeilian-frontend:main
      - docker push $DOCKER_REGISTRY/ireborn/kaopeilian-frontend:latest

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