version: 2
jobs:
  build:
    docker:
      - image: circleci/node:10

    working_directory: ~/workspace

    steps:
      - checkout

      - restore_cache:
          keys:
          - v1-dependencies-{{ checksum "package.json" }}

      - run: npm install

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

      - run: npm test

      - store_test_results:
          path: reports/junit

  publish:
    docker:
      - image: circleci/node:10

    steps:
      - add_ssh_keys
      - checkout

      - run:
          name: Authorize NPM
          command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/.npmrc

      - restore_cache:
          keys:
          - v1-dependencies-{{ checksum "package.json" }}

      - run:
          name: Publish to NPM
          command: npm publish
          
workflows:
  version: 2
  main:
    jobs:
      - build:
          context: org-global
          filters:
            branches:
              only: master
            tags:
              only: /v.*/
      - publish:
          context: org-global
          requires:
            - build
          filters:
            branches:
              ignore: /.*/
            tags:
              only: /v.*/