version: 2

defaults: &defaults
  working_directory: /go/src/github.com/weaveworks/scope
  docker:
    - image: weaveworks/scope-backend-build:build-updates-1b1c9664

client-defaults: &client-defaults
  working_directory: /home/weave/scope
  docker:
    - image: node:10.19
  environment:
      NPM_CONFIG_LOGLEVEL: warn
      NPM_CONFIG_PROGRESS: false
      XDG_CACHE_HOME: /home/weave/scope/.cache

workflows:
  version: 2
  test_and_deploy:
    jobs:
      - lint
      - unit-test
      - client-build
      - client-test:
          requires:
            - client-build
      - xplatform-build:
          requires:
            - build
      - build:
          requires:
            - client-build
      - integration-tests:
          requires:
            - lint
            - unit-test
            - build
      - deploy:
          filters:
            branches:
              only: master
          requires:
            - client-test
            - integration-tests

jobs:
  lint:
    <<: *defaults
    steps:
      - checkout
      - run: make BUILD_IN_CONTAINER=false lint

  unit-test:
    <<: *defaults
    parallelism: 1
    steps:
      - checkout
      - run: COVERDIR=./coverage NO_SCHEDULER=t make BUILD_IN_CONTAINER=false CODECGEN_UID=23 tests
      - persist_to_workspace:
          root: .
          paths:
          - coverage

  # Create client/build/index.html
  client-build:
    <<: *client-defaults
    steps:
      - checkout
      - restore_cache:
          name: Restoring Yarn Cache
          key: yarn-cache-2-{{ checksum "client/yarn.lock" }}
      - restore_cache:
          name: Restoring client/node_modules
          key: node-modules-{{ checksum "client/yarn.lock" }}-{{ checksum ".circleci/config.yml" }}
      - run: cd client; yarn install
      - save_cache:
          name: Saving Yarn Cache
          key: yarn-cache-2-{{ checksum "client/yarn.lock" }}
          paths:
            - "/home/weave/scope/.cache/yarn"
      - save_cache:
          name: Saving client/node_modules
          # include the CI config in the checksum because it will change when the docker image changes
          key: node-modules-{{ checksum "client/yarn.lock" }}-{{ checksum ".circleci/config.yml" }}
          paths:
            - "/home/weave/scope/client/node_modules"
      - run: |
          cd client
          yarn run build
          yarn run build-external
          yarn run bundle
      - persist_to_workspace:
          root: /home/weave/scope
          paths:
          - client/build/
          - client/build-external/
          - client/bundle/weave-scope.tgz


  client-test:
    <<: *client-defaults
    steps:
      - checkout
      - restore_cache:
          name: Restoring Yarn Cache
          key: yarn-cache-2-{{ checksum "client/yarn.lock" }}
      - restore_cache:
          name: Restoring client/node_modules
          key: node-modules-{{ checksum "client/yarn.lock" }}-{{ checksum ".circleci/config.yml" }}
      - run: |
          cd client
          yarn install
          yarn run lint
          yarn test

  xplatform-build:
    <<: *defaults
    steps:
      - checkout
      - run: GOARCH=arm  make BUILD_IN_CONTAINER=false GO_BUILD_INSTALL_DEPS= prog/scope
      - run: GOOS=darwin make BUILD_IN_CONTAINER=false GO_BUILD_INSTALL_DEPS= prog/scope

  build:
    <<: *defaults
    steps:
      - checkout
      - setup_remote_docker
      - attach_workspace:
          at: .
      - run: make BUILD_IN_CONTAINER=false SUDO= static all
      - run: cd extras; make BUILD_IN_CONTAINER=false
      - run: make -C tools/runner
      - persist_to_workspace:
          root: .
          paths:
          - scope.tar
          - cloud-agent.tar
          - tools/runner/runner
          - prog/externalui/
          - prog/staticui/
          - report/report.codecgen.go
          - render/detailed/detailed.codecgen.go

  integration-tests:
    machine:
      image: circleci/classic:201709-01
    working_directory: /home/circleci/src/github.com/weaveworks/scope
    environment:
      CIRCLE_ARTIFACTS: /tmp/artifacts
      CLOUDSDK_CORE_DISABLE_PROMPTS: 1
      GOPATH: /home/circleci/
    parallelism: 2
    steps:
      - checkout
      - attach_workspace:
          at: .
      - run: |
          sudo apt-get update
          sudo apt-get install python-pip jq pv
      - run: mkdir $CIRCLE_ARTIFACTS
      # kick off creation of test VMs
      - run: test -z "$SECRET_PASSWORD" || bin/setup-circleci-secrets "$SECRET_PASSWORD"
      - run: test -z "$SECRET_PASSWORD" || (cd integration; ./gce.sh make_template)
      - run: test -z "$SECRET_PASSWORD" || (cd integration; ./gce.sh setup && eval $(./gce.sh hosts); ./setup.sh)
      - run: make deps; touch tools/runner/runner
      # Run all integration tests
      - run:
          command: test -z "$SECRET_PASSWORD" || (cd integration; eval $(./gce.sh hosts); ./run_all.sh)
          no_output_timeout: 5m
      # Destroy testing VMs:
      - run:
          command: test -z "$SECRET_PASSWORD" || (cd integration; ./gce.sh destroy)
          background: true
      # Code coverage
      - run: ./tools/cover/gather_coverage.sh ./coverage
      - run: goveralls -repotoken $COVERALLS_REPO_TOKEN -coverprofile=profile.cov -service=circleci
      - run: cp coverage.* */*.codecgen.go $CIRCLE_ARTIFACTS
      - store_artifacts:
          path: /tmp/artifacts

  deploy:
    machine:
      image: circleci/classic:201709-01
    working_directory: /home/circleci/src/github.com/weaveworks/scope
    environment:
      IMAGES: scope cloud-agent
      SCOPE_SKIP_UI_ASSETS: true
    steps:
      - checkout
      - attach_workspace:
          at: .
      - run: |
          docker load -i scope.tar
          docker load -i cloud-agent.tar
      - run: |
          test -z "${DOCKER_USER}" && exit 0
          docker login -u $DOCKER_USER -p $DOCKER_PASS
          for IMAGE in $IMAGES; do
              test "${DOCKER_ORGANIZATION:-$DOCKER_USER}" = "weaveworks" || docker tag weaveworks/$IMAGE:latest ${DOCKER_ORGANIZATION:-$DOCKER_USER}/$IMAGE:latest
              docker tag weaveworks/$IMAGE:latest ${DOCKER_ORGANIZATION:-$DOCKER_USER}/$IMAGE:$(./tools/image-tag)
              docker push ${DOCKER_ORGANIZATION:-$DOCKER_USER}/$IMAGE:latest
              docker push ${DOCKER_ORGANIZATION:-$DOCKER_USER}/$IMAGE:$(./tools/image-tag)
          done
      - run: |
          test -z "${QUAY_USER}" && exit 0
          docker login -e '.' -u "$QUAY_USER" -p "$QUAY_PASSWORD" quay.io
          docker tag weaveworks/scope:$(./tools/image-tag) "quay.io/${QUAY_ORGANIZATION}/scope:$(./tools/image-tag)"
          docker push "quay.io/${QUAY_ORGANIZATION}/scope:$(./tools/image-tag)"
      - run: test -z "${UI_BUCKET_KEY_ID}" || (make BUILD_IN_CONTAINER=false ui-upload ui-pkg-upload)
