<commit_before># This CircleCI config requires the following env vars to be configured
# AWS keys (to push images and trigger stack update)
# see "AWS Permissions" section in your CircleCI project configuration section

# OPENRESTY_REPO_URI - repo uri, do not include version tag
# example: 0000000000.dkr.ecr.us-east-1.amazonaws.com/myapplication/openresty

# Production database connection info
# SUPER_USER - your database admin user
# SUPER_USER_PASSWORD - password for the database admin user
# PRODUCTION_DB_HOST - host uri
# example - myapplication-db.zzzzzzzzz.us-east-1.rds.amazonaws.com:5432
# DB_NAME - database name

version: 2
jobs:
  pull_docker_images:
    machine:
      enabled: true
    steps:
      - restore_cache:
          keys:
          - v2-docker-images
      - run:
          name: docker pull & save
          command: |
            if [ ! -f ~/.docker/images.tar ]; then
              docker pull postgres
              docker pull subzerocloud/postgrest
              docker pull openresty/openresty:jessie
              docker pull lren/pgtap
              # docker pull subzerocloud/pg-amqp-bridge
              # docker pull rabbitmq:3-management
              mkdir -p ~/.docker
              docker save --output ~/.docker/images.tar \
                postgres \
                subzerocloud/postgrest \
                openresty/openresty:jessie \
                lren/pgtap
                # subzerocloud/pg-amqp-bridge \
                # rabbitmq:3-management \
            fi
      - save_cache:
          paths:
            - ~/.docker
          key: v2-docker-images

  install_npm_dependencies:
    docker:
      - image: circleci/node:latest
    steps:
      - checkout
      - restore_cache:
          keys:
          - v2-npm-dependencies-{{ checksum "package.json" }}
      - run: npm install
      - save_cache:
          paths:
            - node_modules
          key: v2-npm-dependencies-{{ checksum "package.json" }}
  test:
    machine:
      enabled: true
    steps:
      - checkout
      - restore_cache:
          keys:
          - v2-docker-images
      - restore_cache:
          keys:
          - v2-npm-dependencies-{{ checksum "package.json" }}
      - run:
          name: load cached docker images
          command: docker load --input ~/.docker/images.tar
      
      - run:
         name: bring up application stack
         command: docker-compose up -d db postgrest openresty && sleep 10

      - run:
          name: run tests
          command: npm test
  
  deploy:
    machine:
      enabled: true
    steps:
      - checkout
      - run:
          name: install dependencies
          command: |
            apt-get install sqitch
      - deploy:
          name: build and push openresty image
          command: |
            docker build -t openresty ./openresty/
            docker tag openresty $OPENRESTY_REPO_URI:$CIRCLE_TAG
            aws ecr get-login --no-include-email | sh
            docker push $OPENRESTY_REPO_URI:$CIRCLE_TAG
      - deploy:
          name: deploy the latest database migrations
          command: |
            cd db/migrations
            # sqitch config --user engine.pg.client /opt/local/pgsql/bin/psql
            sqitch config --user user.name 'CircleCI'
            sqitch config --user user.email '<EMAIL>'
            sqitch target add production db:pg://$SUPER_USER:$SUPER_USER_PASSWORD@$PRODUCTION_DB_HOST/$DB_NAME
            sqitch engine add pg production
            sqitch --verify deploy
      - deploy:
          name: tag openrety image as latest
          command: |
            # this will be fast since all the layers are already in the repository
            docker tag openresty $OPENRESTY_REPO_URI:latest
            docker push $OPENRESTY_REPO_URI:latest
      - deploy:
          name: trigger ECS service update
          command: |
            aws cloudformation update-stack \
              --stack-name $COMPOSE_PROJECT_NAME \
              --capabilities CAPABILITY_IAM \
              --use-previous-template \
              --parameters \
                ParameterKey=ClusterName,UsePreviousValue=true \
                ParameterKey=DesiredCount,ParameterValue=1 \
                ParameterKey=version,ParameterValue=$CIRCLE_TAG \
                ParameterKey=OpenRestyImage,UsePreviousValue=true \
                ParameterKey=ListenerHostNamePattern,UsePreviousValue=true \
                ParameterKey=HasHttpsListener,UsePreviousValue=true \
                ParameterKey=DbHost,UsePreviousValue=true \
                ParameterKey=DbPassword,UsePreviousValue=true \
                ParameterKey=JwtSecret,UsePreviousValue=true
                
                # uncomment the lines below if you changed default values
                # when you created the stack
                # ParameterKey=PostgrestImage,UsePreviousValue=true \
                # ParameterKey=DbPort,UsePreviousValue=true \
                # ParameterKey=DbName,UsePreviousValue=true \
                # ParameterKey=DbSchema,UsePreviousValue=true \
                # ParameterKey=DbUser,UsePreviousValue=true \
                # ParameterKey=DbAnonRole,UsePreviousValue=true \
                # ParameterKey=DbPool,UsePreviousValue=true \
                # ParameterKey=MaxRows,UsePreviousValue=true \
                # ParameterKey=PreRequest,UsePreviousValue=true \
                
  
