version: 2

defaults: &defaults
  working_directory: ~/repo
  docker:
    - image: circleci/node:10.2.1

jobs:
  test:
    <<: *defaults
    steps:
      - checkout

      - run: yarn
      - run:
          name: Run tests unit
          command: yarn test

      - save_cache:
          paths:
            - node_modules
          key: v1-dependencies-

  build:
    <<: *defaults
    steps:
      - checkout

      - run: yarn
      - run:
          name: Build package
          command: yarn build

      - persist_to_workspace:
          root: ~/repo
          paths: .

  release:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/repo
      - run:
          name: release
          command: yarn run semantic-release || true
      - run:
          name: Notify on Slack
          when: on_success
          command: yarn notify $(git log --format=oneline -n 1)
      - run:
          name: Notify on Slack
          when: on_fail
          command: yarn notify $(git log --format=oneline -n 1) error

workflows:
  version: 2
  test-deploy:
    jobs:
      - test
      - build
      - release:
          requires:
            - test
            - build
          filters:
            branches:
              only: /^master$/
