<commit_before>defaults: &defaults
  working_directory: ~/noms-digital-studio/licences
  docker:
    - image: circleci/node:8

version: 2
jobs:
  build:
    <<: *defaults
    steps:
      - checkout
      - run:
          name: Update npm
          command: 'sudo npm install -g npm@latest'
      - restore_cache:
          key: dependency-cache-{{ checksum "package-lock.json" }}
      - run:
          name: Install Dependencies
          command: npm ci
      - save_cache:
          key: dependency-cache-{{ checksum "package-lock.json" }}
          paths:
            - node_modules
      - run:
          name: Linter check
          command: npm run lint
      - run:
          command: |
            npm run build
            DATE=$(date '+%Y-%m-%d')
            export BUILD_NUMBER=${DATE}.${CIRCLE_BUILD_NUM}
            export GIT_REF="$CIRCLE_SHA1"
            npm run record-build-info
      - persist_to_workspace:
          root: .
          paths:
            - node_modules
            - build-info.json
            - assets/stylesheets

  test:
    <<: *defaults
    steps:
      - checkout
      - restore_cache:
          key: dependency-cache-{{ checksum "package-lock.json" }}
      - run:
          name: unit tests with coverage
          command: npm run test-with-coverage-ci
          environment:
            MOCHA_FILE: ./reports/test-results.xml
      - store_test_results:
          path: ./reports
      - store_artifacts:
          path: coverage
          destination: coverage

  build_docker:
    <<: *defaults
    steps:
      - checkout
      - attach_workspace:
          at: ~/noms-digital-studio/licences
      - setup_remote_docker:
          exclusive: true
      - run:
          name: Build container and publish to docker hub (master only)
          command: |
             VERSION=$(egrep buildNumber build-info.json  | awk -F"\"" '{print $4}')
             GIT_REF=$(egrep gitRef build-info.json  | awk -F"\"" '{print $8}')
             BUILD_VERSION=${VERSION}

             docker build --rm=false . \
                 --build-arg BUILD_NUMBER=$VERSION \
                 --build-arg GIT_REF=$GIT_REF \
                 --tag "mojdigitalstudio/licences:$BUILD_VERSION" \
                 --label "maintainer=<EMAIL>" \
                 --label "build.number=$CIRCLE_BUILD_NUM" \
                 --label "build.url=$CIRCLE_BUILD_URL" \
                 --label "build.gitref=$GIT_REF"

             docker login -u $DOCKER_USER -p $DOCKER_PASS
             docker tag mojdigitalstudio/licences:$BUILD_VERSION mojdigitalstudio/licences:latest
             docker push mojdigitalstudio/licences:$BUILD_VERSION
             docker push mojdigitalstudio/licences:latest

  deploy_release_to_eb:
      working_directory: ~/noms-digital-studio/licences
      docker:
        - image: mojdigitalstudio/circleci-node-awsebcli
      steps:
        - checkout
        - attach_workspace:
            at: ~/noms-digital-studio/licences
        - deploy:
            name: Deploy to Elastic Beanstalk Dev
            command: |
              VERSION=$(egrep buildNumber build-info.json | awk -F"\"" '{print $4}')
              npm run plant-beanstalk ${VERSION}
              ~/.local/bin/eb deploy --process --verbose --label ${VERSION}
        - persist_to_workspace:
            root: .
            paths:
              - Dockerrun.aws.json

  feature_tests:
    <<: *defaults
    steps:
      - run:
          name: execute feature tests - mock
          command: |
            curl -v -X POST https://circleci.com/api/v1.1/project/github/noms-digital-studio/licences-feature-specs?circle-token=$CIRCLE_TOKEN

  deploy_stage:
      working_directory: ~/noms-digital-studio/licences
      docker:
        - image: mojdigitalstudio/circleci-node-awsebcli
      steps:
        - checkout
        - attach_workspace:
            at: ~/noms-digital-studio/licences
        - deploy:
            name: Deploy to Elastic Beanstalk Stage
            command: |
              VERSION=$(egrep licences Dockerrun.aws.json | awk -F"\"" '{print $4}' |  awk -F":" '{print $2}')
              GIT_REF=$(egrep gitRef build-info.json  | awk -F"\"" '{print $8}')
              git config user.name "<NAME>"
              git config user.email "<EMAIL>"
              git tag -a ${VERSION} ${GIT_REF} -m "${VERSION}"
              git push origin ${VERSION}
              ~/.local/bin/eb deploy licences-stage --label ${VERSION}
        - store_artifacts:
              path: Dockerrun.aws.json

  accept_stage:
    docker:
      - image: circleci/node:8
    working_directory: ~/noms-digital-studio/licences
    steps:
      - run:
          name: execute feature tests - stage
          command: |
            curl -v -X POST --data build_parameters[CIRCLE_JOB]=accept_stage \
              https://circleci.com/api/v1.1/project/github/noms-digital-studio/licences-feature-specs?circle-token=$CIRCLE_TOKEN
workflows:
  version: 2
  build_test_deploy:
    jobs:
      - build
      - test:
          requires:
            - build
      - build_docker:
          requires:
            - test
          filters:
            branches:
              only:
                - master
      - deploy_release_to_eb:
          requires:
            - build_docker
      - feature_tests:
          requires:
            - deploy_release_to_eb
      - hold:
          type: approval
          requires:
            - deploy_release_to_eb
      - deploy_stage:
          requires:
            - hold
      - accept_stage:
          requires:
            - deploy_stage<commit_msg>Create docker image on path branch<commit_after>defaults: &defaults
  working_directory: ~/noms-digital-studio/licences
  docker:
    - image: circleci/node:8

