<commit_before>version: 2.1
commands:
  deployment_setup:
    steps:
      - run:
          name: "Set the Tier"
          command: |
            TIER=DEV
            if [[ ${CIRCLE_BRANCH} =~ (prod|uat|test).* ]]; then
              TIER=$(awk -F[\-_] '{print toupper($1)}' \<<< ${CIRCLE_BRANCH})
            fi
            echo "export TIER=${TIER}" >> $BASH_ENV
            echo "Tier was identified as ${TIER} for branch ${CIRCLE_BRANCH}"
      - run:
          name: "Assign Project-level vars"
          command: |
            if [ ! -f deployment.key.json ]; then
              echo "Deployment JSON keyfile not found - loading from CircleCI."
              KEY=${DEPLOYMENT_KEY_ISB_CGC}
            else
              echo "Deployment JSON keyfile found."
              KEY="NA"
            fi
            CLIENT_EMAIL=${DEPLOYMENT_CLIENT_EMAIL_ISB_CGC}
            PROJECT_ID=${DEPLOYMENT_PROJECT_ID_ISB_CGC}
            BUCKET=${DEPLOYMENT_BUCKET_ISB_CGC}
            if [[ ${TIER} == "PROD" ]] || [[ ${TIER} == "DEV" ]]; then
              BUCKET="${BUCKET}/${TIER,,}"
              echo "Using default project ${PROJECT_ID} and related deployment SA and bucket."
            elif [[ ${TIER} == "UAT" ]]; then
              KEY=${DEPLOYMENT_KEY_ISB_CGC_UAT}
              CLIENT_EMAIL=${DEPLOYMENT_CLIENT_EMAIL_ISB_CGC_UAT}
              PROJECT_ID=${DEPLOYMENT_PROJECT_ID_ISB_CGC_UAT}
              BUCKET=${DEPLOYMENT_BUCKET_ISB_CGC_UAT}
            elif [[ ${TIER} == "TEST" ]]; then
              KEY=${DEPLOYMENT_KEY_ISB_CGC_TEST}
              CLIENT_EMAIL=${DEPLOYMENT_CLIENT_EMAIL_ISB_CGC_TEST}
              PROJECT_ID=${DEPLOYMENT_PROJECT_ID_ISB_CGC_TEST}
              BUCKET=${DEPLOYMENT_BUCKET_ISB_CGC_TEST}
            else
              echo "[ERROR] - Unrecognized tier: ${TIER} - exitng."
              exit 1
            fi

            echo "export DEPLOYMENT_KEY=\"${KEY}\"" >> $BASH_ENV
            echo "export DEPLOYMENT_CLIENT_EMAIL=${CLIENT_EMAIL}" >> $BASH_ENV
            echo "export DEPLOYMENT_PROJECT_ID=${PROJECT_ID}" >> $BASH_ENV
            echo "export DEPLOYMENT_BUCKET=${BUCKET}" >> $BASH_ENV

  deployment_config:
    steps:
      - run:
          name: "Set tier-specific configuration file"
          command: |
            sudo -E /bin/bash ./shell/pull_config.sh
            xargs -a deployment_config.txt -I{} echo "export {}" >> $BASH_ENV

  auth:
    steps:
      - run:
          name: "Service Account Auth and Project Settings"
          command: |
            sudo -E /bin/bash ./shell/gcloud_authenticate.sh

  install_cloud_sdk:
    steps:
      - run:
          name: "Install CloudSDK"
          command: |
            echo "export CLOUDSDK_CORE_DISABLE_PROMPTS=1" >> $BASH_ENV
            echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] http://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
            curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key --keyring /usr/share/keyrings/cloud.google.gpg add -
            sudo apt-get update && sudo apt-get -y --allow-downgrades install google-cloud-sdk=251.0.0-0

