version: 2
jobs:
  build:
    docker:
      - image: node:12.14.1

    dependencies:
      pre:
        - echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc

    steps:
      - checkout

      - run: npm ci

      - save_cache:
          paths:
            - node_modules
          key: v1-dependencies-{{ checksum "package.json" }}

  deploy:
    docker:
      - image: node:12.14.1

    steps:
      - checkout

      - restore_cache:
          keys:
          - v1-dependencies-{{ checksum "package.json" }}
          # fallback to using the latest cache if no exact match is found
          - v1-dependencies-

      - run: npx semantic-release

  lint:
    docker:
      - image: node:12.14.1

    steps:
      - checkout

      - restore_cache:
          keys:
          - v1-dependencies-{{ checksum "package.json" }}
          # fallback to using the latest cache if no exact match is found
          - v1-dependencies-

      - run: npm run lint

  test:
    docker:
      - image: node:12.14.1

    steps:
      - checkout

      - restore_cache:
          keys:
          - v1-dependencies-{{ checksum "package.json" }}
          # fallback to using the latest cache if no exact match is found
          - v1-dependencies-

      - run: npm run test

workflows:
  version: 2
  build_deploy:
    jobs:
      - build:
          context: reaction-publish-semantic-release
      - lint:
          requires:
            - build
      - test:
          requires:
            - build
      - deploy:
          context: reaction-publish-semantic-release
          requires:
            - lint
            - test
          filters:
            branches:
              only: trunk
