<commit_before>version: 2.1

orbs:
  python: circleci/python@0.2.1

jobs:
  backend-build:
    machine:
      image: ubuntu-1604:201903-01
    steps:
      - checkout
      # restore cache and docker layer cache
      - restore_cache:
          keys:
            - jarrback-{{ .Environment.CACHE_VERSION }}.{{ .Branch }}
          paths:
            - ~/caches/jarr-server.tar
            - ~/caches/jarr-worker.tar
      - run:
          name: Loading docker image layer caches
          command: |
            set +o pipefail
            docker load -i ~/caches/jarr-server.tar | true
            docker load -i ~/caches/jarr-worker.tar | true
      - run:
          name: Building images for server and worker
          command: |
            mkdir -p ~/caches
            make build-server
            make build-worker
      - run:
          name: Saving docker image layer caches
          command: |
            docker save -o ~/caches/jarr-server.tar jarr-server
            docker save -o ~/caches/jarr-worker.tar jarr-worker
      - save_cache:
          key: jarrback-{{ .Environment.CACHE_VERSION }}.{{ .Branch }}-{{ epoch }}
          paths:
            - ~/caches/jarr-server.tar
            - ~/caches/jarr-worker.tar

  front-build:
    machine:
      image: ubuntu-1604:201903-01
    steps:
      - checkout
      # restore cache and docker layer cache
      - restore_cache:
          keys:
            - jarrfront-{{ .Environment.CACHE_VERSION }}.{{ .Branch }}
          paths:
            - ~/caches/jarr-front.tar
      - run:
          name: Loading docker image layer caches
          command: |
            set +o pipefail
            docker load -i ~/caches/jarr-front.tar | true
      - run:
          name: Building images for front
          command: |
            mkdir -p ~/caches
            make build-front PUBLIC_URL=https://app.jarr.info REACT_APP_API_URL=https://api.jarr.info
      - run:
          name: Saving docker image layer caches
          command: |
            docker save -o ~/caches/jarr-front.tar jarr-front
      - save_cache:
          key: jarrfront-{{ .Environment.CACHE_VERSION }}.{{ .Branch }}-{{ epoch }}
          paths:
            - ~/caches/jarr-front.tar

  backend-test:
    machine:
      image: ubuntu-1604:201903-01
    steps:
      - checkout
      # restore cache and docker layer cache
      - restore_cache:
          keys:
            - jarrback-{{ .Environment.CACHE_VERSION }}.{{ .Branch }}
          paths:
            - ~/caches/jarr-server.tar
            - ~/caches/jarr-worker.tar
      - run:
          name: Loading docker image layer caches
          command: |
            docker load -i ~/caches/jarr-server.tar
            docker load -i ~/caches/jarr-worker.tar
      - run:
          name: Moving test config to default config
          command: |
            mkdir -p ~/coverage_results
            rm example_conf/jarr.test.json example_conf/jarr.json
            mv example_conf/jarr.circleci.json example_conf/jarr.json
      - run:
          name: Starting test env (postgres)
          command: |
            docker-compose --file Dockerfiles/circleci-env.yml up -d
            sleep 10s
      - run:
          name: Creating postgres database
          command: >-
            docker-compose --file Dockerfiles/circleci-env.yml
            exec postgresql su postgres -c
            "createdb jarr --no-password"
      - run:
          name: Populating database with tables
          command: >-
            docker-compose --file Dockerfiles/circleci-env.yml
            exec server bash -c "pipenv run ./manager.py db_create"
      - run:
          name: Installing testing dependencies
          command: >-
            docker-compose --file Dockerfiles/circleci-env.yml
            exec server bash -c "pipenv sync --dev --bare"
      - run:
          name: Running tests
          command: >-
            docker-compose --file Dockerfiles/circleci-env.yml
            exec server bash -c
            "pipenv run nosetests tests/ -vv --with-coverage --cover-package=jarr"
      - run:
          name: Uplaoding coverage to codacy
          command: >-
            docker-compose --file Dockerfiles/circleci-env.yml
            exec -u root server bash -c "chown -R jarr:jarr /jarr/coverage_results";
            docker-compose --file Dockerfiles/circleci-env.yml
            exec server bash -c
            "pipenv run coverage xml -o coverage_results/coverage.xml";
            pip install --upgrade codacy-coverage;
            python-codacy-coverage -r ~/coverage_results/coverage.xml
      - run:
          name: Running PEP8 linter
          command: >-
            docker-compose --file Dockerfiles/circleci-env.yml
            exec server bash -c
            "pipenv run pycodestyle --ignore=E126,E127,E128,W503 jarr/ --exclude=jarr/migrations"
      - run:
          name: Running MyPy linter
          command: >-
            docker-compose --file Dockerfiles/circleci-env.yml
            exec server bash -c
            "pipenv run mypy jarr --ignore-missing-imports --exclude=jarr/crawler/lib/__init__.py"

  push-docker-images:
    machine:
      image: ubuntu-1604:201903-01
    environment:
      GIT_TAG: << pipeline.git.tag >>
    steps:
      - restore_cache:
          keys:
            - jarrback-{{ .Environment.CACHE_VERSION }}.{{ .Branch }}
          paths:
            - ~/caches/jarr-server.tar
            - ~/caches/jarr-worker.tar
      - restore_cache:
          keys:
            - jarrfront-{{ .Environment.CACHE_VERSION }}.{{ .Branch }}
          paths:
            - ~/caches/jarr-front.tar
      - run:
          name: Loading ALL docker image layer caches
          command: |
            set +o pipefail
            docker load -i ~/caches/jarr-server.tar
            docker load -i ~/caches/jarr-worker.tar
            docker load -i ~/caches/jarr-front.tar
      - run:
          name: Pushing tested images to registry
          command: |
            echo "$DOCKER_PASS" | docker login --username $DOCKER_USER --password-stdin
            if [ "$GIT_TAG" != "" ]; then
              export DOCKER_TAG=$GIT_TAG
            else
              export DOCKER_TAG=latest
            fi
            docker image tag jarr-server $DOCKER_USER/jarr-server:$DOCKER_TAG
            docker image tag jarr-worker $DOCKER_USER/jarr-worker:$DOCKER_TAG
            docker image tag jarr-front $DOCKER_USER/jarr-front:$DOCKER_TAG
            docker push $DOCKER_USER/jarr-server:$DOCKER_TAG
            docker push $DOCKER_USER/jarr-worker:$DOCKER_TAG
            docker push $DOCKER_USER/jarr-front:$DOCKER_TAG

