version: 2.1

executors:
  docker-publisher:
    environment:
      IMAGE_NAME: bukuserver/bukuserver
    docker:
      - image: circleci/buildpack-deps:stretch

test-template: &test-template
  working_directory: ~/Buku
  environment:
    CI_FORCE_TEST: 1
  steps:
    - run: &init
        command: |
          apt update && apt install -y --no-install-recommends git
          pip install --upgrade pip
          pip install --upgrade setuptools flake8 pylint
    - checkout
    - run: &deps
        command: |
          pip install -e .[tests]
          pip install -r requirements.txt
    - run:
        command:
          python3 -m pytest ./tests/test_*.py --cov buku -vv --durations=0 -c ./tests/pytest.ini

lint-template: &lint-template
  working_directory: ~/Buku
  environment:
    CI_FORCE_TEST: 1
  steps:
    - run: *init
    - checkout
    - run: *deps
    - run:
        command: |
          python3 -m flake8
          echo buku | xargs pylint --rcfile tests/.pylintrc
          find . -iname "*.py" ! -path "./api/*" | xargs pylint --rcfile tests/.pylintrc

jobs:
  lint:
    docker:
      - image: python:3.13-slim
    <<: *lint-template

  py39:
    docker:
      - image: python:3.9-slim
    <<: *test-template

  py310:
    docker:
      - image: python:3.10-slim
    <<: *test-template

  py311:
    docker:
      - image: python:3.11-slim
    <<: *test-template

  py312:
    docker:
      - image: python:3.12-slim
    <<: *test-template

  py313:
    docker:
      - image: python:3.13-slim
    <<: *test-template

#  package-and-publish:
#    machine: true
#    working_directory: ~/Buku
#    steps:
#      - checkout
#      - run:
#          name: "package with packagecore"
#          command: |
#            # Use latest installed python3 from pyenv
#            export PYENV_VERSION="$(pyenv versions | grep -Po '\b3\.\d+\.\d+' | tail -1)"
#            pip install packagecore
#            packagecore -o ./dist/ ${CIRCLE_TAG#v}
#      - run:
#          name: "publish to GitHub"
#          command: |
#            go get github.com/tcnksm/ghr
#            ghr -t ${GITHUB_API_TOKEN} -u ${CIRCLE_PROJECT_USERNAME} -r ${CIRCLE_PROJECT_REPONAME} -c ${CIRCLE_SHA1} -replace ${CIRCLE_TAG} ./dist/

  build-docker-image:
    executor: docker-publisher
    steps:
      - checkout
      - setup_remote_docker
      - run:
          name: Build Docker image
          command: docker build -t $IMAGE_NAME:latest .
      - run:
          name: Archive Docker image
          command: docker save -o image.tar $IMAGE_NAME
      - persist_to_workspace:
          root: .
          paths:
            - ./image.tar

  publish-on-docker-hub:
    executor: docker-publisher
    steps:
      - attach_workspace:
          at: /tmp/workspace
      - setup_remote_docker
      - run:
          name: Load archived Docker image
          command: docker load -i /tmp/workspace/image.tar
      - run:
          name: Publish Docker Image to Docker Hub
          command: |
            echo "${DOCKERHUB_PASS}" | docker login -u "${DOCKERHUB_USERNAME}" --password-stdin
            docker push ${IMAGE_NAME}:latest

workflows:
  version: 2.1

  CircleCI:
    jobs: &all-tests
      - lint
      - py39
      - py310
      - py311
      - py312
      - py313

  nightly:
    triggers:
      - schedule:
          cron: "0 0 * * 6"
          filters:
            branches:
              only:
                - master
    jobs: *all-tests

#  publish-github-release:
#    jobs:
#      - package-and-publish:
#          filters:
#            tags:
#              only: /^v.*/
#            branches:
#              ignore: /.*/

  publish-docker-image:
    jobs:
      - build-docker-image:
          filters:
            tags:
              only: /^v.*/
            branches:
              ignore: /.*/
      - publish-on-docker-hub:
          requires:
            - build-docker-image
          filters:
            tags:
              only: /^v.*/
            branches:
              ignore: /.*/