jobs:
  build_job:
    environment:
      TZ: "/usr/share/zoneinfo/America/Los_Angeles"
    working_directory: ~/ISB-CGC-API
    docker:
      - image: circleci/python:3.7.0-stretch
      - image: circleci/mysql:5.7
        environment:
          MYSQL_ROOT_HOST: "%"
          MYSQL_USER: ubuntu
          MYSQL_PASSWORD: "<PASSWORD>"
          MYSQL_ROOT_PASSWORD: "<PASSWORD>"
    steps:
      - checkout
      - run:
          name: Set Python Path
          command: |
            echo "export PYTHONPATH=/home/circleci/${CIRCLE_PROJECT_REPONAME}:/home/circleci/${CIRCLE_PROJECT_REPONAME}/lib" >> $BASH_ENV
      - restore_cache:
          keys:
            - isb-cgc-api-lib-{{ checksum "requirements.txt" }}
      - restore_cache:
          keys:
            - isb-cgc-api-google-deps-{{ checksum "shell/install-deps.sh" }}
      - run:
          name: Install Dependencies
          command: |
            sudo -E /bin/bash ./shell/install-deps.sh
      - save_cache:
          key: isb-cgc-api-lib-{{ checksum "requirements.txt" }}
          paths:
            - ./lib
      - deployment_setup
      - auth
      - deployment_config
      - run:
          name: Staging
          command: |
            sudo -E /bin/bash ./shell/gcloud-pull-staging-files.sh
      - save_cache:
          key: isb-cgc-api-{{ epoch }}
          paths:
            - ./apiv4
            - ./shell
            - ./ISB-CGC-Common
            - ./Dockerfile
            - ./app.yaml
            - ./openapi-appengine.yaml
            - ./settings.py
            - ./txt
            - ./json
            - ./.env
      - save_cache:
          key: isb-cgc-api-{{ .Branch }}-{{ .Revision }}-
          paths:
            - ./.env
  deploy_job:
    environment:
      TZ: "/usr/share/zoneinfo/America/Los_Angeles"
    docker:
      - image: circleci/python:3.7.0-stretch
    working_directory: ~/ISB-CGC-API
    steps:
      - restore_cache:
          keys:
            - isb-cgc-api-
      - restore_cache:
          keys:
            - isb-cgc-api-google-deps-
      - install_cloud_sdk
      - deployment_setup
      - auth
      - deployment_config
      - deploy:
          nane: Deployment
          command: |<commit_msg>-> Store branch+revision for main cache to prevent cache collisions across tiers<commit_after>version: 2.1
commands:
  deployment_setup:
    steps:
      - run:
          name: "Set the Tier"
          command: |
            TIER=DEV
            if [[ ${CIRCLE_BRANCH} =~ (prod|uat|test).* ]]; then
              TIER=$(awk -F[\-_] '{print toupper($1)}' \<<< ${CIRCLE_BRANCH})
            fi
            echo "export TIER=${TIER}" >> $BASH_ENV
            echo "Tier was identified as ${TIER} for branch ${CIRCLE_BRANCH}"
      - run:
          name: "Assign Project-level vars"
          command: |
            if [ ! -f deployment.key.json ]; then
              echo "Deployment JSON keyfile not found - loading from CircleCI."
              KEY=${DEPLOYMENT_KEY_ISB_CGC}
            else
              echo "Deployment JSON keyfile found."
              KEY="NA"
            fi
            CLIENT_EMAIL=${DEPLOYMENT_CLIENT_EMAIL_ISB_CGC}
            PROJECT_ID=${DEPLOYMENT_PROJECT_ID_ISB_CGC}
            BUCKET=${DEPLOYMENT_BUCKET_ISB_CGC}
            if [[ ${TIER} == "PROD" ]] || [[ ${TIER} == "DEV" ]]; then
              BUCKET="${BUCKET}/${TIER,,}"
              echo "Using default project ${PROJECT_ID} and related deployment SA and bucket."
            elif [[ ${TIER} == "UAT" ]]; then
              KEY=${DEPLOYMENT_KEY_ISB_CGC_UAT}
              CLIENT_EMAIL=${DEPLOYMENT_CLIENT_EMAIL_ISB_CGC_UAT}
              PROJECT_ID=${DEPLOYMENT_PROJECT_ID_ISB_CGC_UAT}
              BUCKET=${DEPLOYMENT_BUCKET_ISB_CGC_UAT}
            elif [[ ${TIER} == "TEST" ]]; then
              KEY=${DEPLOYMENT_KEY_ISB_CGC_TEST}
              CLIENT_EMAIL=${DEPLOYMENT_CLIENT_EMAIL_ISB_CGC_TEST}
              PROJECT_ID=${DEPLOYMENT_PROJECT_ID_ISB_CGC_TEST}
              BUCKET=${DEPLOYMENT_BUCKET_ISB_CGC_TEST}
            else
              echo "[ERROR] - Unrecognized tier: ${TIER} - exitng."
              exit 1
            fi

            echo "export DEPLOYMENT_KEY=\"${KEY}\"" >> $BASH_ENV
            echo "export DEPLOYMENT_CLIENT_EMAIL=${CLIENT_EMAIL}" >> $BASH_ENV
            echo "export DEPLOYMENT_PROJECT_ID=${PROJECT_ID}" >> $BASH_ENV
            echo "export DEPLOYMENT_BUCKET=${BUCKET}" >> $BASH_ENV

  deployment_config:
    steps:
      - run:
          name: "Set tier-specific configuration file"
          command: |
            sudo -E /bin/bash ./shell/pull_config.sh
            xargs -a deployment_config.txt -I{} echo "export {}" >> $BASH_ENV

  auth:
    steps:
      - run:
          name: "Service Account Auth and Project Settings"
          command: |
            sudo -E /bin/bash ./shell/gcloud_authenticate.sh

  install_cloud_sdk:
    steps:
      - run:
          name: "Install CloudSDK"
          command: |
            echo "export CLOUDSDK_CORE_DISABLE_PROMPTS=1" >> $BASH_ENV
            echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] http://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
            curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key --keyring /usr/share/keyrings/cloud.google.gpg add -
            sudo apt-get update && sudo apt-get -y --allow-downgrades install google-cloud-sdk=251.0.0-0

