# NOTE: Current plan gives 1500 build minutes per month.
version: 2
jobs:
  test:
    docker:
     # Build by Thanos make docker-ci
      - image: quay.io/thanos/thanos-ci:v0.2.0
    working_directory: /go/src/github.com/thanos-io/thanos
    environment:
      GO111MODULE: 'on'
      # Run garbage collection more aggresively to avoid getting OOMed during the lint phase (4GB limit).
      GOGC: "20"
      # By default Go uses GOMAXPROCS but a Circle CI executor has many
      # cores (> 30) while the CPU and RAM resources are throttled. If we
      # don't limit this to the number of allocated cores, the job is
      # likely to get OOMed and killed.
      GOOPTS: "-p 2"
    steps:
      - checkout
      - setup_remote_docker:
            version: 17.07.0-ce
      - run:
          name: Create Secret if PR is not forked
          # GCS integration tests are run only for author's PR that have write access, because these tests
          # require credentials. Env variables that sets up these tests will work only for these kind of PRs.
          command: |
            if ! [ -z ${GCP_PROJECT} ]; then
              echo $GOOGLE_APPLICATION_CREDENTIALS_CONTENT > $GOOGLE_APPLICATION_CREDENTIALS
              echo "Awesome! GCS integration tests are enabled."
            fi
      - run: make deps
      - run: make lint
      - run: make check-docs
      - run: make format
      - run:
          name: "Run all tests"
          # TODO(bwplotka): Setup some S3 tests for CI.
          # taskset sets CPU affinity to 2 (current CPU limit).
          command: |
            if [ -z ${GCP_PROJECT} ]; then
              taskset 2 make test-local
              exit
            fi
            taskset 2 make test-only-gcs

  # Cross build is needed for publish_release but needs to be done outside of docker.
  cross_build:
    machine: true
    working_directory: /home/circleci/.go_workspace/src/github.com/thanos-io/thanos
    environment:
      GOBIN: "/home/circleci/.go_workspace/go/bin"
    steps:
    - checkout
    - run: make crossbuild
    - persist_to_workspace:
        root: .
        paths:
        - .build

  publish_master:
    docker:
      # Build by Thanos make docker-ci
      - image: quay.io/thanos/thanos-ci:v0.1.0
    working_directory: /go/src/github.com/thanos-io/thanos
    steps:
      - checkout
      - setup_remote_docker:
          version: 17.07.0-ce
      - attach_workspace:
          at: .
      - run: ln -s .build/linux-amd64/thanos thanos
      - run: make docker
      - run: docker run thanos --help
      # Upload to both dockerhub and quay.io.
      - run: echo "${DOCKERHUB_PASSWORD}" | docker login -u="${DOCKERHUB_USERNAME}" --password-stdin
      - run: make docker-push DOCKER_IMAGE_REPO=thanosio/thanos
      - run: echo "${QUAY_PASSWORD}" | docker login -u="${QUAY_USERNAME}" quay.io --password-stdin
      - run: make docker-push

  publish_release:
    docker:
      # Available from https://hub.docker.com/r/circleci/golang/
      - image: circleci/golang:1.13.1
    working_directory: /go/src/github.com/thanos-io/thanos
    environment:
      GOBIN: "/go/bin"
    steps:
      - checkout
      - setup_remote_docker:
          version: 17.07.0-ce
      - attach_workspace:
          at: .
      - run: make tarballs-release
      - store_artifacts:
          path: .tarballs
          destination: releases
      - run: ln -s .build/linux-amd64/thanos thanos
      - run: make docker
      - run: docker run thanos --help
      # Upload to both dockerhub and quay.io.
      - run: echo "${DOCKERHUB_PASSWORD}" | docker login -u="${DOCKERHUB_USERNAME}" --password-stdin
      - run: make docker-push DOCKER_IMAGE_REPO=thanosio/thanos DOCKER_IMAGE_TAG=$CIRCLE_TAG
      - run: echo "${QUAY_PASSWORD}" | docker login -u="${QUAY_USERNAME}" quay.io --password-stdin
      - run: make docker-push DOCKER_IMAGE_TAG=$CIRCLE_TAG

workflows:
  version: 2
  thanos:
    jobs:
    - test:
        filters:
          tags:
            only: /.*/
    - publish_master:
        requires:
        - test
        filters:
          branches:
            only: master
    - cross_build:
        requires:
        - test
        filters:
          tags:
            only: /^v[0-9]+(\.[0-9]+){2}(-.+|[^-.]*)$/
          branches:
            ignore: /.*/
    - publish_release:
        requires:
        - test
        - cross_build
        filters:
          tags:
            only: /^v[0-9]+(\.[0-9]+){2}(-.+|[^-.]*)$/
          branches:
            ignore: /.*/
