version: 2
jobs:
  unit test:
    docker:
      - image: cimg/node:lts
        auth:
          username: $DOCKERHUB_USERNAME
          password: $DOCKERHUB_PASSWORD
        environment:
          TZ: "Europe/London"
          PG_CONNECTION_STRING: postgres://postgres:postgres@localhost:5432/circle_test
          TIMEOUT: 15000
      - image: cimg/postgres:15.2-postgis
        auth:
          username: $DOCKERHUB_USERNAME
          password: $DOCKERHUB_PASSWORD
        environment:
          POSTGRES_USER: postgres
          POSTGRES_HOST_AUTH_METHOD: trust
          POSTGRES_DB: circle_test
          TZ: "Europe/London"
    working_directory: ~/repo
    steps:
      - checkout
      - restore_cache:
          keys:
            - v1-deps-{{ checksum "package.json" }}
            - v1-deps-
      - run:
          name: install
          command: |
            npm config set //registry.npmjs.org/:_authToken=$NPM_TOKEN
            npm install
      - save_cache:
          key: v1-deps-{{ checksum "package.json" }}
          paths:
            - node_modules
      - run:
          name: test
          command: npm test
      - run:
          name: codecov
          command: npm run coverage
  lint:
    docker:
      - image: cimg/node:lts
        auth:
          username: $DOCKERHUB_USERNAME
          password: $DOCKERHUB_PASSWORD
    working_directory: ~/repo
    steps:
      - checkout
      - run:
          name: install standard
          command: |
            npm config set //registry.npmjs.org/:_authToken=$NPM_TOKEN
            npm install
      - run:
          name: lint
          command: npm run lint
  release:
    docker:
      - image: cimg/node:lts
        auth:
          username: $DOCKERHUB_USERNAME
          password: $DOCKERHUB_PASSWORD
        environment:
          TZ: "Europe/London"
    working_directory: ~/repo
    steps:
      - checkout
      - restore_cache:
          keys:
            - v1-deps-{{ checksum "package.json" }}
            - v1-deps-
      - run:
          name: install
          command: |
            npm config set //registry.npmjs.org/:_authToken=$NPM_TOKEN
            npm install
      - save_cache:
          key: v1-deps-{{ checksum "package.json" }}
          paths:
            - node_modules
      - run:
          name: release
          command: |
            chmod +x version_blueprint
            npx semantic-release
workflows:
  version: 2
  test_and_release:
    jobs:
    - unit test:
        context:
          - docker-hub-creds
          - build-env-vars
    - lint:
        context:
          - docker-hub-creds
          - build-env-vars
    - release:
        context:
          - docker-hub-creds
          - build-env-vars
        requires:
        - unit test
        - lint
        filters:
          branches:
            only: master