version: 2
jobs:
  build:
    <<: *defaults
    steps:
      - checkout
      - run:
          name: Update npm
          command: 'sudo npm install -g npm@latest'
      - restore_cache:
          key: dependency-cache-{{ checksum "package-lock.json" }}
      - run:
          name: Install Dependencies
          command: npm ci
      - save_cache:
          key: dependency-cache-{{ checksum "package-lock.json" }}
          paths:
            - node_modules
      - run:
          name: Linter check
          command: npm run lint
      - run:
          command: |
            npm run build
            DATE=$(date '+%Y-%m-%d')
            export BUILD_NUMBER=${DATE}.${CIRCLE_BUILD_NUM}
            export GIT_REF="$CIRCLE_SHA1"
            npm run record-build-info
      - persist_to_workspace:
          root: .
          paths:
            - node_modules
            - build-info.json
            - assets/stylesheets

  test:
    <<: *defaults
    steps:
      - checkout
      - restore_cache:
          key: dependency-cache-{{ checksum "package-lock.json" }}
      - run:
          name: unit tests with coverage
          command: npm run test-with-coverage-ci
          environment:
            MOCHA_FILE: ./reports/test-results.xml
      - store_test_results:
          path: ./reports
      - store_artifacts:
          path: coverage
          destination: coverage

  build_docker:
    <<: *defaults
    steps:
      - checkout
      - attach_workspace:
          at: ~/noms-digital-studio/licences
      - setup_remote_docker:
          exclusive: true
      - run:
          name: Build container and publish to docker hub (master only)
          command: |
             VERSION=$(egrep buildNumber build-info.json  | awk -F"\"" '{print $4}')
             GIT_REF=$(egrep gitRef build-info.json  | awk -F"\"" '{print $8}')
             BUILD_VERSION=${VERSION}

             docker build --rm=false . \
                 --build-arg BUILD_NUMBER=$VERSION \
                 --build-arg GIT_REF=$GIT_REF \
                 --tag "mojdigitalstudio/licences:$BUILD_VERSION" \
                 --label "maintainer=<EMAIL>" \
                 --label "build.number=$CIRCLE_BUILD_NUM" \
                 --label "build.url=$CIRCLE_BUILD_URL" \
                 --label "build.gitref=$GIT_REF"

             docker login -u $DOCKER_USER -p $DOCKER_PASS
             docker tag mojdigitalstudio/licences:$BUILD_VERSION mojdigitalstudio/licences:latest
             docker push mojdigitalstudio/licences:$BUILD_VERSION
             docker push mojdigitalstudio/licences:latest

  deploy_release_to_eb:
      working_directory: ~/noms-digital-studio/licences
      docker:
        - image: mojdigitalstudio/circleci-node-awsebcli
      steps:
        - checkout
        - attach_workspace:
            at: ~/noms-digital-studio/licences
        - deploy:
            name: Deploy to Elastic Beanstalk Dev
            command: |
              VERSION=$(egrep buildNumber build-info.json | awk -F"\"" '{print $4}')
              npm run plant-beanstalk ${VERSION}
              ~/.local/bin/eb deploy --process --verbose --label ${VERSION}
        - persist_to_workspace:
            root: .
            paths:
              - Dockerrun.aws.json

  feature_tests:
    <<: *defaults
    steps:
      - run:
          name: execute feature tests - mock
          command: |
            curl -v -X POST https://circleci.com/api/v1.1/project/github/noms-digital-studio/licences-feature-specs?circle-token=$CIRCLE_TOKEN

  deploy_stage:
      working_directory: ~/noms-digital-studio/licences
      docker:
        - image: mojdigitalstudio/circleci-node-awsebcli
      steps:
        - checkout
        - attach_workspace:
            at: ~/noms-digital-studio/licences
        - deploy:
            name: Deploy to Elastic Beanstalk Stage
            command: |
              VERSION=$(egrep licences Dockerrun.aws.json | awk -F"\"" '{print $4}' |  awk -F":" '{print $2}')
              GIT_REF=$(egrep gitRef build-info.json  | awk -F"\"" '{print $8}')
              git config user.name "Circle CI"
              git config user.email "<EMAIL>"
              git tag -a ${VERSION} ${GIT_REF} -m "${VERSION}"
              git push origin ${VERSION}
              ~/.local/bin/eb deploy licences-stage --label ${VERSION}
        - store_artifacts:
              path: Dockerrun.aws.json

  accept_stage:
    docker:
      - image: circleci/node:8
    working_directory: ~/noms-digital-studio/licences
    steps:
      - run:
          name: execute feature tests - stage
          command: |
            curl -v -X POST --data build_parameters[CIRCLE_JOB]=accept_stage \
              https://circleci.com/api/v1.1/project/github/noms-digital-studio/licences-feature-specs?circle-token=$CIRCLE_TOKEN
workflows:
  version: 2
  build_test_deploy:
    jobs:
      - build
      - test:
          requires:
            - build
      - build_docker:
          requires:
            - test
          filters:
            branches:
              only:
                - master
                - patch
      - deploy_release_to_eb:
          requires:
            - build_docker
      - feature_tests:
          requires:
            - deploy_release_to_eb
      - hold:
          type: approval
          requires:
            - deploy_release_to_eb
      - deploy_stage:
          requires:
            - hold
      - accept_stage:
          requires:
            - deploy_stage
{'fraction_non_alphanumeric': 0.10463045414069456, 'fraction_numerical': 0.0026714158504007124, 'mean_word_length': 1.5700228832951946, 'pattern_counts': {'":': 2, '<': 24, '<?xml version=': 0, '>': 8, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, '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': '876962', 'n_tokens_mistral': 3393, 'n_tokens_neox': 3227, 'n_words': 694}