workflows:
  version: 2
  build-and-deploy:
    jobs:
      - pull_docker_images
      - install_npm_dependencies
      - test:
          requires:
            - pull_docker_images
            - install_npm_dependencies
      - deploy:
          requires:
            - test
          filters:
            tags:
              only: /v[0-9]+(\.[0-9]+)*/
            branches:
              ignore: /.*/


<commit_msg>bugfixes in deploy process<commit_after># This CircleCI config requires the following env vars to be configured
# AWS keys (to push images and trigger stack update)
# see "AWS Permissions" section in your CircleCI project configuration section

# OPENRESTY_REPO_URI - repo uri, do not include version tag
# example: 0000000000.dkr.ecr.us-east-1.amazonaws.com/myapplication/openresty

# Production database connection info
# SUPER_USER - your database admin user
# PRODUCTION_SUPER_USER_PASSWORD - <PASSWORD>
# PRODUCTION_DB_HOST - host uri (ex: myapplication-db.zzzzzzzzz.us-east-1.rds.amazonaws.com:5432)
# DB_NAME - database name
# AWS_REGION - region where you ECS cluster is located (ex: us-east-1)

version: 2
jobs:
  pull_docker_images:
    machine:
      enabled: true
    steps:
      - restore_cache:
          keys:
          - v2-docker-images
      - run:
          name: docker pull & save
          command: |
            if [ ! -f ~/.docker/images.tar ]; then
              docker pull postgres
              docker pull subzerocloud/postgrest
              docker pull openresty/openresty:jessie
              docker pull lren/pgtap
              # docker pull subzerocloud/pg-amqp-bridge
              # docker pull rabbitmq:3-management
              mkdir -p ~/.docker
              docker save --output ~/.docker/images.tar \
                postgres \
                subzerocloud/postgrest \
                openresty/openresty:jessie \
                lren/pgtap
                # subzerocloud/pg-amqp-bridge \
                # rabbitmq:3-management \
            fi
      - save_cache:
          paths:
            - ~/.docker
          key: v2-docker-images

  install_npm_dependencies:
    docker:
      - image: circleci/node:latest
    steps:
      - checkout
      - restore_cache:
          keys:
          - v2-npm-dependencies-{{ checksum "package.json" }}
      - run: npm install
      - save_cache:
          paths:
            - node_modules
          key: v2-npm-dependencies-{{ checksum "package.json" }}
  test:
    machine:
      enabled: true
    steps:
      - checkout
      - restore_cache:
          keys:
          - v2-docker-images
      - restore_cache:
          keys:
          - v2-npm-dependencies-{{ checksum "package.json" }}
      - run:
          name: load cached docker images
          command: docker load --input ~/.docker/images.tar
      
      - run:
         name: bring up application stack
         command: docker-compose up -d db postgrest openresty && sleep 10

      - run:
          name: run tests
          command: npm test
  
  deploy:
    machine:
      enabled: true
      environment:
        PATH: "$PATH:/home/$USER/perl5/bin"
        PERL_CPANM_OPT: "--local-lib=/home/$USER/perl5"
        PERL5LIB: "/home/$USER/perl5/lib/perl5:$PERL5LIB"
    steps:
      - checkout
      - restore_cache:
          keys:
          - v2-deploy-dependencies
      - run:
          name: install dependencies
          command: |
            curl -L https://cpanmin.us | perl - App::cpanminus
            cpanm App::Sqitch DBD::Pg
      - save_cache:
          paths:
            - /home/$USER/perl5
          key: v2-deploy-dependencies
      - deploy:
          name: build and push openresty image
          command: |
            docker build -t openresty ./openresty/
            docker tag openresty $OPENRESTY_REPO_URI:$CIRCLE_TAG
            aws ecr get-login --region $AWS_REGION | sh
            docker push $OPENRESTY_REPO_URI:$CIRCLE_TAG
      - deploy:
          name: deploy the latest database migrations
          command: |
            cd db/migrations
            # sqitch config --user engine.pg.client /opt/local/pgsql/bin/psql
            sqitch config --user user.name 'CircleCI'
            sqitch config --user user.email '<EMAIL>'
            sqitch target add production db:pg://$SUPER_USER:$PRODUCTION_SUPER_USER_PASSWORD@$PRODUCTION_DB_HOST/$DB_NAME
            sqitch engine add pg production
            sqitch deploy
      - deploy:
          name: tag openrety image as latest
          command: |
            # this will be fast since all the layers are already in the repository
            docker tag openresty $OPENRESTY_REPO_URI:latest
            docker push $OPENRESTY_REPO_URI:latest
      - deploy:
          name: trigger ECS service update
          command: |
            aws cloudformation update-stack \
              --region $AWS_REGION \
              --stack-name $COMPOSE_PROJECT_NAME \
              --capabilities CAPABILITY_IAM \
              --use-previous-template \
              --parameters \
                ParameterKey=ClusterName,UsePreviousValue=true \
                ParameterKey=DesiredCount,ParameterValue=1 \
                ParameterKey=Version,ParameterValue=$CIRCLE_TAG \
                ParameterKey=OpenRestyImage,UsePreviousValue=true \
                ParameterKey=ListenerHostNamePattern,UsePreviousValue=true \
                ParameterKey=HasHttpsListener,UsePreviousValue=true \
                ParameterKey=DbHost,UsePreviousValue=true \
                ParameterKey=DbPassword,UsePreviousValue=true \
                ParameterKey=JwtSecret,UsePreviousValue=true
                
                # uncomment the lines below if you changed default values
                # when you created the stack
                # ParameterKey=PostgrestImage,UsePreviousValue=true \
                # ParameterKey=DbPort,UsePreviousValue=true \
                # ParameterKey=DbName,UsePreviousValue=true \
                # ParameterKey=DbSchema,UsePreviousValue=true \
                # ParameterKey=DbUser,UsePreviousValue=true \
                # ParameterKey=DbAnonRole,UsePreviousValue=true \
                # ParameterKey=DbPool,UsePreviousValue=true \
                # ParameterKey=MaxRows,UsePreviousValue=true \
                # ParameterKey=PreRequest,UsePreviousValue=true \
                
  
workflows:
  version: 2
  build-and-deploy:
    jobs:
      - pull_docker_images:
          filters:
            tags:
              only: /v[0-9]+(\.[0-9]+)*/
      - install_npm_dependencies:
          filters:
            tags:
              only: /v[0-9]+(\.[0-9]+)*/
      - test:
          filters:
            tags:
              only: /v[0-9]+(\.[0-9]+)*/
          requires:
            - pull_docker_images
            - install_npm_dependencies
      - deploy:
          requires:
            - test
          filters:
            tags:
              only: /v[0-9]+(\.[0-9]+)*/
            branches:
              ignore: /.*/



{'fraction_non_alphanumeric': 0.0810091891020474, 'fraction_numerical': 0.007093341931323553, 'mean_word_length': 1.6550395891290393, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6395932', 'n_tokens_mistral': 3580, 'n_tokens_neox': 3349, 'n_words': 770}