version: 2.1
commands:
  build_and_deploy:
    steps:
      - checkout
      - setup_remote_docker:
          docker_layer_caching: true
      - run:
          name: install and login doctl
          command: |
            sudo apt-get update
            sudo apt install -y gettext-base openssl
            wget https://github.com/digitalocean/doctl/releases/download/v1.45.1/doctl-1.45.1-linux-amd64.tar.gz
            tar xf doctl-1.45.1-linux-amd64.tar.gz
            sudo mv doctl /usr/local/bin
            doctl registry login
      - run:
          name: install kubectl
          command: |
            curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
            chmod +x ./kubectl
            sudo mv ./kubectl /usr/local/bin/kubectl
            sudo apt-get update
            sudo apt-get install apt-transport-https ca-certificates gnupg curl
            curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmor -o /usr/share/keyrings/cloud.google.gpg
            echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
            sudo apt-get update && sudo apt-get install google-cloud-cli && sudo apt-get install google-cloud-cli-gke-gcloud-auth-plugin -y
            echo $GOOGLE_CREDENTIALS > ${HOME}/gcloud-service-key.json
            gcloud auth activate-service-account --key-file=${HOME}/gcloud-service-key.json

      - run:
          name: configure credentials
          command: |
              if [ "$CIRCLE_BRANCH" == "dev-rke" ] || [ "$CIRCLE_BRANCH" == "development" ]; then
              mkdir ~/.kube
              touch ~/.kube/config
              cat \<<EOF> ~/.kube/config
              apiVersion: v1
              clusters:
              - cluster:
                    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUJlRENDQVIrZ0F3SUJBZ0lCQURBS0JnZ3Foa2pPUFFRREFqQWtNU0l3SUFZRFZRUUREQmx5YTJVeUxYTmwKY25abGNpMWpZVUF4TnpBME1EWTJOekV5TUI0WERUSXpNVEl6TVRJek5URTFNbG9YRFRNek1USXlPREl6TlRFMQpNbG93SkRFaU1DQUdBMVVFQXd3WmNtdGxNaTF6WlhKMlpYSXRZMkZBTVRjd05EQTJOamN4TWpCWk1CTUdCeXFHClNNNDlBZ0VHQ0NxR1NNNDlBd0VIQTBJQUJEdzZ1d0xxU3pQWjNUWU1CYURrMmppZklXbmVkQlBGQnZIOG8vVnoKYkdvQUJ4bUFkcTF2Z2dmODN2YWxIVU91QmpWZm1ZQkJsb1lrM1k1bFZHZ3hGeDZqUWpCQU1BNEdBMVVkRHdFQgovd1FFQXdJQ3BEQVBCZ05WSFJNQkFmOEVCVEFEQVFIL01CMEdBMVVkRGdRV0JCU21ScVRzU3hKYlNKU3ZNaGEvCnc2VzVPOHFjMkRBS0JnZ3Foa2pPUFFRREFnTkhBREJFQWlCclZwQXRoZmNreHlvZnc1b3FKcWtmSVliRUc3ajkKNmMrQ05hWlE1NVNBNUFJZ1l1QWs4NjQyK0hrb2dLODljYk1aMHRVVEpRZ1BKZjBVMUVoZzFRVGZsVWs9Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K
                    server: https://rke-k8s.blackdice.ai:6443
                name: default
              contexts:
              - context:
                    cluster: default
                    user: default
                name: default
              current-context: default
              kind: Config
              preferences: {}
              users:
                - name: default
                  user:
                    client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUJrakNDQVRpZ0F3SUJBZ0lJWTJBQVVxbUFIZTB3Q2dZSUtvWkl6ajBFQXdJd0pERWlNQ0FHQTFVRUF3d1oKY210bE1pMWpiR2xsYm5RdFkyRkFNVGN3TkRBMk5qY3hNakFlRncweU16RXlNekV5TXpVeE5USmFGdzB5TkRFeQpNekF5TXpVeE5USmFNREF4RnpBVkJnTlZCQW9URG5ONWMzUmxiVHB0WVhOMFpYSnpNUlV3RXdZRFZRUURFd3h6CmVYTjBaVzA2WVdSdGFXNHdXVEFUQmdjcWhrak9QUUlCQmdncWhrak9QUU1CQndOQ0FBUXVJTVJWc0NGeURXN0sKVm1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srRTl5CjZTckJIUm1IS2JLeERDVUNJQlRzZ1U1VHp0K1pmTDlkbkt5K2YrVU4vRVlsczFmakd1ckpPV1ZIbVYwbwotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg==
                    client-key-data: LS0tLS1CRUdJTiBFQyBQUklWQVRFIEtFWS0tLS0tCk1IY0NBUUVFSU9kK1FNZDN3elQzMEJ4UHEyUmlCUzU2Z1N6ZG9NeGhUUXk3YTJMNzB1RTZvQW9HQ0NxR1NNNDkKQXdFSG9VUURRZ0FFTGlERVZiQWhjZzF1eWxaaUhDbUExNmZiU3VtNG9UTTlUZWhQalhxY2xQancrN0o5ZkJBRgpBaUMxSi9GamxEQnUxNzkwTHNneHhhVGp0TnJaNUZpS3p3PT0KLS0tLS1FTkQgRUMgUFJJVkFURSBLRVktLS0tLQo=
              EOF
              elif [ "$CIRCLE_BRANCH" == "qa" ] || [ "$CIRCLE_BRANCH" == "qa-rke" ]; then
              mkdir ~/.kube
              touch ~/.kube/config
              cat \<<EOF> ~/.kube/config
              apiVersion: v1
              clusters:
              - cluster:
                    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUJlVENDQVIrZ0F3SUJBZ0lCQURBS0JnZ3Foa2pPUFFRREFqQWtNU0l3SUFZRFZRUUREQmx5YTJVeUxYTmwKY25abGNpMWpZVUF4TnpFd01qYzJOalE0TUI0WERUSTBNRE14TWpJd05UQTBPRm9YRFRNME1ETXhNREl3TlRBMApPRm93SkRFaU1DQUdBMVVFQXd3WmNtdGxNaTF6WlhKMlpYSXRZMkZBTVRjeE1ESTNOalkwT0RCWk1CTUdCeXFHClNNNDlBZ0VHQ0NxR1NNNDlBd0VIQTBJQUJBNHZXRGh3WEhzZm9ESWtWS1l0S2xBL1hleTl1TERPaDkrd1FGbGgKYllQWmVpRjd0WTZMVlBFQWQvUXhvbG5CT05EWWZlR3hRQitWYUx3d3RGOUxVMWFqUWpCQU1BNEdBMVVkRHdFQgovd1FFQXdJQ3BEQVBCZ05WSFJNQkFmOEVCVEFEQVFIL01CMEdBMVVkRGdRV0JCUnZzd3N4RjNvNjJyVnRsZWNzCndWTFZiMkxROURBS0JnZ3Foa2pPUFFRREFnTklBREJGQWlCcndxcVY4amhrVFlvTTByWHZpZGtweWREODRYaEYKQkkzV1VFUFh3cGRtcWdJaEFNOEV3bkNnNTdFUm1zRzhWYzdRckw2OTk2MHJORlovUGF2RXpZTmNlOWdLCi0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K
                    server: https://rke-qa.blackdice.ai:6443
                name: default
              contexts:
              - context:
                    cluster: default
                    user: default
                name: default
              current-context: default
              kind: Config
              preferences: {}
              users:
                - name: default
                  user:
                    client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUJrekNDQVRpZ0F3SUJBZ0lJTE00cTcvSElMRmN3Q2dZSUtvWkl6ajBFQXdJd0pERWlNQ0FHQTFVRUF3d1oKY210bE1pMWpiR2xsYm5RdFkyRkFNVGN4TURJM05qWTBPREFlRncweU5EQXpNVEl5TURVd05EaGFGdzB5TlRBegpNVE14TXpNeU1qTmFNREF4RnpBVkJnTlZCQW9URG5ONWMzUmxiVHB0WVhOMFpYSnpNUlV3RXdZRFZRUURFd3h6CmVYTjBaVzA2WVdSdGFXNHdXVEFUQmdjcWhrak9QUUlCQmdncWhrak9QUU1CQndOQ0FBU1VQWVBBdVhUT29wOUkKclM3VGExYmN0ZXVMTVhmdDJOWlJjVDBma2wzRGxjVzdpMFl0UE9LNWh3Vm56QjRTZmVjcHlSVFZON05UQkZPRgpNRWF5RHZENm8wZ3dSakFPQmdOVkhROEJBZjhFQkFNQ0JhQXdFd1lEVlIwbEJBd3dDZ1lJS3dZQkJRVUhBd0l3Ckh3WURWUjBqQkJnd0ZvQVVTcDVWeExjUkMrRUlCcG1XWEFtTnhWanphYVl3Q2dZSUtvWkl6ajBFQXdJRFNRQXcKUmdJaEFMakp0YjdBb0xsaFByL1ZH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B6cUtmU0swdTAydFczTFhyaXpGMzdkaldVWEU5SDVKZHc1WEZ1NHRHTFR6aQp1WWNGWjh3ZUVuM25LY2tVMVRlelV3UlRoVEJHc2c3dytnPT0KLS0tLS1FTkQgRUMgUFJJVkFURSBLRVktLS0tLQo=
              EOF
              elif [ "$CIRCLE_BRANCH" == "staging" ]; then
                export CLUSTER=$STAG_CLUSTER
                doctl kubernetes cluster kubeconfig save $CLUSTER
              elif [ "$CIRCLE_BRANCH" == "gke-staging" ] || [ "$CIRCLE_BRANCH" == "orion" ] || [ "$CIRCLE_BRANCH" == "landatel" ]; then
                gcloud container clusters get-credentials blackdice-stag --region europe-central2 --project gke-blackdice
              elif [ "$CIRCLE_BRANCH" == "gke-pov" ]; then
                gcloud container clusters get-credentials blackdice-pov --region europe-central2 --project gke-blackdice
              elif [ "$CIRCLE_BRANCH" == "production" ]; then
                gcloud container clusters get-credentials blackdice-prod --region europe-central2 --project gke-blackdice
              elif [ -n "${CIRCLE_TAG}" ]; then
               gcloud container clusters get-credentials blackdice-prod --region europe-central2 --project gke-blackdice
              fi
      - run:
          name: build and deploy to cluster
          command: |
            if [ -n "${CIRCLE_TAG}" ]; then
             docker build -t $DO_CONTAINER_REGISTRY/$CIRCLE_PROJECT_REPONAME:${CIRCLE_TAG} .
             docker push $DO_CONTAINER_REGISTRY/$CIRCLE_PROJECT_REPONAME:${CIRCLE_TAG}
             export IMAGE_NAME=$DO_CONTAINER_REGISTRY/$CIRCLE_PROJECT_REPONAME:${CIRCLE_TAG}
            else
              TAG=`echo $CIRCLE_SHA1 | cut -c1-8`
              docker build -t $DO_CONTAINER_REGISTRY/$CIRCLE_PROJECT_REPONAME:$TAG .
              docker push $DO_CONTAINER_REGISTRY/$CIRCLE_PROJECT_REPONAME:$TAG
              export IMAGE_NAME=$DO_CONTAINER_REGISTRY/$CIRCLE_PROJECT_REPONAME:$TAG
            fi

            #Deploy to K8s
            export DEPLOYMENT_NAME=netstar-categorizer

            if [ "$CIRCLE_BRANCH" == "staging" ]; then
              cat deployment/staging/deployment.yaml | envsubst | kubectl apply -f -
            else
              cat deployment/deployment.yaml | envsubst | kubectl apply -f -
            fi


jobs:
  build_and_deploy:
    docker:
      - image: cimg/base:2024.02
    steps:
      - build_and_deploy

workflows:
  version: 2
  build-deploy-dev:
    jobs:
    - build_and_deploy:
        filters:
          branches:
            only: 
            - dev-rke
            - development
            - qa
            - staging
            - production
            - dev-rke
            - gke-staging
            - gke-pov
  circleci-serviceplatform-tags:
    jobs:
      - build_and_deploy:
          filters:
            tags:
              only: /.*/
            branches:
              ignore: /.*/
