# Java Maven CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-java/ for more details
#
version: 2.1

orbs:
  # defined at https://github.com/snowdrop/circleci-orbs
  # needs to have been published before being used here
  openshift: snowdrop/openshift@0.3.0

jobs:
  JDK8:
    docker:
      # specify the version you desire here
      - image: circleci/openjdk:8-jdk

      # Specify service dependencies here if necessary
      # CircleCI maintains a library of pre-built images
      # documented at https://circleci.com/docs/2.0/circleci-images/
      # - image: circleci/postgres:9.4

    working_directory: ~/repo

    environment:
      # Customize the JVM maximum heap limit
      MAVEN_OPTS: -Xmx3200m

    steps:
      - checkout

      # Download and cache dependencies
      - restore_cache:
          keys:
          - v8-dependencies-{{ checksum "pom.xml" }}
          # fallback to using the latest cache if no exact match is found
          - v8-dependencies-

      - run: mvn -DskipTests clean install dependency:resolve-plugins dependency:go-offline
      - save_cache:
          paths:
            - ~/.m2
          key: v8-dependencies-{{ checksum "pom.xml" }}

      # run tests!
      - run: mvn clean integration-test -Pwith-examples -Pwith-tests
  JDK11:
    docker:
      # specify the version you desire here
      - image: circleci/openjdk:11-jdk

      # Specify service dependencies here if necessary
      # CircleCI maintains a library of pre-built images
      # documented at https://circleci.com/docs/2.0/circleci-images/
      # - image: circleci/postgres:9.4

    working_directory: ~/repo

    environment:
      # Customize the JVM maximum heap limit
      MAVEN_OPTS: -Xmx3200m

    steps:
      - checkout

      # Download and cache dependencies
      - restore_cache:
          keys:
          - v11-dependencies-{{ checksum "pom.xml" }}
          # fallback to using the latest cache if no exact match is found
          - v11-dependencies-

      - run: mvn -DskipTests clean install dependency:resolve-plugins dependency:go-offline
      - save_cache:
          paths:
            - ~/.m2
          key: v11-dependencies-{{ checksum "pom.xml" }}

      # run tests!
      - run: mvn clean integration-test -Pwith-examples -Pwith-tests

  OPENSHIFT_3_11:
    machine: true
    steps:
      - checkout

      # Download and cache dependencies
      - restore_cache:
          keys:
          - v8-dependencies-{{ checksum "pom.xml" }}
          # fallback to using the latest cache if no exact match is found
          - v8-dependencies-

      - run: mvn -DskipTests clean install dependency:resolve-plugins dependency:go-offline
      - save_cache:
          paths:
            - ~/.m2
          key: v8-dependencies-{{ checksum "pom.xml" }}
      - openshift/launch_openshift_311
      - run:
          name: Run build on openshift
          command: |
            oc login -u admin -p admin
            oc new-project itests
            oc import-image fabric8/s2i-java:2.3 --from=docker.io/fabric8/s2i-java:2.3 --confirm
            oc tag --source docker docker.io/fabric8/s2i-java:2.3 s2i-java:2.3
            mvn -B clean install -Pbuild -Pwith-examples -Pwith-tests

  CLI_OPTIONS:
    machine: true
    steps:
      - checkout

      # Download and cache dependencies
      - restore_cache:
          keys:
          - v8-dependencies-{{ checksum "pom.xml" }}
          # fallback to using the latest cache if no exact match is found
          - v8-dependencies-

      - run: mvn -DskipTests clean install dependency:resolve-plugins dependency:go-offline
      - save_cache:
          paths:
            - ~/.m2
          key: v8-dependencies-{{ checksum "pom.xml" }}
      - openshift/launch_openshift_311
      - run:
          name: Run build on openshift
          command: |
            oc login -u admin -p admin
            oc new-project itests
            mvn -B clean install -Pbuild
      - run:
          name: Run build using -Ddekorate.build=true on kubernetes
          command: |
            oc login -u admin -p admin
            oc new-project dekorate-k8s-build
            mvn -B clean install -DskipTests -Ddekorate.build=true -pl :spring-boot-on-kubernetes-example -Pwith-examples -Dapp.name=sbk-one
            docker images | grep sbk-one 
            IMAGES=`docker images | grep sbk-one | wc -l`
            if [ $IMAGES -lt 1 ]; then echo "Docker image sbk-one" exit 1; fi
      - run:
          name: Run build using -Ddekorate.deploy=true on kubernetes
          command: |
            oc login -u admin -p admin
            oc new-project dekorate-k8s-deploy
            mvn -B clean install -DskipTests -Ddekorate.deploy=true -pl :spring-boot-on-kubernetes-example -Pwith-examples -Dapp.name=sbk-two
            docker images 
            IMAGES=`docker images | grep sbk-two | wc -l`
            if [ $IMAGES -lt 1 ]; then exit 1; fi
            oc get deployments 
            DC=`oc get deployments | grep sbk | wc -l`
            if [ $DC -lt 1 ]; then echo "Deployment sbk-two not found."; exit 1; fi
      - run:
          name: Run build using -Ddekorate.build=true on openshift
          command: |
            oc login -u admin -p admin
            oc new-project dekorate-os-build
            oc import-image fabric8/s2i-java:2.3 --from=docker.io/fabric8/s2i-java:2.3 --confirm
            oc tag --source docker docker.io/fabric8/s2i-java:2.3 s2i-java:2.3
            mvn -B clean install -DskipTests -Ddekorate.build=true -pl :spring-boot-on-openshift-example -Pwith-examples -Dapp.name=sbo-one
            docker images 
            IMAGES=`oc get istag | grep sbo-one | wc -l`
            if [ $IMAGES -lt 1 ]; then echo "Docker image sbo-one"; exit 1; fi
      - run:
          name: Run build using -Ddekorate.deploy=true on openshift
          command: |
            oc login -u admin -p admin
            oc new-project dekorate-os-deploy
            oc import-image fabric8/s2i-java:2.3 --from=docker.io/fabric8/s2i-java:2.3 --confirm
            oc tag --source docker docker.io/fabric8/s2i-java:2.3 s2i-java:2.3
            mvn -B clean install -DskipTests -Ddekorate.deploy=true -pl :spring-boot-on-openshift-example -Pwith-examples -Dapp.name=sbo-two
            docker images | grep sbo-two
            IMAGES=`docker images | grep sbo-two | wc -l`
            if [ $IMAGES -lt 1 ]; then echo "Docker image sbo-two not found."; exit 1; fi
            oc get dc 
            DCS=`oc get dc | grep sbo | wc -l`
            if [ $DCS -lt 1 ]; then echo "DeploymentConfig sbo-two not found"; exit 1; fi
workflows:
  version: 2
  build_and_test:
    jobs:
      - JDK8
      - JDK11
      - OPENSHIFT_3_11
      - CLI_OPTIONS
