version: 2.1

orbs:
  slack: circleci/slack@4.10.1
  localstack: localstack/platform@1.0

commands:
  store_jacoco_site:
    parameters:
      module:
        type: string
    steps:
      - store_artifacts:
          path: << parameters.module >>/target/site/jacoco/index.html
      - store_artifacts:
          path: << parameters.module >>/target/site/jacoco/jacoco-resources

  install_java17:
    steps:
      - run:
          name: Install Java 17
          command: |
            sudo apt-get update
            sudo apt install -y openjdk-17-jdk
            sudo rm /etc/alternatives/java
            sudo ln -s /usr/lib/jvm/java-17-openjdk-amd64/bin/java /etc/alternatives/java

  mvn_verify:
    steps:
      - checkout
      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "pom.xml" }}
      - run:
          name: Write .env
          command: |
            echo "RDBMS_PASSWORD=$RDBMS_PASSWORD" >> qqq-sample-project/.env
      - run:
          name: Run Maven Verify
          command: |
            mvn -s .circleci/mvn-settings.xml -T4 verify
      - store_jacoco_site:
          module: qqq-backend-core
      - store_jacoco_site:
          module: qqq-backend-module-filesystem
      - store_jacoco_site:
          module: qqq-backend-module-rdbms
      - store_jacoco_site:
          module: qqq-middleware-javalin
      - store_jacoco_site:
          module: qqq-middleware-picocli
      - store_jacoco_site:
          module: qqq-sample-project
      - run:
          name: Save test results
          command: |
            mkdir -p ~/test-results/junit/
            find . -type f -regex ".*/target/surefire-reports/.*xml" -exec cp {} ~/test-results/junit/ \;
          when: always
      - store_test_results:
          path: ~/test-results
      - save_cache:
          paths:
            - ~/.m2
          key: v1-dependencies-{{ checksum "pom.xml" }}

  mvn_jar_deploy:
    steps:
      - checkout
      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "pom.xml" }}
      - run:
          name: Run Maven Jar Deploy
          command: |
            mvn -s .circleci/mvn-settings.xml -T4 flatten:flatten jar:jar deploy:deploy
      - save_cache:
          paths:
            - ~/.m2
          key: v1-dependencies-{{ checksum "pom.xml" }}

jobs:
  mvn_test:
    executor: localstack/default
    steps:
      - localstack/startup
      - install_java17
      - mvn_verify
      - slack/notify:
          event: fail

  mvn_deploy:
    executor: localstack/default
    steps:
      - localstack/startup
      - install_java17
      - mvn_verify
      - mvn_jar_deploy
      - slack/notify:
          event: always

workflows:
  test_only:
    jobs:
      - mvn_test:
          context: [ qqq-maven-registry-credentials, kingsrook-slack, build-qqq-sample-app ]
          filters:
            branches:
              ignore: /dev/
            tags:
              ignore: /(version|snapshot)-.*/

  deploy:
    jobs:
      - mvn_deploy:
          context: [ qqq-maven-registry-credentials, kingsrook-slack, build-qqq-sample-app ]
          filters:
            branches:
              only: /dev/
            tags:
              only: /(version|snapshot)-.*/

