version: 2.1
orbs:
  aws-ecr: circleci/aws-ecr@7.0.0

jobs:
  build-and-push:
    machine:
      image: ubuntu-2204:current
    resource_class: medium
    parameters:
      docker-tag:
        type: string
      path:
        type: string
      repo:
        type: string
      dockerfile:
        type: string
    steps:
      - aws-ecr/build-and-push-image:
          checkout: true
          account-url: AWS_ECR_ACCOUNT_URL
          aws-access-key-id: AWS_ACCESS_KEY_ID
          aws-secret-access-key: AWS_SECRET_ACCESS_KEY
          create-repo: false
          dockerfile: <<parameters.dockerfile>>
          path: <<parameters.path>>
          region: AWS_REGION
          repo: <<parameters.repo>>
          tag: '<<parameters.docker-tag>>${CIRCLE_SHA1}'
  deploy:
    machine:
      image: ubuntu-2204:current
    resource_class: medium
    parameters:
      docker-tag:
        type: string
      path:
        type: string
      deploy-name:
        type: string
      deploy-namespace:
        type: string
    steps:
      - run:
          name: kubectl apply
          command: |
            CMD='/home/ubuntu/cluster-for-B/deploy.sh <<parameters.path>> <<parameters.docker-tag>>'${CIRCLE_SHA1}' <<parameters.deploy-name>> <<parameters.deploy-namespace>>'
            echo $CMD
            ssh ${USER_NAME}@${HOST_NAME} ${CMD}

            # 判断正则表达式是否匹配，匹配规则从下面的示例中提取，示例：2023.wk42,2023.wk43
            if [[ "${CIRCLE_BRANCH}" =~ ^([0-9]{4}.wk[0-9]{2},?)+$ ]]; then
              curl -X POST -H "Content-Type: application/json" -d '{"msg_type":"text","content":{"text":"'${CIRCLE_USERNAME}' 触发了正式环境分支 '${CIRCLE_BRANCH}' 更新与部署"}}' https://open.larksuite.com/open-apis/bot/v2/hook/68004e4a-1381-4886-a982-cd77d5f2e6a1
            fi
            # 判断正则表达式是否匹配，匹配规则从下面的示例中提取，示例：canary.2023.wk42,canary.2023.wk43
            if [[ "${CIRCLE_BRANCH}" =~ ^canary.([0-9]{4}.wk[0-9]{2},?)+$ ]]; then
              curl -X POST -H "Content-Type: application/json" -d '{"msg_type":"text","content":{"text":"'${CIRCLE_USERNAME}' 触发了灰度环境分支 '${CIRCLE_BRANCH}' 更新与部署"}}' https://open.larksuite.com/open-apis/bot/v2/hook/68004e4a-1381-4886-a982-cd77d5f2e6a1
            fi
  docker-hub-build-push:
    machine:
      image: ubuntu-2404:current
    resource_class: medium
    parameters:
      repo:
        type: string
      dockerfile:
        type: string
      docker-tag:
        type: string
    steps:
      - checkout
      - run:
          name: Build Docker image
          command: |
            # 检查是否为ARM构建
            if [[ "<<parameters.docker-tag>>" == *"arm64"* ]]; then
              # 设置Docker buildx进行多平台构建
              docker buildx create --use --name multiarch
              docker buildx inspect --bootstrap
              # 构建ARM64架构的镜像
              docker buildx build -t <<parameters.repo>>:<<parameters.docker-tag>> --platform linux/arm64 --no-cache -f <<parameters.dockerfile>> --load .
            else
              # 普通x86构建
              docker build -t <<parameters.repo>>:<<parameters.docker-tag>> --no-cache -f <<parameters.dockerfile>> .
            fi
      - run:
          name: Publish Docker Image to Docker Hub
          command: |
            echo "$DOCKERHUB_PASS" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin
            IMAGE_TAG="0.0.${CIRCLE_BUILD_NUM}"
            docker tag <<parameters.repo>>:<<parameters.docker-tag>> <<parameters.repo>>:$IMAGE_TAG
            docker push <<parameters.repo>>:<<parameters.docker-tag>>
            docker push <<parameters.repo>>:$IMAGE_TAG

workflows:
  version: 2
  backend_build_and_push:
    jobs:
      - build-and-push:
          name: build-for-test
          context:
            - ecr-new
          path: .
          dockerfile: Dockerfile
          repo: catalog-agent
          docker-tag: ''
          filters:
            branches:
              only:
                - master
      - deploy:
          name: deploy-for-test
          docker-tag: ''
          path: '/home/ubuntu/cluster-for-B/gbase-dev/catalog-agent/deploy.yaml'
          deploy-name: catalog-agent
          deploy-namespace: gbase-dev
          context:
            - ecr-new
          filters:
            branches:
              only:
                - master
          requires:
            - build-for-test
      - build-and-push:
          name: build-for-prod
          context:
            - ecr-new
          path: .
          dockerfile: Dockerfile
          repo: catalog-agent
          docker-tag: ''
          filters:
            branches:
              only:
                - prod
      - build-and-push:
          name: build-for-staging
          context:
            - ecr-new
          path: .
          dockerfile: Dockerfile
          repo: catalog-agent
          docker-tag: ''
          filters:
            branches:
              only:
                - staging
      - deploy:
          name: deploy-for-prod
          docker-tag: ''
          path: '/home/ubuntu/cluster-for-B/default/catalog-agent/deploy.yaml'
          deploy-name: catalog-agent
          deploy-namespace: gbase-dev
          context:
            - ecr-new
          filters:
            branches:
              only:
                - prod
          requires:
            - build-for-prod
      - deploy:
          name: deploy-for-staging
          docker-tag: ''
          path: '/home/ubuntu/cluster-for-B/gbase-staging/catalog-agent/deploy.yaml'
          deploy-name: catalog-agent
          deploy-namespace: gbase-staging
          context:
            - ecr-new
          filters:
            branches:
              only:
                - staging
          requires:
            - build-for-staging
      - docker-hub-build-push:
          name: docker-hub-build-push
          repo: gptbasesparticle/catalog-agent
          dockerfile: Dockerfile
          docker-tag: latest
          filters:
            branches:
              only:
                - onprem
      - docker-hub-build-push:
          name: docker-hub-build-push-arm
          repo: gptbasesparticle/catalog-agent
          dockerfile: Dockerfile
          docker-tag: latest_arm64
          filters:
            branches:
              only:
                - onprem