jobs:
  build_job:
    environment:
      TZ: "/usr/share/zoneinfo/America/Los_Angeles"
    working_directory: ~/ISB-CGC-API
    docker:
      - image: circleci/python:3.7.0-stretch
      - image: circleci/mysql:5.7
        environment:
          MYSQL_ROOT_HOST: "%"
          MYSQL_USER: ubuntu
          MYSQL_PASSWORD: "<PASSWORD>"
          MYSQL_ROOT_PASSWORD: "<PASSWORD>"
    steps:
      - checkout
      - run:
          name: Set Python Path
          command: |
            echo "export PYTHONPATH=/home/circleci/${CIRCLE_PROJECT_REPONAME}:/home/circleci/${CIRCLE_PROJECT_REPONAME}/lib" >> $BASH_ENV
      - restore_cache:
          keys:
            - isb-cgc-api-lib-{{ checksum "requirements.txt" }}
      - restore_cache:
          keys:
            - isb-cgc-api-google-deps-{{ checksum "shell/install-deps.sh" }}
      - run:
          name: Install Dependencies
          command: |
            sudo -E /bin/bash ./shell/install-deps.sh
      - save_cache:
          key: isb-cgc-api-lib-{{ checksum "requirements.txt" }}
          paths:
            - ./lib
      - deployment_setup
      - auth
      - deployment_config
      - run:
          name: Staging
          command: |
            sudo -E /bin/bash ./shell/gcloud-pull-staging-files.sh
      - save_cache:
          key: isb-cgc-api-{{ .Branch }}-{{ .Revision }}-{{ epoch }}
          paths:
            - ./apiv4
            - ./shell
            - ./ISB-CGC-Common
            - ./Dockerfile
            - ./app.yaml
            - ./openapi-appengine.yaml
            - ./settings.py
            - ./txt
            - ./json
            - ./.env
  deploy_job:
    environment:
      TZ: "/usr/share/zoneinfo/America/Los_Angeles"
    docker:
      - image: circleci/python:3.7.0-stretch
    working_directory: ~/ISB-CGC-API
    steps:
      - restore_cache:
          keys:
            - isb-cgc-api-{{ .Branch }}-{{ .Revision }}-
      - restore_cache:
          keys:
            - isb-cgc-api-google-deps-
      - install_cloud_sdk
      - deployment_setup
      - auth
      - deployment_config
      - deploy:
          nane: Deployment
          command: |
{'fraction_non_alphanumeric': 0.12361948955916473, 'fraction_numerical': 0.0037122969837587007, 'mean_word_length': 1.6899650524213679, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 40, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2839698', 'n_tokens_mistral': 3623, 'n_tokens_neox': 3545, 'n_words': 634}