workflows:
  jarr-testing:
    jobs:
      - backend-build:
          filters:
            tags:
              only: /.*/
      - front-build:
          filters:
            tags:
              only: /.*/
      - backend-test:<commit_msg>Fixing docker push for latest tag<commit_after>version: 2.1

orbs:
  python: circleci/python@0.2.1

jobs:
  backend-build:
    machine:
      image: ubuntu-1604:201903-01
    steps:
      - checkout
      # restore cache and docker layer cache
      - restore_cache:
          keys:
            - jarrback-{{ .Environment.CACHE_VERSION }}.{{ .Branch }}
          paths:
            - ~/caches/jarr-server.tar
            - ~/caches/jarr-worker.tar
      - run:
          name: Loading docker image layer caches
          command: |
            set +o pipefail
            docker load -i ~/caches/jarr-server.tar | true
            docker load -i ~/caches/jarr-worker.tar | true
      - run:
          name: Building images for server and worker
          command: |
            mkdir -p ~/caches
            make build-server
            make build-worker
      - run:
          name: Saving docker image layer caches
          command: |
            docker save -o ~/caches/jarr-server.tar jarr-server
            docker save -o ~/caches/jarr-worker.tar jarr-worker
      - save_cache:
          key: jarrback-{{ .Environment.CACHE_VERSION }}.{{ .Branch }}-{{ epoch }}
          paths:
            - ~/caches/jarr-server.tar
            - ~/caches/jarr-worker.tar

  front-build:
    machine:
      image: ubuntu-1604:201903-01
    steps:
      - checkout
      # restore cache and docker layer cache
      - restore_cache:
          keys:
            - jarrfront-{{ .Environment.CACHE_VERSION }}.{{ .Branch }}
          paths:
            - ~/caches/jarr-front.tar
      - run:
          name: Loading docker image layer caches
          command: |
            set +o pipefail
            docker load -i ~/caches/jarr-front.tar | true
      - run:
          name: Building images for front
          command: |
            mkdir -p ~/caches
            make build-front PUBLIC_URL=https://app.jarr.info REACT_APP_API_URL=https://api.jarr.info
      - run:
          name: Saving docker image layer caches
          command: |
            docker save -o ~/caches/jarr-front.tar jarr-front
      - save_cache:
          key: jarrfront-{{ .Environment.CACHE_VERSION }}.{{ .Branch }}-{{ epoch }}
          paths:
            - ~/caches/jarr-front.tar

  backend-test:
    machine:
      image: ubuntu-1604:201903-01
    steps:
      - checkout
      # restore cache and docker layer cache
      - restore_cache:
          keys:
            - jarrback-{{ .Environment.CACHE_VERSION }}.{{ .Branch }}
          paths:
            - ~/caches/jarr-server.tar
            - ~/caches/jarr-worker.tar
      - run:
          name: Loading docker image layer caches
          command: |
            docker load -i ~/caches/jarr-server.tar
            docker load -i ~/caches/jarr-worker.tar
      - run:
          name: Moving test config to default config
          command: |
            mkdir -p ~/coverage_results
            rm example_conf/jarr.test.json example_conf/jarr.json
            mv example_conf/jarr.circleci.json example_conf/jarr.json
      - run:
          name: Starting test env (postgres)
          command: |
            docker-compose --file Dockerfiles/circleci-env.yml up -d
            sleep 10s
      - run:
          name: Creating postgres database
          command: >-
            docker-compose --file Dockerfiles/circleci-env.yml
            exec postgresql su postgres -c
            "createdb jarr --no-password"
      - run:
          name: Populating database with tables
          command: >-
            docker-compose --file Dockerfiles/circleci-env.yml
            exec server bash -c "pipenv run ./manager.py db_create"
      - run:
          name: Installing testing dependencies
          command: >-
            docker-compose --file Dockerfiles/circleci-env.yml
            exec server bash -c "pipenv sync --dev --bare"
      - run:
          name: Running tests
          command: >-
            docker-compose --file Dockerfiles/circleci-env.yml
            exec server bash -c
            "pipenv run nosetests tests/ -vv --with-coverage --cover-package=jarr"
      - run:
          name: Uplaoding coverage to codacy
          command: >-
            docker-compose --file Dockerfiles/circleci-env.yml
            exec -u root server bash -c "chown -R jarr:jarr /jarr/coverage_results";
            docker-compose --file Dockerfiles/circleci-env.yml
            exec server bash -c
            "pipenv run coverage xml -o coverage_results/coverage.xml";
            pip install --upgrade codacy-coverage;
            python-codacy-coverage -r ~/coverage_results/coverage.xml
      - run:
          name: Running PEP8 linter
          command: >-
            docker-compose --file Dockerfiles/circleci-env.yml
            exec server bash -c
            "pipenv run pycodestyle --ignore=E126,E127,E128,W503 jarr/ --exclude=jarr/migrations"
      - run:
          name: Running MyPy linter
          command: >-
            docker-compose --file Dockerfiles/circleci-env.yml
            exec server bash -c
            "pipenv run mypy jarr --ignore-missing-imports --exclude=jarr/crawler/lib/__init__.py"

  push-docker-images:
    machine:
      image: ubuntu-1604:201903-01
    environment:
      GIT_TAG: << pipeline.git.tag >>
    steps:
      - restore_cache:
          keys:
            - jarrback-{{ .Environment.CACHE_VERSION }}.{{ .Branch }}
          paths:
            - ~/caches/jarr-server.tar
            - ~/caches/jarr-worker.tar
      - restore_cache:
          keys:
            - jarrfront-{{ .Environment.CACHE_VERSION }}.{{ .Branch }}
          paths:
            - ~/caches/jarr-front.tar
      - run:
          name: Loading ALL docker image layer caches
          command: |
            set +o pipefail
            docker load -i ~/caches/jarr-server.tar
            docker load -i ~/caches/jarr-worker.tar
            docker load -i ~/caches/jarr-front.tar
      - run:
          name: Pushing tested images to registry
          command: |
            echo "$DOCKER_PASS" | docker login --username $DOCKER_USER --password-stdin
            if [ "$GIT_TAG" != "<nil>" ]; then
              export DOCKER_TAG=$GIT_TAG
            else
              export DOCKER_TAG=latest
            fi
            docker image tag jarr-server $DOCKER_USER/jarr-server:$DOCKER_TAG
            docker image tag jarr-worker $DOCKER_USER/jarr-worker:$DOCKER_TAG
            docker image tag jarr-front $DOCKER_USER/jarr-front:$DOCKER_TAG
            docker push $DOCKER_USER/jarr-server:$DOCKER_TAG
            docker push $DOCKER_USER/jarr-worker:$DOCKER_TAG
            docker push $DOCKER_USER/jarr-front:$DOCKER_TAG

workflows:
  jarr-testing:
    jobs:
      - backend-build:
          filters:
            tags:
              only: /.*/
      - front-build:
          filters:
            tags:
              only: /.*/
      - backend-test:
{'fraction_non_alphanumeric': 0.10055947104555693, 'fraction_numerical': 0.00988156651892756, 'mean_word_length': 1.5190336749633968, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 22, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '4077961', 'n_tokens_mistral': 4322, 'n_tokens_neox': 4048, 'n_words': 973}