version: 2.1
executors:
  node14:
    docker:
      - image: circleci/node:14
        environment:
          NPM_CONFIG_PREFIX: "~/.npm-global"
    working_directory: ~/repo

orbs:
  helix-post-deploy: adobe/helix-post-deploy@3.0.0
  helix-gcloud-setup: adobe/helix-gcloud-setup@1.0.0

commands:
  setup:
    steps:
      - checkout
      - restore_cache:
          keys:
            - v3-dependencies-{{ arch }}-{{ checksum "package-lock.json" }}
      - run:
          name: install latest npm
          command: sudo npm -g install npm
      - run:
          name: Installing Dependencies
          command: npm ci
      - save_cache:
          paths:
            - node_modules
          key: v3-dependencies-{{ arch }}-{{ checksum "package-lock.json" }}
      - run:
          name: prepare test git user
          command: git config --global user.email "circleci@example.com" && git config --global user.name "CircleCi Build"

jobs:
  build:
    executor: node14
    steps:
      - setup
      - run: mkdir junit
      - run:
          name: Lint
          command: npm run lint
      - run:
          name: Getting Code Coverage
          command: npm run test-ci
      - store_test_results:
          path: junit
      - store_artifacts:
          path: junit

  semantic-release:
    executor: node14
    steps:
      - setup
      - run:
          name: revert changes to package-lock.json
          command: git checkout -- package-lock.json
      - helix-post-deploy/config-wsk
      - helix-post-deploy/config-env
      - helix-gcloud-setup/load-config
      - run:
          name: Semantic Release
          command: npm run semantic-release
      - run:
          name: Post-Deployment Integration Test
          command: npm run test-postdeploy
      - store_test_results:
          path: junit
      - run:
          name: Link Sequences
          command: npm run deploy-sequences
      # see https://circleci.com/orbs/registry/orb/adobe/helix-post-deploy
      # for more available parameters
      - helix-post-deploy/monitoring:
           targets: universal, aws, google, adeobeio
           statuspage_name: Helix Service
           statuspage_group: Development
           newrelic_group_policy: Development Repeated Failure
           newrelic_group_targets: universal
           incubator: true # remove only when promoting service to production

  branch-deploy:
    executor: node14
    steps:
      - setup
      - run:
          name: revert changes to package-lock.json
          command: git checkout -- package-lock.json
      - helix-post-deploy/config-wsk
      - helix-gcloud-setup/load-config
      - run:
          name: Branch Deployment
          command: npm run deploy-ci
      - run:
          name: Post-Deployment Integration Test
          command: npm run test-postdeploy
      - store_test_results:
          path: junit

workflows:
  version: 2
  build:
    jobs:
      - build
      - branch-deploy:
          context: Project Helix
          requires:
            - build
          filters:
            branches:
              ignore: main
      - semantic-release:
          context: Project Helix
          requires:
            - build
          filters:
            branches:
              only: main
