version: 2.1

defaults: &defaults
  working_directory: ~/grommet-ci
  docker:
    - image: circleci/node:10

jobs:
  checkout:
    <<: *defaults
    steps:
      - checkout
      - restore_cache:
          keys:
            - yarn-cache-{{ checksum "yarn.lock" }}
      - run: yarn --frozen-lockfile
      - save_cache:
          key: yarn-cache-{{ checksum "yarn.lock" }}
          paths:
            - ~/.cache
            - ./node_modules
      - persist_to_workspace:
          root: ~/grommet-ci
          paths:
            - ./
  lint:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/grommet-ci
      - run:
          name: Running linter
          command: yarn lint
  build:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/grommet-ci
      - run:
          name: Build
          command: yarn build
      - persist_to_workspace:
          root: ~/grommet-ci
          paths:
            - ./
  publish:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/grommet-ci
      - run:
          name: Authenticate with registry
          command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/grommet-ci/.npmrc
      - run:
          name: Publish package
          command: npm publish
workflows:
  build_test:
    jobs:
      - checkout:
          filters:
            tags:
              only: /.*/
      - lint:
          requires:
            - checkout
          filters:
            tags:
              only: /.*/
      - build:
          requires:
            - checkout
          filters:
            tags:
              only: /.*/
      - publish:
          requires:
            - lint
            - build
          filters:
            tags:
              only: /^v.*/
            branches:
              ignore: /.*/
