version: 2.1

# -------------------------------------------------------------------------------------------------
# PIPELINE PARAMS
# -------------------------------------------------------------------------------------------------

parameters:
  cache-version:
    type: string
    default: v1

# -------------------------------------------------------------------------------------------------
# ORBS
# -------------------------------------------------------------------------------------------------

orbs:
  orb-tools: circleci/orb-tools@9.1.1

# -------------------------------------------------------------------------------------------------
# ANCHORS
# -------------------------------------------------------------------------------------------------

workflow-devbranch_filters: &workflow-devbranch_filters
  branches:
    ignore:
      - master
      - main
workflow-masterbranch_filters: &workflow-masterbranch_filters
  branches:
    only:
      - master
      - main
workflow-tag_filters: &workflow-tag_filters
  branches:
    ignore: /.*/
  tags:
    only: /^v.*/

# -------------------------------------------------------------------------------------------------
# EXECUTORS
# -------------------------------------------------------------------------------------------------

executors:
  node:
    docker:
      - image: circleci/node:14.17

# -------------------------------------------------------------------------------------------------
# JOBS
# -------------------------------------------------------------------------------------------------

jobs:
  dependencies:
    executor: node
    steps:
      - checkout
      - restore_cache:
          keys:
            - << pipeline.parameters.cache-version >>-dependencies-{{ arch }}-{{ checksum "yarn.lock" }}
      - run:
          name: Authenticate with registry
          command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > .npmrc
      - run:
          name: Install dependencies
          command: yarn --prefer-offline --pure-lockfile --ignore-engines
      - save_cache:
          key: << pipeline.parameters.cache-version >>-dependencies-{{ arch }}-{{ checksum "yarn.lock" }}
          paths:
            - node_modules
  release:
    executor: node
    steps:
      - checkout
      - restore_cache:
          key: << pipeline.parameters.cache-version >>-dependencies-{{ arch }}-{{ checksum "yarn.lock" }}
      - run:
          name: Setup origin with write access
          command: git remote set-url origin "https://michael-robot:$GITHUB_TOKEN@github.com/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME"
      - run:
          name: Create version with semantic-release
          command: yarn run semantic-release

# -------------------------------------------------------------------------------------------------
# WORKFLOWS
# -------------------------------------------------------------------------------------------------

workflows:
  main-workflow:
    jobs:
      - orb-tools/lint
      - orb-tools/pack
      - dependencies:
          context: npm
          filters: *workflow-masterbranch_filters
      - hold-publish-dev:
          type: approval
          filters: *workflow-devbranch_filters
      - orb-tools/publish-dev:
          context: orb-release
          orb-name: ornikar/frontend-orb
          requires:
            - orb-tools/lint
            - orb-tools/pack
            - hold-publish-dev
          filters: *workflow-devbranch_filters
      - hold-release:
          type: approval
          filters: *workflow-masterbranch_filters
      - release:
          context: github
          requires:
            - orb-tools/lint
            - orb-tools/pack
            - dependencies
            - hold-release
          filters: *workflow-masterbranch_filters
  # publish-prod:
  #   jobs:
  #     - orb-tools/pack:
  #         filters: *workflow-tag_filters
  #     - orb-tools/publish:
  #         checkout: false
  #         attach-workspace: true
  #         orb-ref: "ornikar/frontend-orb@${CIRCLE_TAG:1}"
  #         context: orb-release
  #         requires:
  #           - orb-tools/pack
  #         filters: *workflow-tag_filters
