version: "2.1"

# Parameters. {{{

parameters:

    # Bump this to reset all caches.
    cache_epoch:
      type: integer
      default: 2

# }}}

# Executors. {{{

executors:

  base:
    docker:
      - image: koreader/kobase:0.3.4-20.04
        auth:
          username: $DOCKER_USERNAME
          password: $DOCKER_PASSWORD

# }}}

# Jobs. {{{

jobs:

  # Build. {{{

  build:
    executor: base
    resource_class: medium
    environment:
      BASH_ENV: "~/.bashrc"
      CCACHE_MAXSIZE: "128M"
      CLICOLOR_FORCE: "1"
      EMULATE_READER: "1"
      MAKEFLAGS: "PARALLEL_JOBS=3 OUTPUT_DIR=build INSTALL_DIR=install"
    steps:
      # Checkout / fetch. {{{
      - checkout
      - run:
          name: Fetch
          command: .ci/fetch.sh
      # }}}
      # Check.
      - run:
          name: Check
          command: .ci/check.sh
      # Restore / setup caches. {{{
      - run:
          name: Generate cache key
          command: make TARGET= cache-key
      - restore_cache:
          name: Restore build directory
          keys:
            - &CACHE_KEY_BUILD_DIR '<< pipeline.parameters.cache_epoch >>-{{ .Environment.CIRCLE_JOB }}-build-{{ arch }}-{{ checksum "cache-key" }}'
      - restore_cache:
          name: Restore build cache
          keys:
            - &CACHE_KEY_BUILD_CACHE '<< pipeline.parameters.cache_epoch >>-{{ .Environment.CIRCLE_JOB }}-ccache-{{ arch }}-{{ checksum "cache-key" }}'
            - '<< pipeline.parameters.cache_epoch >>-{{ .Environment.CIRCLE_JOB }}-ccache-{{ arch }}-'
      - run:
          name: Setup build cache
          command: |
            set -x
            which ccache
            ccache --version
            ccache --zero-stats
            ccache --show-config
      # }}}
      # Build.
      - run:
          name: Build
          command: .ci/build.sh
      # Clean / save caches. {{{
      # We want to save cache prior to testing so we don't have to clean it up.
      - run:
          name: Clean caches
          when: always
          command: |
            set -x
            # Trim the build directory.
            rm -rf build/{cmake,staging,thirdparty}
            ccache --cleanup >/dev/null
            ccache --show-stats --verbose
      - save_cache:
          name: Save build cache
          key: *CACHE_KEY_BUILD_CACHE
          paths:
            - /home/ko/.cache/ccache
      - save_cache:
          name: Save build directory
          key: *CACHE_KEY_BUILD_DIR
          paths:
            - build
      # }}}
      # Tests / coverage. {{{
      # Our lovely unit tests.
      - run:
          name: Test
          command: .ci/test.sh
      # Upload coverage statistics (master branch only).
      - run:
          name: Uploading coverage
          command: .ci/after_success.sh
      # Store test results for better feedback / insights.
      - store_test_results:
          path: &TESTS_XML test-results.xml
      # CircleCI doesn't make the test results available as artifacts (October 2017).
      - store_artifacts:
          path: *TESTS_XML
      # }}}

  # }}}

  # Docs. {{{

  docs:
    executor: base
    resource_class: small
    environment:
      BASH_ENV: "~/.bashrc"
    steps:
      - checkout
      - run:
          name: fetch
          command: .ci/fetch.sh
      # Docs & translations (master branch only).
      - run:
          name: docs-and-translation
          command: .ci/after_success_docs_translation.sh

  # }}}

# }}}

# Workflows. {{{

workflows:

  version: 2

  build:

    jobs:

      - build

      - docs:
          context: koreader-vars
          filters:
            branches:
              only: master
          requires:
            - build

# }}}

# vim: foldmethod=marker foldlevel=0
