version: 2

refs:
  container: &container
    docker:
      - image: circleci/node:12.20.1
    working_directory: ~/repo

jobs:
  all:
    <<: *container
    steps:
      - checkout
      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "package.json" }}
            - v1-dependencies-
      - run:
          name: Versions
          command: node -v && npm -v && yarn -v
      - run:
          name: Install dependencies
          command: yarn install
      - save_cache:
          paths:
            - node_modules
          key: v1-dependencies-{{ checksum "package.json" }}
      - run:
          name: Test
          command: yarn test
      - run:
          name: Post to GitHub
          command: npx ci-scripts github-post
      - run:
          name: Post to Slack
          command: npx ci-scripts slack

  master:
    <<: *container
    steps:
      - checkout
      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "package.json" }}
            - v1-dependencies-
      - run:
          name: Versions
          command: node -v && npm -v && yarn -v
      - run:
          name: Install dependencies
          command: yarn install
      - save_cache:
          paths:
            - node_modules
          key: v1-dependencies-{{ checksum "package.json" }}
      - run:
          name: Test
          command: yarn test
      - run:
          name: Post to GitHub
          command: npx ci-scripts github-post
      - run:
          name: Post to Slack
          command: npx ci-scripts slack
      - run:
          name: Release
          command: npx semantic-release

workflows:
  version: 2
  all:
    jobs:
      - all:
          context: common-env-vars
          filters:
            branches:
              ignore:
                - master
  master:
    jobs:
      - master:
          context: common-env-vars
          filters:
            branches:
              only: master
