referenced:
  generate-hash-step: &generate-hash-step
    run:
      name: Generate external data hash
      command: |
        cd ITK
        find . -name \*.md5 -o -name \*.sha512 -print0 | xargs -0 git log -n 1 | tee /home/circleci/external-data.hashable
  restore-data-step: &restore-data-step
     restore_cache:
       keys:
         - 'v1-external-data-{{ checksum "/home/circleci/external-data.hashable" }}'
         - 'v1-external-data'

version: 2
jobs:
  build:
    docker:
      - image: circleci/python:3.7-stretch
    working_directory: ~/
    resource_class: medium
    branches:
      ignore:
        - gh-pages
        - dashboard
        - hooks
    environment:
      CTEST_DASHBOARD_ROOT: /home/circleci
      CTEST_SOURCE_DIRECTORY: /home/circleci/ITK
      CTEST_BINARY_DIRECTORY: /home/circleci/ITK-build
      DASHBOARD_BRANCH_DIRECTORY: /home/circleci/ITK-dashboard
      ExternalData_OBJECT_STORES: /home/circleci/.ExternalData
    steps:
      - checkout:
          path : ~/ITK
      - *generate-hash-step
      - *restore-data-step
      - restore_cache:
          keys:
            - ccache-{{ arch }}-{{ .Branch }}
            - ccache-{{ arch }}-master
            - ccache-{{ arch }}
      - run:
          name: Cloning dashboard branch
          command: |
            git clone --single-branch ${CIRCLE_REPOSITORY_URL} -b dashboard ${DASHBOARD_BRANCH_DIRECTORY}
      - run:
          name: Dependencies
          command: |
            sudo apt-get install -y  rsync ninja-build ccache
            sudo pip install --upgrade pip
            sudo pip install cmake==3.11.4 scikit-ci-addons numpy
      - run:
          name: CCache initialization
          command: |
            ccache --show-stats
            ccache --zero-stats
            ccache --max-size=2.0G
      - run: echo 'export DASHBOARD_MODEL=$( [[ "$CIRCLE_BRANCH" = "master" ]] && echo Continuous || echo Experimental )' >> $BASH_ENV
      - run:
          name: Build and Testing with CTest
          environment:
            ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS: 2
            CTEST_OUTPUT_ON_FAILURE: 1
            CTEST_CONFIGURATION_TYPE: "Release"
            CTEST_BUILD_FLAGS: "-j 2"
            PARALLEL_LEVEL: 2
            CTEST_CMAKE_GENERATOR: "Ninja"
          command: |
            export PATH=/usr/lib/ccache:${PATH}
            mkdir -p ${CTEST_BINARY_DIRECTORY}
            ctest -V -Ddashboard_no_clean:BOOL=1 \
              -DCTEST_CUSTOM_WARNING_EXCEPTION:STRING="itkIndex.h:.*warning: array subscript is above array bounds" \
              -S "${DASHBOARD_BRANCH_DIRECTORY}/circleci.cmake"
      - run:
          name: ccache stats
          when: always
          command: |
            ccache --show-stats
      - run:
          name: Formatting CTest for JUnit
          when: always
          command: |
            env
            mkdir -p /tmp/test-results
            ci_addons ctest_junit_formatter ${CTEST_BINARY_DIRECTORY} > /tmp/test-results/JUnit-${CIRCLE_NODE_INDEX}.xml
      - store_test_results:
          path: /tmp/test-results
          destination: ctest
      - save_cache:
          key: 'ccache-{{ arch }}-{{ .Branch }}-{{ epoch }}'
          paths: [ "/home/circleci/.ccache" ]
      - save_cache:
          key: 'v1-external-data-{{ checksum "/home/circleci/external-data.hashable" }}'
          paths: [ "/home/circleci/.ExternalData" ]
