options:
  docker: true

image: node:lts-alpine

definitions:
  services:
    docker:
      memory: 4096
pipelines:
  tags:
    '*.*.*-stag':
      - step:
          size: 2x
          image: google/cloud-sdk:latest
          name: Build Image
          caches:
            - docker
          script:
            # Generate Image Name from git tag
            - export TAG=$(echo "$BITBUCKET_TAG" | sed -e "s/v//g")
            - export SUFFIX=$([[ ! -z "$TAG" ]] && echo ":$TAG" || echo "")
            - export IMAGE_NAME=gcr.io/emr-v2/emrv2-frontend-stag${SUFFIX}
            # GCP
            - echo $GCR_GKE_KEY > ~/.gcloud-api-key.json
            - gcloud auth activate-service-account --key-file ~/.gcloud-api-key.json
            - gcloud config set project emr-v2
            - gcloud container clusters get-credentials emrv2-staging --zone=asia-southeast1 --project emr-v2
            - gcloud auth configure-docker --quiet
            # BUILD IMAGE
            - docker build --build-arg APP_ENV=test -t $IMAGE_NAME .
            # PUBLISH IMAGE
            - docker push $IMAGE_NAME
            # DEPLOYMENT
            # - kubectl set image deployment/emrv2-stag-frontend emrv2-stag-frontend=$IMAGE_NAME
      - step:
          size: 2x
          image: google/cloud-sdk:latest
          name: Deploy to SG Cluster
          deployment: staging
          script:
            # Generate Image Name from git tag and set is current image
            - export TAG=$(echo "$BITBUCKET_TAG" | sed -e "s/v//g")
            - export SUFFIX=$([[ ! -z "$TAG" ]] && echo ":$TAG" || echo "")
            - export IMAGE_NAME=gcr.io/emr-v2/emrv2-frontend-stag${SUFFIX}
            - sed -i "s|{{image}}|$IMAGE_NAME|g" stag-sg-deployment.yml
            # GCP LOGIN
            - echo $GCR_GKE_KEY > ~/.gcloud-api-key.json
            - gcloud auth activate-service-account --key-file ~/.gcloud-api-key.json
            - gcloud config set project emr-v2
            - gcloud container clusters get-credentials emrv2-staging --zone=asia-southeast1 --project emr-v2
            # Deploy
            - kubectl apply -f stag-sg-deployment.yml
    '*.*.*-prod':
      - step:
          size: 2x
          image: google/cloud-sdk:latest
          name: Build Image to SG Registry
          caches:
            - docker
          script:
            # Generate Image Name from git tag and set is current image
            - export TAG=$(echo "$BITBUCKET_TAG" | sed -e "s/v//g")
            - export SUFFIX=$([[ ! -z "$TAG" ]] && echo ":$TAG" || echo "")
            - export IMAGE_NAME=gcr.io/emr-v2/emrv2-frontend-prod${SUFFIX}
            # GCP
            - echo $GCR_GKE_KEY > ~/.gcloud-api-key.json
            - gcloud auth activate-service-account --key-file ~/.gcloud-api-key.json
            - gcloud config set project emr-v2
            - gcloud container clusters get-credentials emrv2-production --zone=asia-southeast1 --project emr-v2
            - gcloud auth configure-docker --quiet
            # BUILD IMAGE
            - docker build --build-arg APP_ENV=production -t $IMAGE_NAME .
            # PUBLISH IMAGE
            - docker push $IMAGE_NAME
            # DEPLOYMENT
            # - kubectl set image -n default deployment/emrv2-prod-frontend emrv2-prod-frontend=$IMAGE_NAME
      - step:
          size: 2x
          image: google/cloud-sdk:latest
          name: Deploy to SG Cluster
          deployment: production
          script:
            # Generate Image Name from git tag and set is current image
            - export TAG=$(echo "$BITBUCKET_TAG" | sed -e "s/v//g")
            - export SUFFIX=$([[ ! -z "$TAG" ]] && echo ":$TAG" || echo "")
            - export IMAGE_NAME=gcr.io/emr-v2/emrv2-frontend-prod${SUFFIX}
            - sed -i "s|{{image}}|$IMAGE_NAME|g" prod-sg-deployment.yml
            # GCP Login
            - echo $GCR_GKE_KEY > ~/.gcloud-api-key.json
            - gcloud auth activate-service-account --key-file ~/.gcloud-api-key.json
            - gcloud config set project emr-v2
            - gcloud container clusters get-credentials emrv2-production --zone=asia-southeast1 --project emr-v2
            # Deploy
            - kubectl apply -f prod-sg-deployment.yml
      - step:
          size: 2x
          image: google/cloud-sdk:latest
          name: Build Image to China Registry
          caches:
            - docker
          script:
            # Generate Image Name from git tag and set is current image
            - export TAG=$(echo "$BITBUCKET_TAG" | sed -e "s/v//g")
            - export SUFFIX=$([[ ! -z "$TAG" ]] && echo ":$TAG" || echo "")
            - export IMAGE_NAME=registry-intl.cn-hongkong.aliyuncs.com/alicloud-emrv2/dh-fe-prod-cn${SUFFIX}
            # Build and Push to China Registry
            - docker build --build-arg APP_ENV=production -t $IMAGE_NAME -f china-prod.Dockerfile .
            - echo "$ALICLOUD_REGISTRY_PASSWORD" | docker login --username=kristhian@5147111765705870 --password-stdin registry-intl.cn-hongkong.aliyuncs.com
            - docker push $IMAGE_NAME
      - step:
          size: 2x
          name: Deploy to Shanghai Cluster
          deployment: production-cn
          script:
            # Generate Image Name from git tag and set is current image
            - export TAG=$(echo "$BITBUCKET_TAG" | sed -e "s/v//g")
            - export SUFFIX=$([[ ! -z "$TAG" ]] && echo ":$TAG" || echo "")
            - export IMAGE_NAME=registry-intl.cn-hongkong.aliyuncs.com/alicloud-emrv2/dh-fe-prod-cn${SUFFIX}
            - sed -i "s|{{image}}|$IMAGE_NAME|g" prod-cn-deployment.yml
            - pipe: atlassian/kubectl-run:1.1.2
              variables:
                KUBE_CONFIG: $KUBE_CONFIG_PRODCN
                KUBECTL_COMMAND: 'apply'
                RESOURCE_PATH: 'prod-cn-deployment.yml'    
  branches:
    staging-cn:
      - step:
          size: 2x
          image: google/cloud-sdk:latest
          name: Build and Deploy Shanghai Cluster
          deployment: test
          caches:
            - docker
          script:
            # SETUP
            - export IMAGE_NAME=registry-intl.cn-hongkong.aliyuncs.com/alicloud-emrv2/dh-fe-stag-cn:$BITBUCKET_COMMIT
            # BUILD IMAGE
            - docker build --build-arg APP_ENV=test -t $IMAGE_NAME -f china-stag.Dockerfile .
            # PUBLISH IMAGE
            - docker login --username=kristhian@5147111765705870 --password=$ALICLOUD_REGISTRY_PASSWORD registry-intl.cn-hongkong.aliyuncs.com
            - docker push $IMAGE_NAME
            # DEPLOYMENT
            # - kubectl set image deployment/emrv2-stag-frontend emrv2-stag-frontend=$IMAGE_NAME
      - step:
          size: 2x
          name: Deploy to Shanghai Cluster
          deployment: staging-cn
          script:
            - sed -i "s|{{image}}|registry-intl.cn-hongkong.aliyuncs.com/alicloud-emrv2/dh-fe-stag-cn:$BITBUCKET_COMMIT|g" stag-cn-deployment.yml
            - pipe: atlassian/kubectl-run:1.1.2
              variables:
                KUBE_CONFIG: $KUBE_CONFIG_STAGCN
                KUBECTL_COMMAND: 'apply'
                RESOURCE_PATH: 'stag-cn-deployment.yml'
    master-cn:
      - step:
          size: 2x
          image: google/cloud-sdk:latest
          name: Build Image to China Registry
          caches:
            - docker
          script:
            # SETUP
            - export IMAGE_NAME=registry-intl.cn-hongkong.aliyuncs.com/alicloud-emrv2/dh-fe-prod-cn:$BITBUCKET_COMMIT
            # BUILD IMAGE
            - docker build --build-arg APP_ENV=production -t $IMAGE_NAME -f china-prod.Dockerfile .
            # PUBLISH IMAGE
            - docker login --username=kristhian@5147111765705870 --password=$ALICLOUD_REGISTRY_PASSWORD registry-intl.cn-hongkong.aliyuncs.com
            - docker push $IMAGE_NAME
            # DEPLOYMENT
            # - kubectl set image -n default deployment/emrv2-prod-frontend emrv2-prod-frontend=$IMAGE_NAME
      - step:
          size: 2x
          name: Deploy to Shanghai Cluster
          deployment: production
          script:
            - sed -i "s|{{image}}|registry-intl.cn-hongkong.aliyuncs.com/alicloud-emrv2/dh-fe-prod-cn:$BITBUCKET_COMMIT|g" prod-cn-deployment.yml
            - pipe: atlassian/kubectl-run:1.1.2
              variables:
                KUBE_CONFIG: $KUBE_CONFIG_PRODCN
                KUBECTL_COMMAND: 'apply'
                RESOURCE_PATH: 'prod-cn-deployment.yml'
    staging-backup:
      - step:
          size: 2x
          image: google/cloud-sdk:latest
          name: Build and Deploy
          deployment: staging
          caches:
            - docker
          script:
            # SETUP
            - export IMAGE_NAME=gcr.io/emr-v2/emrv2-frontend-stag:$BITBUCKET_COMMIT
            - echo $GCR_GKE_KEY > ~/.gcloud-api-key.json
            - gcloud auth activate-service-account --key-file ~/.gcloud-api-key.json
            - gcloud config set project emr-v2
            - gcloud container clusters get-credentials emrv2-staging --zone=asia-southeast1 --project emr-v2
            - gcloud auth configure-docker --quiet
            # BUILD IMAGE
            - docker build --build-arg APP_ENV=test -t $IMAGE_NAME .
            # PUBLISH IMAGE
            - docker push $IMAGE_NAME
            # DEPLOYMENT
            - kubectl set image deployment/emrv2-stag-frontend emrv2-stag-frontend=$IMAGE_NAME
    master-backup:
      - step:
          size: 2x
          image: google/cloud-sdk:latest
          name: Build and Deploy
          deployment: production
          caches:
            - docker
          script:
            # SETUP
            - export IMAGE_NAME=gcr.io/emr-v2/emrv2-frontend-prod:$BITBUCKET_COMMIT
            - echo $GCR_GKE_KEY > ~/.gcloud-api-key.json
            - gcloud auth activate-service-account --key-file ~/.gcloud-api-key.json
            - gcloud config set project emr-v2
            - gcloud container clusters get-credentials emrv2-production --zone=asia-southeast1 --project emr-v2
            - gcloud auth configure-docker --quiet
            # BUILD IMAGE
            - docker build --build-arg APP_ENV=production -t $IMAGE_NAME .
            # PUBLISH IMAGE
            - docker push $IMAGE_NAME
            # DEPLOYMENT
            - kubectl set image -n default deployment/emrv2-prod-frontend emrv2-prod-frontend=$IMAGE_NAME
      - step:
          size: 2x
          image: google/cloud-sdk:latest
          name: Build Image to China Registry
          caches:
            - docker
          script:
            - export IMAGE_NAME=registry-intl.cn-hongkong.aliyuncs.com/alicloud-emrv2/dh-fe-prod-cn:$BITBUCKET_COMMIT
            - docker build --build-arg APP_ENV=production -t $IMAGE_NAME -f china-prod.Dockerfile .
            - docker login --username=kristhian@5147111765705870 --password=$ALICLOUD_REGISTRY_PASSWORD registry-intl.cn-hongkong.aliyuncs.com
            - docker push $IMAGE_NAME
      - step:
          size: 2x
          name: Deploy to Shanghai Cluster
          deployment: production-cn
          script:
            - sed -i "s|{{image}}|registry-intl.cn-hongkong.aliyuncs.com/alicloud-emrv2/dh-fe-prod-cn:$BITBUCKET_COMMIT|g" prod-cn-deployment.yml
            - pipe: atlassian/kubectl-run:1.1.2
              variables:
                KUBE_CONFIG: $KUBE_CONFIG_PRODCN
                KUBECTL_COMMAND: 'apply'
                RESOURCE_PATH: 'prod-cn-deployment.yml'