version: 2
jobs:
  build:
    docker:
      - image: circleci/golang:1.16

    environment:
      TEST_RESULTS: /tmp/test-results # path to where test results will be saved

    steps:
      - checkout
      - run: mkdir -p $TEST_RESULTS

      - restore_cache:
          keys:
            - go-mod-v4-{{ checksum "go.sum" }}

      - run:
          name: Run unit tests

          command: |
            gotestsum --junitfile ${TEST_RESULTS}/gotestsum-report.xml -- ./...

      - save_cache:
          key: go-mod-v4-{{ checksum "go.sum" }}
          paths:
            - "/go/pkg/mod"

      - store_test_results: # upload test results for display in Test Summary
          path: /tmp/test-results
workflows:
  version: 2
  build-workflow:
    jobs:
      - build

version: 2
jobs:
  test:
    docker:
      - image: circleci/golang:1.16

    environment:
      TEST_RESULTS: /tmp/test-results # path to where test results will be saved

    steps:
      - checkout
      - run: mkdir -p $TEST_RESULTS

      - restore_cache:
          keys:
            - go-mod-v4-{{ checksum "go.sum" }}

      - run:
          name: Run unit tests

          command: |
            gotestsum --junitfile ${TEST_RESULTS}/gotestsum-report.xml -- ./...

      - save_cache:
          key: go-mod-v4-{{ checksum "go.sum" }}
          paths:
            - "/go/pkg/mod"

      - store_test_results: # upload test results for display in Test Summary
          path: /tmp/test-results
  build-and-push-docker:
    docker:
      - image: circleci/golang:1.16

    steps:
      - checkout
      - setup_remote_docker
      - run:
          name: Docker login to Runtime Registry
          command: |
            echo "Login to Runtime Registry"
            TOKEN=$(curl -f -X POST "${SERVICETOKEN_ENDPOINT}" -d "{\"username\":\"${SERVICE_USERNAME}\",\"password\":\"${SERVICE_PASSWORD}\"}" -s --retry 3 | jq -r ".raw_id_token")
            echo "${TOKEN}" | docker login "${RUNTIME_REGISTRY}" -u x-runtime-id --password-stdin
      - run:
          name: Build and push to Runtime Registry
          command: |
            IMG_TAG=${RUNTIME_REGISTRY}/s/heroku/shaas/shaas:${CIRCLE_SHA1}
            echo "Build $IMG_TAG"
            docker build --no-cache -t $IMG_TAG .

            echo "Push $IMG_TAG to Runtime Registry"
            docker push $IMG_TAG
workflows:
  version: 2
  build-workflow:
    jobs:
      - test
      - build-and-push-docker
Build and push to runtime-registry
Build and push to runtime-registry
