version: 2

jobs:

  lint:
    working_directory: /work
    docker: [{image: 'docker:20.10-git'}]
    environment:
      DOCKER_BUILDKIT: 1
    steps:
      - checkout
      - setup_remote_docker:
          version: 20.10.6
          reusable: true
          exclusive: false
      - run:
          name: "Docker version"
          command: docker version
      - run:
          name: "Docker info"
          command: docker info
      - run:
          name: "Shellcheck - build image"
          command: |
            docker build --progress=plain -f dockerfiles/Dockerfile.shellcheck --tag cli-validator:$CIRCLE_BUILD_NUM .
      - run:
          name: "Shellcheck"
          command: |
            docker run --rm cli-validator:$CIRCLE_BUILD_NUM \
                make shellcheck
      - run:
          name: "Lint - build image"
          command: |
            docker build --progress=plain -f dockerfiles/Dockerfile.lint --tag cli-linter:$CIRCLE_BUILD_NUM .
      - run:
          name: "Lint"
          command: |
            docker run --rm cli-linter:$CIRCLE_BUILD_NUM

  cross:
    working_directory: /work
    docker: [{image: 'docker:20.10-git'}]
    environment:
      DOCKER_BUILDKIT: 1
      BUILDX_VERSION: "v0.8.2"
    parallelism: 3
    steps:
      - checkout
      - setup_remote_docker:
          version: 20.10.6
          reusable: true
          exclusive: false
      - run:
          name: "Docker version"
          command: docker version
      - run:
          name: "Docker info"
          command: docker info
      - run: apk add make curl
      - run: mkdir -vp ~/.docker/cli-plugins/
      - run: curl -fsSL --output ~/.docker/cli-plugins/docker-buildx https://github.com/docker/buildx/releases/download/${BUILDX_VERSION}/buildx-${BUILDX_VERSION}.linux-amd64
      - run: chmod a+x ~/.docker/cli-plugins/docker-buildx
      - run: docker buildx version
      - run: docker context create buildctx
      - run: docker buildx create --use buildctx && docker buildx inspect --bootstrap
      - run: GROUP_INDEX=$CIRCLE_NODE_INDEX GROUP_TOTAL=$CIRCLE_NODE_TOTAL docker buildx bake cross --progress=plain
      - store_artifacts:
          path: /work/build

  test:
    working_directory: /work
    docker: [{image: 'docker:20.10-git'}]
    environment:
      DOCKER_BUILDKIT: 1
    steps:
      - checkout
      - setup_remote_docker:
          version: 20.10.6
          reusable: true
          exclusive: false
      - run:
          name: "Docker version"
          command: docker version
      - run:
          name: "Docker info"
          command: docker info
      - run:
          name: "Unit Test with Coverage - build image"
          command: |
            mkdir -p test-results/unit-tests
            docker build --progress=plain -f dockerfiles/Dockerfile.dev --tag cli-builder:$CIRCLE_BUILD_NUM .
      - run:
          name: "Unit Test with Coverage"
          command: |
            docker run \
                -e GOTESTSUM_JUNITFILE=/tmp/junit.xml \
                --name \
                test-$CIRCLE_BUILD_NUM cli-builder:$CIRCLE_BUILD_NUM \
                make test-coverage
            docker cp \
                test-$CIRCLE_BUILD_NUM:/tmp/junit.xml \
                ./test-results/unit-tests/junit.xml
      - run:
          name: "Upload to Codecov"
          command: |
            docker cp \
                test-$CIRCLE_BUILD_NUM:/go/src/github.com/docker/cli/coverage.txt \
                coverage.txt
            apk add -U bash curl
            curl -s https://codecov.io/bash | bash || \
                echo 'Codecov failed to upload'
      - store_test_results:
          path:  test-results
      - store_artifacts:
          path:  test-results

  validate:
    working_directory: /work
    docker: [{image: 'docker:20.10-git'}]
    environment:
      DOCKER_BUILDKIT: 1
    steps:
      - checkout
      - setup_remote_docker:
          version: 20.10.6
          reusable: true
          exclusive: false
      - run:
          name: "Docker version"
          command: docker version
      - run:
          name: "Docker info"
          command: docker info
      - run:
          name: "Validate - build image"
          command: |
            rm -f .dockerignore # include .git
            docker build --progress=plain -f dockerfiles/Dockerfile.dev --tag cli-builder-with-git:$CIRCLE_BUILD_NUM .
      - run:
          name: "Validate Vendor, Docs, and Code Generation"
          command: |
            docker run --rm cli-builder-with-git:$CIRCLE_BUILD_NUM \
                make ci-validate
          no_output_timeout: 15m

workflows:
  version: 2
  ci:
    jobs:
      - lint
      - cross
      - test
      - validate
