version: 2.1

jobs:
  python3:
    docker:
      - image: cimg/python:3.9
    environment:
      - MINICONDA_PATH: ~/miniconda
      - CONDA_ENV_NAME: testenv
      - PYTHON_VERSION: "3.10"
      - OMP_NUM_THREADS: 2 # Avoid over-commit with HistGradientBoosting
    steps:
      - checkout
      - run: ./build_tools/circle/checkout_merge_commit.sh
      - restore_cache:
          key: saved-cache
      - run:
          command: ./build_tools/circle/build_doc.sh
          no_output_timeout: 30m
      - store_artifacts:
          path: doc/_build/html
          destination: doc
      - store_artifacts:
          path: ~/log.txt
          destination: log.txt
      # Persists generated documentation so that it can be attached and deployed
      # in the 'deploy' step.
      - persist_to_workspace:
          root: doc/_build/html
          paths: .
      - save_cache:
          key: saved-cache
          paths:
            - /home/circleci/skrub_data

  deploy:
    docker:
      - image: cimg/python:3.9
    steps:
      - checkout
      - run: ./build_tools/circle/checkout_merge_commit.sh
      # Attach documentation generated in the 'python3' step so that it can be
      # deployed.
      - attach_workspace:
          at: doc/_build/html
      - run: ls -ltrh doc/_build/html
      - deploy:
          command: |
            if [[ "${CIRCLE_BRANCH}" =~ ^main$|^[0-9]+\.[0-9]+\.X$ ]]; then
              bash build_tools/circle/push_doc.sh doc/_build/html
            fi

workflows:
  version: 2
  build-doc-and-deploy:
    jobs:
      - python3
      - deploy:
          requires:
            - python3
