job_defaults: &job_defaults
  working_directory: /go/src/github.com/jimmidyson/configmap-reload

  docker:
  - image: golang:1.12.6

  environment: &env_defaults
  - CGO_ENABLED: "0"
  - GO111MODULE: "on"

version: 2

jobs:
  checkout_code:
    <<: *job_defaults

    steps:
      - checkout

      - save_cache:
          key: code-repo-{{ .Environment.CIRCLE_SHA1 }}-{{ .Environment.CIRCLE_TAG }}
          paths:
          - /go/src/github.com/jimmidyson/configmap-reload

  build:
    <<: *job_defaults

    steps:
      - restore_cache:
          keys:
          - code-repo-{{ .Environment.CIRCLE_SHA1 }}-{{ .Environment.CIRCLE_TAG }}
      
      - restore_cache:
          keys:
          - gomod-cache-{{ checksum "go.sum" }}

      - run:
          name: Build binary

          command: make cross out/configmap-reload

      - persist_to_workspace:
          root: .
          paths:
          - out/configmap-reload*

      - store_artifacts:
          path: ./out
          destination: out/

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

  build_docker:
    <<: *job_defaults

    docker:
    - image: docker:18.09.3-git

    steps:
      - restore_cache:
          keys:
          - code-repo-{{ .Environment.CIRCLE_SHA1 }}-{{ .Environment.CIRCLE_TAG }}

      - setup_remote_docker:
          version: 18.09.3

      - attach_workspace:
          at: .

      - run:
          name: Build Docker image
          command: |
            apk add -U make bash
            make docker GOOS=linux GOARCH=amd64 DOCKER_IMAGE_TAG=${CIRCLE_SHA1}

      - run:
          name: Export Docker image
          command: |
            docker save -o ./configmap-reload-docker.tar jimmidyson/configmap-reload:${CIRCLE_SHA1}

      - persist_to_workspace:
          root: .
          paths:
          - configmap-reload-docker.tar

      - store_artifacts:
          path: ./configmap-reload-docker.tar
          destination: configmap-reload-docker.tar

  push_docker:
    <<: *job_defaults

    docker:
    - image: docker:18.09.3-git

    steps:
      - setup_remote_docker:
          version: 18.09.3

      - attach_workspace:
          at: .

      - run:
          name: Push Docker image
          command: |
            if [ "${CIRCLE_PROJECT_USERNAME}" != "jimmidyson" ]; then
              echo "Skipping - this is not in origin repository"
              exit 0
            fi
            if [ -z "${DOCKER_LOGIN}" ] || [ -z "${DOCKER_PASSWORD}" ]; then
              echo "Missing Docker login information!!!"
              exit 1
            fi
            docker load -q -i ./configmap-reload-docker.tar
            DOCKER_TAG=${CIRCLE_TAG:-latest}
            docker tag jimmidyson/configmap-reload:${CIRCLE_SHA1} jimmidyson/configmap-reload:${DOCKER_TAG}
            docker login -u ${DOCKER_LOGIN} -p ${DOCKER_PASSWORD}
            docker push jimmidyson/configmap-reload:${DOCKER_TAG}

workflows:
  version: 2
  build_and_deploy:
    jobs:
    - checkout_code:
        filters:
          tags:
            only:
              /v[0-9]+(\.[0-9]+)*/
    - build:
        requires:
        - checkout_code
        filters:
          tags:
            only:
              /v[0-9]+(\.[0-9]+)*/
    - build_docker:
        requires:
        - build
        filters:
          tags:
            only:
              /v[0-9]+(\.[0-9]+)*/
    - push_docker:
        requires:
        - build_docker
        filters:
          branches:
            only: master
          tags:
            only:
              /v[0-9]+(\.[0-9]+)*/
