defaults: &defaults
  working_directory: ~/shadow
  docker:
    - image: circleci/openjdk:8-jdk
  environment:
    - TERM: "dumb"
    - GRADLE_OPTS: "-Xmx1024m"

version: 2
jobs:
  build:
    <<: *defaults
    steps:
      - checkout

      - restore_cache:
          keys:
            - v1-gradle-{{ .Branch }}-{{ checksum "gradle/wrapper/gradle-wrapper.properties" }}-{{ checksum "gradle/dependencies.gradle" }}
            - v1-gradle-{{ .Branch }}-{{ checksum "gradle/wrapper/gradle-wrapper.properties" }}
            - v1-gradle-{{ .Branch }}
            - v1-gradle-master
            - v1-gradle

      - run: ./gradlew downloadDependencies

      - save_cache:
          key: v1-gradle-{{ .Branch }}-{{ checksum "gradle/wrapper/gradle-wrapper.properties" }}-{{ checksum "gradle/dependencies.gradle" }}
          paths:
            - ~/.gradle

      - run: ./gradlew build --scan

      - run:
          name: Save test results
          command: |
            mkdir -p ~/junit/
            find . -type f -regex ".*/build/test-results/.*xml" -exec cp {} ~/junit/ \;
          when: always

      - store_test_results:
          path: ~/junit

      - store_artifacts:
          path: ~/junit

      - persist_to_workspace:
          root: ~/shadow
          paths:
            - build/*

  deploy-snapshot:
    <<: *defaults
    steps:
      - checkout

      - attach_workspace:
          at: ~/build

      - run: ./gradlew artifactoryPublish

workflows:
  version: 2
  build_and_publish:
    jobs:
      - build:
          filters:
            branches:
              ignore: gh-pages
      - deploy-snapshot:
          requires:
            - build
          filters:
            branches:
              only: master
