version: 2.1

orbs:
  sixrs-gke: sixriversystems/sixrs-gke@7
  gcp-cli: circleci/gcp-cli@3

aliases:
  - &node_image cimg/node:16.19.1
  - &npm_global_cache dependency-cache-v1-{{ checksum ".node-version" }}-{{ checksum "package-lock.json" }}

jobs:
  setup:
    docker:
      - image: *node_image
        environment:
          NODE_ENV: test
    steps:
      - checkout
      - restore_cache:
          keys:
            - *npm_global_cache
      - run:
          name: Install node dependencies
          command: |
            npm ci
      - save_cache:
          key: *npm_global_cache
          paths:
            - ~/.npm
      - persist_to_workspace:
          root: ./
          paths:
            - ./node_modules
            - ./dist
  publish:
    docker:
      - image: *node_image
        environment:
          NODE_ENV: test
    steps:
      - checkout
      - attach_workspace:
          at: ./
      - sixrs-gke/run-release
      - sixrs-gke/npm-public-publish
  test:
    docker:
      - image: *node_image
        environment:
          NODE_ENV: test
          MOCHA_OPTS: --reporter mocha-junit-reporter --reporter-options mochaFile=./reports/junit/mocha/mocha.xml
    steps:
      - checkout
      - attach_workspace:
          at: ./
      - run:
          name: npm test
          command: |
            npm test
      - run:
          name: npm run coverage
          command: |
            npm run coverage
      - sixrs-gke/codecov
      - store_artifacts:
          path: coverage
      - store_artifacts:
          path: reports/junit/
      - store_test_results:
          path: reports/junit/

workflows:
  version: 2
  test_and_publish:
    jobs:
      - setup
      - test:
          # codecov orb step needs internal tokens
          context: 6rs-circle
          requires:
            - setup
      - publish:
          context: 6rs-public-npm
          requires:
            - test
