---
version: 2.1
executors:
  golang:
    docker:
      - image: circleci/golang:1.14.8-node
    resource_class: large
  nodejs:
    docker:
      - image: circleci/node:10.0.0
    resource_class: large
  cosmic:
    docker:
      - image: circleci/buildpack-deps:cosmic
    resource_class: medium
jobs:
  backend-services-checks:
    executor: golang
    steps:
      - checkout
      - run:
          name: Run backend service checks
          command: make backend-services-checks
          working_directory: ~/project/litmus-portal
  frontend-services-checks:
    executor: nodejs
    steps:
      - checkout
      - run:
          name: Run frontend service checks
          command: make frontend-services-checks
          working_directory: ~/project/litmus-portal
  setup-env:
    executor: cosmic
    steps:
      - run: mkdir -p workspace
      - run:
          name: Setup ENV
          command: |
            echo 'export REPONAME="litmuschaos"' >> workspace/env-vars
            echo 'export GRAPHQL_SERVER_IMAGE="litmusportal-server"' >> workspace/env-vars
            echo 'export AUTHENTICATION_SERVER_IMAGE="litmusportal-auth-server"' >> workspace/env-vars
            echo 'export FRONTEND_IMAGE="litmusportal-frontend"' >> workspace/env-vars
            echo 'export SUBSCRIBER_IMAGE="litmusportal-subscriber"' >> workspace/env-vars
            echo 'export IMGTAG="ci"' >> workspace/env-vars
            cat workspace/env-vars >> $BASH_ENV
            source $BASH_ENV
      - persist_to_workspace:
          root: workspace
          paths:
            - env-vars
  docker-build-backend:
    machine:
      image: circleci/classic:201808-01
      resource_class: large
    steps:
      - attach_workspace:
          at: /tmp/workspace
      - run: |
          cat /tmp/workspace/env-vars >> $BASH_ENV
          source $BASH_ENV
      - checkout
      - run:
          name: Build graphql server docker image
          command: docker build . -f build/Dockerfile -t ${REPONAME}/${GRAPHQL_SERVER_IMAGE}:${IMGTAG}
          working_directory: ~/project/litmus-portal/graphql-server
      - run:
          name: Save graphql server docker image
          command: docker save -o /tmp/workspace/${GRAPHQL_SERVER_IMAGE}.tar ${REPONAME}/${GRAPHQL_SERVER_IMAGE}:${IMGTAG}
      - run:
          name: Build auth server docker image
          command: docker images && docker build . -f Dockerfile -t ${REPONAME}/${AUTHENTICATION_SERVER_IMAGE}:${IMGTAG}
          working_directory: ~/project/litmus-portal/authentication
      - run:
          name: Save auth server docker image
          command: docker save -o /tmp/workspace/${AUTHENTICATION_SERVER_IMAGE}.tar ${REPONAME}/${AUTHENTICATION_SERVER_IMAGE}:${IMGTAG}
      - run:
          name: Build subscriber docker image
          command: docker build . -f build/Dockerfile -t ${REPONAME}/${SUBSCRIBER_IMAGE}:${IMGTAG}
          working_directory: ~/project/litmus-portal/cluster-agents/subscriber
      - run:
          name: Save subscriber docker image
          command: docker save -o /tmp/workspace/${SUBSCRIBER_IMAGE}.tar ${REPONAME}/${SUBSCRIBER_IMAGE}:${IMGTAG}
      - persist_to_workspace:
          root: /tmp/workspace
          paths:
            - litmusportal-server.tar
            - litmusportal-subscriber.tar
            - litmusportal-auth-server.tar
  docker-build-frontend:
    machine:
      image: circleci/classic:201808-01
      resource_class: large
    steps:
      - attach_workspace:
          at: /tmp/workspace
      - run: |
          cat /tmp/workspace/env-vars >> $BASH_ENV
          source $BASH_ENV
      - checkout
      - run:
          name: Build frontend docker image
          command: docker build . -f Dockerfile -t ${REPONAME}/${FRONTEND_IMAGE}:${IMGTAG}
          working_directory: ~/project/litmus-portal/frontend
      - run:
          name: Save frontend docker image
          command: docker save -o /tmp/workspace/${FRONTEND_IMAGE}.tar ${REPONAME}/${FRONTEND_IMAGE}:${IMGTAG}
      - persist_to_workspace:
          root: /tmp/workspace
          paths:
            - litmusportal-frontend.tar
  docker-push:
    machine:
      image: circleci/classic:201808-01
      resource_class: large
    steps:
      - attach_workspace:
          at: /tmp/workspace
      - run: |
          cat /tmp/workspace/env-vars >> $BASH_ENV
          source $BASH_ENV
      - checkout
      - run:
          name: Loading all docker images
          command: |
            docker load -i /tmp/workspace/${GRAPHQL_SERVER_IMAGE}.tar
            docker load -i /tmp/workspace/${SUBSCRIBER_IMAGE}.tar
            docker load -i /tmp/workspace/${AUTHENTICATION_SERVER_IMAGE}.tar
            docker load -i /tmp/workspace/${FRONTEND_IMAGE}.tar
      - run:
          name: Pushing graphql server image
          command: bash ./hack/push --TYPE=ci --REPONAME=${REPONAME} --IMGNAME=${GRAPHQL_SERVER_IMAGE} --IMGTAG=${IMGTAG}
      - run:
          name: Pushing frontend image
          command: bash ./hack/push --TYPE=ci --REPONAME=${REPONAME} --IMGNAME=${FRONTEND_IMAGE} --IMGTAG=${IMGTAG}
      - run:
          name: Pushing auth server image
          command: bash ./hack/push --TYPE=ci --REPONAME=${REPONAME} --IMGNAME=${AUTHENTICATION_SERVER_IMAGE} --IMGTAG=${IMGTAG}
      - run:
          name: Pushing subscriber server image
          command: bash ./hack/push --TYPE=ci --REPONAME=${REPONAME} --IMGNAME=${SUBSCRIBER_IMAGE} --IMGTAG=${IMGTAG}
  release:
    machine:
      image: circleci/classic:201808-01
      resource_class: large
    steps:
      - attach_workspace:
          at: /tmp/workspace
      - run: |
          cat /tmp/workspace/env-vars >> $BASH_ENV
          source $BASH_ENV
      - checkout
      - run:
          name: Loading all docker images
          command: |
            docker load -i /tmp/workspace/${GRAPHQL_SERVER_IMAGE}.tar
            docker load -i /tmp/workspace/${SUBSCRIBER_IMAGE}.tar
            docker load -i /tmp/workspace/${AUTHENTICATION_SERVER_IMAGE}.tar
            docker load -i /tmp/workspace/${FRONTEND_IMAGE}.tar
      - run:
          name: Docker images
          command: docker images
      - run:
          name: Pushing graphql server image
          command: bash ./hack/push --TYPE=release --REPONAME=${REPONAME} --IMGNAME=${GRAPHQL_SERVER_IMAGE} --IMGTAG=${IMGTAG}
      - run:
          name: Pushing frontend image
          command: bash ./hack/push --TYPE=release --REPONAME=${REPONAME} --IMGNAME=${FRONTEND_IMAGE} --IMGTAG=${IMGTAG}
      - run:
          name: Pushing auth server image
          command: bash ./hack/push --TYPE=release --REPONAME=${REPONAME} --IMGNAME=${AUTHENTICATION_SERVER_IMAGE} --IMGTAG=${IMGTAG}
      - run:
          name: Pushing subscriber server image
          command: bash ./hack/push --TYPE=release --REPONAME=${REPONAME} --IMGNAME=${SUBSCRIBER_IMAGE} --IMGTAG=${IMGTAG}
workflows:
  version: 2
  litmus-portal:
    jobs:
      - frontend-services-checks:
          filters:
            tags:
              only: /.*/
      - backend-services-checks:
          filters:
            tags:
              only: /.*/
      - setup-env:
          filters:
            tags:
              only: /.*/
      - docker-build-frontend:
          requires:
            - setup-env
            - frontend-services-checks
            - backend-services-checks
          filters:
            tags:
              only: /.*/
      - docker-build-backend:
          requires:
            - setup-env
            - backend-services-checks
            - frontend-services-checks
          filters:
            tags:
              only: /.*/
      - docker-push:
          requires:
            - docker-build-backend
            - docker-build-frontend
          filters:
            branches:
              only:
                - master
                - /^v.*/
      - release:
          requires:
            - docker-build-backend
            - docker-build-frontend
          filters:
            ## release jobs needs to be run for tagged releases alone & not for any branch commits
            branches:
              ignore: /.*/
            tags:
              only: /.*/
