version: 2.1

orbs:
  kubernetes: circleci/kubernetes@0.7.0

commands:
  attach-workspace:
    steps:
    - run:
        name: Preparing workspace directory
        command: |
          sudo rm -rf /tmp/workspace
          sudo mkdir -p /tmp/workspace
          sudo chown -vf circleci:circleci /tmp/workspace
        working_directory: /
    - attach_workspace:
        at: /tmp/workspace

  docker-login:
    steps:
      - run:
          name: Docker login
          command: |
            docker login -u="${DOCKER_USERNAME}" -p="${DOCKER_PASSWORD}" "${DOCKER_REGISTRY}"

  install-golang:
    steps:
      - run:
          name: Setup GO env
          command: |
            mkdir -p ~/tmp
            echo 'export TMPDIR=~/tmp/' >> $BASH_ENV
            echo 'export GOROOT=/opt/go' >> $BASH_ENV
            echo 'export GOPATH=~/go' >> $BASH_ENV
            sudo mkdir -p /opt/go/bin
            mkdir -p ~/go/bin
            echo 'export PATH=$GOROOT/bin:$PATH' >> $BASH_ENV
            echo 'export PATH=$GOPATH/bin:$PATH' >> $BASH_ENV
      - run:
          name: Install Golang
          command: |
            curl --fail -L https://dl.google.com/go/go1.13.7.linux-amd64.tar.gz | sudo tar -C /opt -xzf-

  install-operator-sdk:
    steps:
      - run:
          name: Install operator-sdk
          command: |
            export OPERATOR_SDK_RELEASE_VERSION=v1.2.0
            curl -OJL https://github.com/operator-framework/operator-sdk/releases/download/${OPERATOR_SDK_RELEASE_VERSION}/operator-sdk-${OPERATOR_SDK_RELEASE_VERSION}-x86_64-linux-gnu
            chmod +x operator-sdk-${OPERATOR_SDK_RELEASE_VERSION}-x86_64-linux-gnu && sudo cp operator-sdk-${OPERATOR_SDK_RELEASE_VERSION}-x86_64-linux-gnu /usr/local/bin/operator-sdk && rm operator-sdk-${OPERATOR_SDK_RELEASE_VERSION}-x86_64-linux-gnu

  install-operator-dependencies:
    steps:
      - restore_cache:
          keys:
            - apicast-operator-go-mod-v1-{{ arch }}-{{ checksum "go.sum" }}
      - run:
          name: Install operator dependencies
          command: |
            make download
      - save_cache:
          key: apicast-operator-go-mod-v1-{{ arch }}-{{ checksum "go.sum" }}
          paths:
            - "/go/pkg/mod"

  build-operator-image:
    steps:
      - checkout
      - install-operator-sdk
      - install-operator-dependencies
      - run:
          name: Build Operator
          command: |
              make docker-build-only IMG=apicast-operator:latest

jobs:
  install-operator:
    docker:
      - image: circleci/golang:1.13.7
    working_directory: /go/src/github.com/3scale/apicast-operator
    steps:
      - setup_remote_docker:
          docker_layer_caching: true
      - build-operator-image
      - run:
          name: Sharing requirements to downstream job
          command: |
            mkdir -p /tmp/workspace/images
            docker save -o /tmp/workspace/images/operator-image.tar apicast-operator:latest
      - persist_to_workspace:
          root: /tmp/workspace
          paths:
            - images/operator-image.tar

  license-check:
    docker:
      - image: circleci/ruby:2.5
    steps:
      - install-golang
      - checkout
      - run: sudo gem install license_finder --version 5.7.1
      - run: make licenses-check

  tag-operator-image-master:
    machine:
      image: ubuntu-1604:202007-01
      docker_layer_caching: true
    steps:
      - attach-workspace
      - run:
          name: Unpack docker image
          command: |
            docker load -i /tmp/workspace/images/operator-image.tar
      - run:
          name: Tag image as master
          command: |
              docker tag apicast-operator:latest quay.io/3scale/apicast-operator:master
      - docker-login
      - run:
          name: Push master image
          command: |
              docker push quay.io/3scale/apicast-operator:master
      - run:
          name: Tag image as latest
          command: |
            docker tag quay.io/3scale/apicast-operator:master quay.io/3scale/apicast-operator:latest
      - run:
          name: Push latest image
          command: |
              docker push quay.io/3scale/apicast-operator:latest

  tag-operator-image-release:
    machine:
      image: ubuntu-1604:202007-01
      docker_layer_caching: true
    steps:
      - attach-workspace
      - run:
          name: Unpack docker image
          command: |
            docker load -i /tmp/workspace/images/operator-image.tar
      - run:
          name: Tag image as release ${CIRCLE_TAG}
          command: |
              docker tag apicast-operator:latest quay.io/3scale/apicast-operator:${CIRCLE_TAG}
      - docker-login
      - run:
          name: Push ${CIRCLE_TAG} tag
          command: |
              docker push quay.io/3scale/apicast-operator:${CIRCLE_TAG}

  run-e2e-test:
    docker:
      - image: circleci/golang:1.13.7
    working_directory: /go/src/github.com/3scale/apicast-operator
    steps:
      - install-operator-sdk
      - checkout
      - install-operator-dependencies
      - run:
          name: run E2E tests
          command: |
            make test-e2e
          no_output_timeout: 30m

  test-crds:
    docker:
      - image: circleci/golang:1.13.7
    working_directory: /go/src/github.com/3scale/apicast-operator
    steps:
      - checkout
      - install-operator-dependencies
      - run: make test-crds

  bundle-validate:
    docker:
      - image: circleci/golang:1.13.7
    working_directory: /go/src/github.com/3scale/apicast-operator
    steps:
      - checkout
      - install-operator-sdk
      - run: make bundle
      - run: make bundle-update-test

workflows:
  version: 2
  build:
    jobs:
      - license-check
      - bundle-validate
      - test-crds
      - run-e2e-test
      - install-operator:
          filters: # required since `tag-operator-image-release` job has tag filters AND requires `install-operator`. Otherwise tag build will not be triggered on new tags
            tags:
              only: /^v.*/
            branches:
              only: master
      - tag-operator-image-master:
          context: org-global
          requires:
            - run-e2e-test
            - install-operator
          filters:
            branches:
              only: master
      - tag-operator-image-release:
          context: org-global
          requires:
            - run-e2e-test
            - install-operator
          filters:
            tags:
              only: /^v.*/
            branches:
              ignore: /.*/
