---
version: 2

jobs:
  test:
    docker:
    - image: circleci/golang:1.10
    working_directory: /go/src/github.com/prometheus/node_exporter

    steps:
    - checkout
    - run: make promu
    - run: make
    - run: rm -v node_exporter

  build:
    machine: true
    working_directory: /home/circleci/.go_workspace/src/github.com/prometheus/node_exporter

    environment:
      DOCKER_IMAGE_NAME: prom/node-exporter
      QUAY_IMAGE_NAME: quay.io/prometheus/node-exporter
      DOCKER_TEST_IMAGE_NAME: quay.io/prometheus/golang-builder:1.10-base
      REPO_PATH: github.com/prometheus/node_exporter

    steps:
    - checkout
    - run: make promu
    - run: promu crossbuild -v
    - persist_to_workspace:
        root: .
        paths:
        - .build
    - run: ln -s .build/linux-amd64/node_exporter node_exporter
    - run:
        command: |
          if [ -n "$CIRCLE_TAG" ]; then
            make docker DOCKER_IMAGE_NAME=$DOCKER_IMAGE_NAME DOCKER_IMAGE_TAG=$CIRCLE_TAG
            make docker DOCKER_IMAGE_NAME=$QUAY_IMAGE_NAME DOCKER_IMAGE_TAG=$CIRCLE_TAG
          else
            make docker DOCKER_IMAGE_NAME=$DOCKER_IMAGE_NAME
            make docker DOCKER_IMAGE_NAME=$QUAY_IMAGE_NAME
          fi
    - run: docker images
    - run: docker run --rm -t -v "$(pwd):/app" "${DOCKER_TEST_IMAGE_NAME}" -i "${REPO_PATH}" -T
    - run:
        command: |
          if [ -n "$CIRCLE_TAG" ]; then
            make test-docker DOCKER_IMAGE_TAG=$CIRCLE_TAG
          else
            make test-docker
          fi

  docker_hub_master:
    docker:
    - image: circleci/golang:1.10
    working_directory: /go/src/github.com/prometheus/node_exporter

    environment:
      DOCKER_IMAGE_NAME: prom/node-exporter
      QUAY_IMAGE_NAME: quay.io/prometheus/node-exporter
      DOCKER_TEST_IMAGE_NAME: quay.io/prometheus/golang-builder:1.10-base
      REPO_PATH: github.com/prometheus/node_exporter

    steps:
    - checkout
    - setup_remote_docker
    - attach_workspace:
        at: .
    - run: ln -s .build/linux-amd64/node_exporter node_exporter
    - run: make docker DOCKER_IMAGE_NAME=$DOCKER_IMAGE_NAME
    - run: make docker DOCKER_IMAGE_NAME=$QUAY_IMAGE_NAME
    - run: docker images
    - run: docker login -u $DOCKER_LOGIN -p $DOCKER_PASSWORD
    - run: docker login -u $QUAY_LOGIN -p $QUAY_PASSWORD quay.io
    - run: docker push $DOCKER_IMAGE_NAME
    - run: docker push $QUAY_IMAGE_NAME

  docker_hub_release_tags:
    docker:
    - image: circleci/golang:1.10
    working_directory: /go/src/github.com/prometheus/node_exporter

    environment:
      DOCKER_IMAGE_NAME: prom/node-exporter
      QUAY_IMAGE_NAME: quay.io/prometheus/node-exporter
      DOCKER_TEST_IMAGE_NAME: quay.io/prometheus/golang-builder:1.10-base
      REPO_PATH: github.com/prometheus/node_exporter

    steps:
    - checkout
    - setup_remote_docker
    - run: mkdir -v -p ${HOME}/bin
    - run: curl -L 'https://github.com/aktau/github-release/releases/download/v0.7.2/linux-amd64-github-release.tar.bz2' | tar xvjf - --strip-components 3 -C ${HOME}/bin
    - run: echo 'export PATH=${HOME}/bin:${PATH}' >> ${BASH_ENV}
    - run: make promu
    - attach_workspace:
        at: .
    - run: make promu
    - run: promu crossbuild tarballs
    - run: promu checksum .tarballs
    - run: promu release .tarballs
    - store_artifacts:
        path: .tarballs
        destination: releases
    - run:
        command: |
          if [ -n "$CIRCLE_TAG" ]; then
            make docker DOCKER_IMAGE_NAME=$DOCKER_IMAGE_NAME DOCKER_IMAGE_TAG=$CIRCLE_TAG
            make docker DOCKER_IMAGE_NAME=$QUAY_IMAGE_NAME DOCKER_IMAGE_TAG=$CIRCLE_TAG
          else
            make docker DOCKER_IMAGE_NAME=$DOCKER_IMAGE_NAME
            make docker DOCKER_IMAGE_NAME=$QUAY_IMAGE_NAME
          fi
    - run: docker login -u $DOCKER_LOGIN -p $DOCKER_PASSWORD
    - run: docker login -u $QUAY_LOGIN -p $QUAY_PASSWORD quay.io
    - run: |
        if [[ "$CIRCLE_TAG" =~ ^v[0-9]+(\.[0-9]+){2}$ ]]; then
          docker tag "$DOCKER_IMAGE_NAME:$CIRCLE_TAG" "$DOCKER_IMAGE_NAME:latest"
          docker tag "$QUAY_IMAGE_NAME:$CIRCLE_TAG" "$QUAY_IMAGE_NAME:latest"
        fi
    - run: echo docker push $DOCKER_IMAGE_NAME
    - run: echo docker push $QUAY_IMAGE_NAME

workflows:
  version: 2
  node_exporter:
    jobs:
    - test
    - build
    - docker_hub_master:
        requires:
        - test
        - build
        filters:
          branches:
            only: master
    - docker_hub_release_tags:
        requires:
        - test
        - build
        filters:
          branches:
            ignore: /.*/
          tags:
            only: /^v[0-9]+(\.[0-9]+){2}(-.+|[^-.]*)$/
