# NOTE: Current plan gives 1500 build minutes per month.
version: 2
# https://circleci.com/blog/circleci-hacks-reuse-yaml-in-your-circleci-config-with-yaml/
defaults: &defaults
  docker:
    # Built by Thanos make docker-ci
    - image: &default-docker-image quay.io/thanos/thanos-ci:v1.2-go1.15-node

jobs:
  test:
    docker:
      - image: *default-docker-image
      - image: beaukode/docker-swift-onlyone-authv2-keystone
    working_directory: /go/src/github.com/thanos-io/thanos
    environment:
      GO111MODULE: 'on'
    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 and S3 AWS integration tests are enabled."
            fi
      - run:
          name: "Run unit tests."
          environment:
            GOBIN: "/go/bin"
            THANOS_TEST_OBJSTORE_SKIP: AZURE,COS,ALIYUNOSS
            # Variables for Swift testing.
            OS_AUTH_URL: http://127.0.0.1:5000/v2.0
            OS_PASSWORD: s3cr3t
            OS_PROJECT_NAME: admin
            OS_REGION_NAME: RegionOne
            OS_USERNAME: admin
          # taskset sets CPU affinity to 2 (current CPU limit).
          command: |
            if [ -z ${GCP_PROJECT} ]; then
              export THANOS_TEST_OBJSTORE_SKIP=${THANOS_TEST_OBJSTORE_SKIP},GCS,S3
            fi
            echo "Skipping tests for object storages: ${THANOS_TEST_OBJSTORE_SKIP}"
            taskset 2 make test

  # 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"
      PROMU_VERSION: "0.5.0"
    steps:
      - checkout
      - run: mkdir -p ${GOBIN}
      - run: curl -L "https://github.com/prometheus/promu/releases/download/v${PROMU_VERSION}/promu-${PROMU_VERSION}.$(go env GOOS)-$(go env GOARCH).tar.gz" | tar --strip-components=1 -xzf - -C ${GOBIN}
      - run: mv -f ${GOBIN}/promu "${GOBIN}/promu-v${PROMU_VERSION}"
      - run: make crossbuild -W ${GOBIN}/promu-v${PROMU_VERSION} # Ignore make dependency, it needs to be enforced somehow.
      - persist_to_workspace:
          root: .
          paths:
            - .build

  publish_master:
    <<: *defaults
    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: 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:
    <<: *defaults
    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: /.*/
