kind: pipeline
type: docker
name: build-multiarch-docker

# 匹配 Runner 标签
node:
  runtime: docker
  arch: amd64
  multiarch: true

# 触发条件
trigger:
  include:
    - event: push
      branch: [main, release/*]
    - event: tag

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

clone:
  disable: false
  depth: 1
  tags: true

steps:
  - name: debug docker version
    image: docker:29.0-cli
    commands:
      - docker version

  # 登录容器仓库（使用 docker login）
  - name: registry login
    image: docker:29.0-cli  # 使用官方 Docker CLI 镜像
    volumes:
      - name: docker-sock
        path: /var/run/docker.sock
    environment:
      REGISTRY_SERVER: "swr.cn-north-4.myhuaweicloud.com"
      REGISTRY_USER:
        from_secret: secrets/data/swr_username
      REGISTRY_PASSWORD:
        from_secret: secrets/swr_password
    commands:
      - |
        echo "Logging into registry..."
        echo "=== Debug Variables ==="
        echo "REGISTRY_SERVER: $REGISTRY_SERVER"
        echo "REGISTRY_USER: ${REGISTRY_USER:-empty}"
        echo "REGISTRY_PASSWORD: ${REGISTRY_PASSWORD:-empty}"
        echo "========================"
        echo "$REGISTRY_PASSWORD" | docker login "$REGISTRY_SERVER" -u "$REGISTRY_USER" --password-stdin || {
          echo "Registry login failed!"
          exit 1
        }

  # 构建并推送镜像（使用 docker build/push）
  - name: build and push
    image: docker:29.0-cli
    volumes:
      - name: docker-sock
        path: /var/run/docker.sock
    environment:
      REGISTRY_REPO: "swr.cn-north-4.myhuaweicloud.com/colovu/scratch"
      DRONE_TAG: ${DRONE_TAG}
      DRONE_COMMIT_SHA: ${DRONE_COMMIT_SHA}
      DRONE_BRANCH: ${DRONE_BRANCH}
    commands:
      - |
        set -euo pipefail
        
        if [ -n "${DRONE_TAG:-}" ]; then
          IMAGE_TAG="$DRONE_TAG"
        else
          IMAGE_TAG="${DRONE_COMMIT_SHA:0:8:-unknown}"
        fi
        
        echo "=== Debug Variables ==="
        echo "DRONE_TAG: ${DRONE_TAG:-empty}"
        echo "DRONE_COMMIT_SHA: ${DRONE_COMMIT_SHA:-empty}"
        echo "DRONE_BRANCH: ${DRONE_BRANCH:-empty}"
        echo "REGISTRY_REPO: ${REGISTRY_REPO:-empty}"
        echo "IMAGE_TAG: $IMAGE_TAG"
        echo "========================"
        
        echo "Building image..."
        docker build -t "${REGISTRY_REPO}:${IMAGE_TAG}" .
        
        echo "Pushing image..."
        docker push "${REGISTRY_REPO}:${IMAGE_TAG}"
        
        # 可选：打 latest 标签（仅 main 分支）
        if [ "${DRONE_BRANCH}" = "main" ]; then
          docker tag "${REGISTRY_REPO}:${IMAGE_TAG}" "${REGISTRY_REPO}:latest"
          docker push "${REGISTRY_REPO}:latest"
        fi
        
        echo "Image pushed: ${REGISTRY_REPO}:${IMAGE_TAG}"

  # 清理步骤
  - name: cleanup
    image: docker:29.0-cli
    volumes:
      - name: docker-sock
        path: /var/run/docker.sock
    commands:
      - |
        echo "Logging out of registry..."
        docker logout swr.cn-north-4.myhuaweicloud.com || true
        echo "Cleaning up unused images..."
        docker system prune -af || true
    when:
      status: [success, failure]
