

executors:
  node-executor:
    working_directory: ~/repo
    docker:
      - image: circleci/node:14.16.1
        environment:
          EXTRA_INDEX_URL: "InjectedDuringRuntime"
          AWS_ECR_DOMAIN: "InjectedDuringRuntime"
          JFROG_AUTH: "InjectedDuringRuntime"
          APOLLO_ENGINE_API_KEY: "InjectedDuringRuntime"

  globality-build-executor:
    working_directory: ~/repo
    docker:
      - image: ${AWS_ECR_DOMAIN}/globality-build:2021.41.0
        aws_auth:
          aws_access_key_id: ${AWS_ACCESS_KEY_ID}
          aws_secret_access_key: ${AWS_SECRET_ACCESS_KEY}
        environment:
          EXTRA_INDEX_URL: "InjectedDuringRuntime"
          AWS_ECR_DOMAIN: "InjectedDuringRuntime"
          JFROG_AUTH: "InjectedDuringRuntime"

whitelist: &whitelist
  paths: .

version: 2.1

commands:
  attach_repo_workspace:
    steps:
      - attach_workspace:
          at: ~/repo

  persist_to_repo_workspace:
    steps:
      - persist_to_workspace:
          root: ~/repo
          <<: *whitelist

  set_rc_version:
    steps:
      - run:
          name: Set RC version
          command: |
            sed  -i '/version/s/[^.]*$/'"0-dev${CIRCLE_BUILD_NUM}\",/" package.json

  publish_to_npmjs:
    steps:
      - run:
          name: Publish to NPM
          command: |
            npm publish

  publish_to_jfrog:
    steps:
      - run:
          name: Publish to JFrog
          command: |
            curl -s -u$JFROG_AUTH https://globality.jfrog.io/globality/api/npm/auth > .npmrc
            echo "registry=https://globality.jfrog.io/globality/api/npm/npm"       >> .npmrc
            npm publish

jobs:
  checkout:
    executor: node-executor

    steps:
      - checkout

      - run:
          name: Authenticate NPM
          command: |
            echo "//registry.npmjs.org/:_authToken=${NPM_AUTH_TOKEN}" > .npmrc
      - restore_cache:
          keys:
          - v1-dependencies-{{ checksum "yarn.lock" }}

      - run:
          name: Install Dependencies
          command: yarn install

      - save_cache:
          paths:
            - node_modules
          key: v1-dependencies-{{ checksum "yarn.lock" }}

      - persist_to_repo_workspace

  lint:
    executor: node-executor

    steps:
      - attach_repo_workspace

      - run:
          name: Lint
          command: yarn lint

  test:
    executor: node-executor

    steps:
      - attach_repo_workspace

      - run:
          name: Test
          command: yarn test

      - persist_to_repo_workspace

  build:
    executor: node-executor

    steps:
      - attach_repo_workspace

      - run:
          name: Build code
          command: yarn build

      - persist_to_repo_workspace

  deploy_rc_to_npmjs:
    executor: node-executor

    steps:
      - attach_repo_workspace
      - set_rc_version
      - publish_to_npmjs

  deploy_rc_to_jfrog:
    executor: node-executor

    steps:
      - attach_repo_workspace
      - set_rc_version
      - publish_to_jfrog

  publish_library_to_npmjs:
    executor: node-executor

    steps:
      - attach_repo_workspace
      - publish_to_npmjs

  publish_library_to_jfrog:
    executor: node-executor

    steps:
      - attach_repo_workspace
      - publish_to_jfrog

workflows:
  version: 2

  build:
    jobs:
      - checkout:
          context:
            - Globality-Common
      - lint:
          context:
            - Globality-Common
          requires:
            - checkout
      - test:
          context:
            - Globality-Common
          requires:
            - checkout
      - build:
          context:
            - Globality-Common
          requires:
            - lint
            - test
      - deploy_rc_to_npmjs:
          context:
            - Globality-Common
          requires:
            - build
      - deploy_rc_to_jfrog:
          context:
            - Globality-Common
          requires:
            - build

  release:
    jobs:
      - checkout:
          context:
            - Globality-Common
          filters:
            tags:
              only: /[0-9]+(\.[0-9]+)*/
            branches:
              ignore: /.*/
      - lint:
          context:
            - Globality-Common
          filters:
            tags:
              only: /[0-9]+(\.[0-9]+)*/
            branches:
              ignore: /.*/
          requires:
            - checkout
      - test:
          context:
            - Globality-Common
          filters:
            tags:
              only: /[0-9]+(\.[0-9]+)*/
            branches:
              ignore: /.*/
          requires:
            - checkout
      - build:
          context:
            - Globality-Common
          filters:
            tags:
              only: /[0-9]+(\.[0-9]+)*/
            branches:
              ignore: /.*/
          requires:
            - test
      - publish_library_to_npmjs:
          context:
            - Globality-Common
          filters:
            tags:
              only: /[0-9]+(\.[0-9]+)*/
            branches:
              ignore: /.*/
          requires:
            - build
      - publish_library_to_jfrog:
          context:
            - Globality-Common
          filters:
            tags:
              only: /[0-9]+(\.[0-9]+)*/
            branches:
              ignore: /.*/
          requires:
            - build