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

  docker:
  - image: golang:1.9.2

  environment: &env_defaults
  - CGO_ENABLED: "0"

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

  vendor_dependencies:
    <<: *job_defaults

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

      - restore_cache:
          keys:
          - vendor-{{ checksum "Gopkg.lock" }}

      - run:
          name: Update dependencies
          command: |
            if [ ! -d "vendor" ]; then
              command -v dep >/dev/null 2>&1 || go get github.com/golang/dep/cmd/dep
              dep ensure -v -vendor-only
            fi

      - save_cache:
          key: vendor-{{ checksum "Gopkg.lock" }}
          paths:
          - /go/src/github.com/jimmidyson/configmap-reload/vendor

  build:
    <<: *job_defaults

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

      - restore_cache:
          keys:
          - vendor-{{ checksum "Gopkg.lock" }}

      - run:
          name: Build binary

          command: |
            ROOT_PKG=$(go list .)
            VERSION=$(git describe --match 'v[0-9]*' --dirty --always)
            BUILD_DATE=$(date -u)
            go build -v                                                                                                           \
                -installsuffix "static"                                                                                           \
                -ldflags "-X ${ROOT_PKG}/pkg/version.AppVersion=${VERSION} -X '${ROOT_PKG}/pkg/version.BuildDate=${BUILD_DATE}'"  \
                -o out/configmap-reload \
                .
      - persist_to_workspace:
          root: .
          paths:
          - out/configmap-reload

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

  build_docker:
    <<: *job_defaults

    docker:
    - image: docker:18.01.0-ce-git

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

      - setup_remote_docker:
          version: 17.11.0-ce

      - attach_workspace:
          at: .

      - run:
          name: Build Docker image
          command: |
            docker build \
              -t jimmidyson/configmap-reload:${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.01.0-ce-git

    steps:
      - setup_remote_docker:
          version: 17.11.0-ce

      - 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]+)*/
    - vendor_dependencies:
        requires:
        - checkout_code
        filters:
          tags:
            only:
              /v[0-9]+(\.[0-9]+)*/
    - build:
        requires:
        - vendor_dependencies
        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]+)*/
