# GitLab CI/CD 配置 for speakr 项目
# 自动构建并部署到 Kubernetes

stages:
  - build
  - deploy

variables:
  # 使用阿里云镜像仓库
  IMAGE_NAME: crpi-99azmmphmxwdoi76.cn-guangzhou.personal.cr.aliyuncs.com/sea_lee/speakr
  IMAGE_TAG: $CI_COMMIT_SHORT_SHA

# ============================================
# 阶段 1: 构建 Docker 镜像并推送
# ============================================
build:
  stage: build
  image: docker:latest
  services:
    - docker:dind
  before_script:
    # 登录阿里云镜像仓库
    - echo "$ALIYUN_REGISTRY_PASSWORD" | docker login crpi-99azmmphmxwdoi76.cn-guangzhou.personal.cr.aliyuncs.com -u "$ALIYUN_REGISTRY_USER" --password-stdin
  script:
    - echo "=========================================="
    - echo "🚀 开始构建 speakr 镜像"
    - echo "=========================================="
    - echo "Git 提交: $CI_COMMIT_SHORT_SHA"
    - echo "分支: $CI_COMMIT_BRANCH"
    - echo "提交信息: $CI_COMMIT_TITLE"
    - echo "提交者: $GITLAB_USER_NAME"
    - echo ""

    # 显示 Dockerfile 内容
    - echo "Dockerfile:"
    - cat Dockerfile
    - echo ""

    # 构建 Docker 镜像 (使用现有的 Dockerfile)
    - echo "📦 构建 Docker 镜像..."
    - docker build -t $IMAGE_NAME:$IMAGE_TAG .
    - echo "✓ 镜像构建完成"
    - docker images | grep speakr

    # 打标签为 latest
    - docker tag $IMAGE_NAME:$IMAGE_TAG $IMAGE_NAME:latest

    # 推送到阿里云镜像仓库
    - echo ""
    - echo "📤 推送镜像到阿里云..."
    - docker push $IMAGE_NAME:$IMAGE_TAG
    - docker push $IMAGE_NAME:latest
    - echo "✓ 镜像推送完成"

    - echo ""
    - echo "=========================================="
    - echo "✅ 构建阶段完成!"
    - echo "镜像: $IMAGE_NAME:$IMAGE_TAG"
    - echo "=========================================="
  only:
    - main
    - master
    - develop

# ============================================
# 阶段 2: 部署到 Kubernetes
# ============================================
deploy:
  stage: deploy
  image: bitnami/kubectl:latest
  environment:
    name: production
    url: http://172.18.127.124
  before_script:
    # 配置 kubectl
    - echo "🔧 配置 kubectl..."
    - echo "$KUBECONFIG_CONTENT" | base64 -d > /tmp/kubeconfig
    - export KUBECONFIG=/tmp/kubeconfig
    - chmod 600 /tmp/kubeconfig

    # 验证 Kubernetes 连接
    - echo ""
    - echo "验证 Kubernetes 连接..."
    - kubectl config current-context
    - kubectl version --client
    - kubectl get nodes
  script:
    - echo "=========================================="
    - echo "🚀 部署 speakr 到 Kubernetes"
    - echo "=========================================="
    - echo "命名空间: default"
    - echo "Deployment: speakr"
    - echo "镜像: $IMAGE_NAME:$IMAGE_TAG"
    - echo ""

    # 检查 deployment 是否存在
    - |
      if kubectl get deployment speakr &>/dev/null; then
        echo "✓ Deployment 已存在,更新镜像..."
        kubectl set image deployment/speakr speakr=$IMAGE_NAME:$IMAGE_TAG
      else
        echo "✗ Deployment 不存在,创建新的..."
        kubectl create deployment speakr --image=$IMAGE_NAME:$IMAGE_TAG --replicas=2
      fi

    # 等待 Deployment rollout 完成
    - echo ""
    - echo "⏳ 等待部署完成..."
    - kubectl rollout status deployment/speakr --timeout=10m

    # 确保服务存在
    - |
      if ! kubectl get service speakr &>/dev/null; then
        echo "创建 Service..."
        kubectl expose deployment speakr --port=8899 --target-port=8899 --type=NodePort
      fi

    # 显示最终状态
    - echo ""
    - echo "=========================================="
    - echo "✅ 部署完成!"
    - echo "=========================================="
    - kubectl get pods -l app=speakr
    - echo ""
    - kubectl get service speakr
    - echo ""

    # 获取访问地址
    - |
      NODEPORT=$(kubectl get service speakr -o jsonpath='{.spec.ports[0].nodePort}')
      echo "🌐 访问地址:"
      echo "  NodePort: http://172.18.127.124:$NODEPORT"
      echo "  直接访问: http://172.18.127.124:8899"
      echo ""
  only:
    - main
    - master
