version: 2.1
jobs:

  build-binary:
    docker:
      - image: circleci/golang:1.14
    working_directory: ~/build
    steps:
      - checkout
      - restore_cache:
          keys:
            - go-mod-v3-{{ checksum "go.sum" }}
      - run:
          name: Run go mod download
          command: go mod download
      - run:
          name: Check code formatting
          command: go install golang.org/x/tools/cmd/goimports && make test-fmt
      - run:
          name: Build Flagger
          command: |
            CGO_ENABLED=0 GOOS=linux go build \
                -ldflags "-s -w -X github.com/weaveworks/flagger/pkg/version.REVISION=${CIRCLE_SHA1}" \
                -a -installsuffix cgo -o bin/flagger ./cmd/flagger/*.go
      - run:
          name: Build Flagger load tester
          command: |
            CGO_ENABLED=0 GOOS=linux go build \
                -a -installsuffix cgo -o bin/loadtester ./cmd/loadtester/*.go
      - run:
          name: Run unit tests
          command: |
            go test -race -coverprofile=coverage.txt -covermode=atomic $(go list ./pkg/...)
            bash <(curl -s https://codecov.io/bash)
      - run:
          name: Verify code gen
          command: make test-codegen
      - save_cache:
          key: go-mod-v3-{{ checksum "go.sum" }}
          paths:
            - "/go/pkg/mod/"
      - persist_to_workspace:
          root: bin
          paths:
            - flagger
            - loadtester

  push-container:
    docker:
      - image: circleci/golang:1.14
    steps:
      - checkout
      - setup_remote_docker:
          docker_layer_caching: true
      - attach_workspace:
          at: /tmp/bin
      - run: test/container-build.sh
      - run: test/container-push.sh

  push-binary:
    docker:
      - image: circleci/golang:1.14
    working_directory: ~/build
    steps:
      - checkout
      - setup_remote_docker:
          docker_layer_caching: true
      - restore_cache:
          keys:
            - go-mod-v3-{{ checksum "go.sum" }}
      - run: make release-notes
      - run: github-release-notes -org weaveworks -repo flagger -since-latest-release -include-author > /tmp/release.txt
      - run: test/goreleaser.sh

  e2e-kubernetes-testing:
    machine: true
    steps:
      - checkout
      - attach_workspace:
          at: /tmp/bin
      - run: test/container-build.sh
      - run: test/e2e-kind.sh v1.17.2
      - run: test/e2e-kubernetes.sh
      - run: test/e2e-kubernetes-tests-deployment.sh
      - run: test/e2e-kubernetes-cleanup.sh
      - run: test/e2e-kubernetes-tests-daemonset.sh

  e2e-istio-testing:
    machine: true
    steps:
      - checkout
      - attach_workspace:
          at: /tmp/bin
      - run: test/container-build.sh
      - run: test/e2e-kind.sh
      - run: test/e2e-istio.sh
      - run: test/e2e-istio-tests.sh

  e2e-gloo-testing:
    machine: true
    steps:
      - checkout
      - attach_workspace:
          at: /tmp/bin
      - run: test/container-build.sh
      - run: test/e2e-kind.sh
      - run: test/e2e-gloo.sh
      - run: test/e2e-gloo-tests.sh

  e2e-nginx-testing:
    machine: true
    steps:
      - checkout
      - attach_workspace:
          at: /tmp/bin
      - run: test/container-build.sh
      - run: test/e2e-kind.sh
      - run: test/e2e-nginx.sh
      - run: test/e2e-nginx-tests.sh
      - run: test/e2e-nginx-cleanup.sh
      - run: test/e2e-nginx-custom-annotations.sh
      - run: test/e2e-nginx-tests.sh

  e2e-linkerd-testing:
    machine: true
    steps:
      - checkout
      - attach_workspace:
          at: /tmp/bin
      - run: test/container-build.sh
      - run: test/e2e-kind.sh
      - run: test/e2e-linkerd.sh
      - run: test/e2e-linkerd-tests.sh

  e2e-contour-testing:
    machine: true
    steps:
      - checkout
      - attach_workspace:
          at: /tmp/bin
      - run: test/container-build.sh
      - run: test/e2e-kind.sh
      - run: test/e2e-contour.sh
      - run: test/e2e-contour-tests.sh

  push-helm-charts:
    docker:
      - image: circleci/golang:1.14
    steps:
      - checkout
      - run:
          name: Install kubectl
          command: sudo curl -L https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl -o /usr/local/bin/kubectl && sudo chmod +x /usr/local/bin/kubectl
      - run:
          name: Install helm
          command: sudo curl -L https://storage.googleapis.com/kubernetes-helm/helm-v2.14.2-linux-amd64.tar.gz | tar xz && sudo mv linux-amd64/helm /bin/helm && sudo rm -rf linux-amd64
      - run:
          name: Initialize helm
          command:  helm init --client-only --kubeconfig=$HOME/.kube/kubeconfig
      - run:
          name: Lint charts
          command: |
            helm lint ./charts/*
      - run:
          name: Package charts
          command: |
            mkdir $HOME/charts
            helm package ./charts/* --destination $HOME/charts
      - run:
          name: Publish charts
          command: |
            if echo "${CIRCLE_TAG}" | grep v; then
              REPOSITORY="https://weaveworksbot:${GITHUB_TOKEN}@github.com/weaveworks/flagger.git"
              git config user.email weaveworksbot@users.noreply.github.com
              git config user.name weaveworksbot
              git remote set-url origin ${REPOSITORY}
              git checkout gh-pages
              mv -f $HOME/charts/*.tgz .
              helm repo index . --url https://flagger.app
              git add .
              git commit -m "Publish Helm charts v${CIRCLE_TAG}"
              git push origin gh-pages
            else
              echo "Not a release! Skip charts publish"
            fi

workflows:
  version: 2
  build-test-push:
    jobs:
      - build-binary:
          filters:
            branches:
              ignore:
                - gh-pages
                - /^user-.*/
      - e2e-kubernetes-testing:
          requires:
            - build-binary
      - e2e-istio-testing:
          requires:
            - build-binary
      - e2e-gloo-testing:
          requires:
            - build-binary
      - e2e-nginx-testing:
          requires:
            - build-binary
      - e2e-linkerd-testing:
          requires:
            - build-binary
      - e2e-contour-testing:
          requires:
            - build-binary
      - push-container:
          requires:
            - build-binary
            - e2e-kubernetes-testing
            - e2e-istio-testing
            - e2e-gloo-testing
            - e2e-nginx-testing
            - e2e-linkerd-testing
          filters:
            branches:
              only:
                - master

  release:
    jobs:
      - build-binary:
          filters:
            branches:
              ignore: /.*/
            tags:
              ignore: /^chart.*/
      - push-container:
          requires:
            - build-binary
          filters:
            branches:
              ignore: /.*/
            tags:
              ignore: /^chart.*/
      - push-binary:
          requires:
            - push-container
          filters:
            branches:
              ignore: /.*/
            tags:
              ignore: /^chart.*/
      - push-helm-charts:
          requires:
            - push-container
          filters:
            branches:
              ignore: /.*/
            tags:
              ignore: /^chart.*/
