image: docker.km3net.de/base/python:3

# Change pip's cache directory to be inside the project directory since we can
# only cache local items.
variables:
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"

cache:
  paths:
    - .cache/pip

stages:
  - test
  - coverage
  - doc
  - release

.virtualenv_template: &virtualenv_definition |
  python -V
  python3 -m venv venv
  source venv/bin/activate
  pip install -U pip setuptools wheel setuptools_scm
  hash -r
  pip freeze
  make install-dev
  pip freeze

.junit_template: &junit_definition
    artifacts:
      reports:
        junit: "reports/junit*.xml"

test-py3.6:
    image: docker.km3net.de/base/python:3.6
    stage: test
    script:
        - *virtualenv_definition
        - make test
    <<: *junit_definition

test-py3.7:
    image: docker.km3net.de/base/python:3.7
    stage: test
    script:
        - *virtualenv_definition
        - make test
    <<: *junit_definition

test-py3.8:
    image: docker.km3net.de/base/python:3.8
    stage: test
    script:
        - *virtualenv_definition
        - make test
    <<: *junit_definition

test-py3.9:
    image: docker.km3net.de/base/python:3.9
    stage: test
    script:
        - *virtualenv_definition
        - make test
    <<: *junit_definition

test-py3.10:
    image: docker.km3net.de/base/python:3.10
    stage: test
    script:
        - *virtualenv_definition
        - make test
    <<: *junit_definition

test-py3.11:
    image: docker.km3net.de/base/python:3.11
    stage: test
    script:
        - *virtualenv_definition
        - make test
    <<: *junit_definition

test-py3.12:
    image: git.km3net.de:4567/common/dockerfiles/base/python:3.12
    stage: test
    script:
        - *virtualenv_definition
        - make test
    <<: *junit_definition

code-style:
    image: git.km3net.de:4567/common/dockerfiles/base/python:3.12
    stage: test
    script:
        - *virtualenv_definition
        - make black-check
    allow_failure: true

coverage:
    image: git.km3net.de:4567/common/dockerfiles/base/python:3.12
    stage: coverage
    script:
        - *virtualenv_definition
        - make install-dev
        - "make test-cov|grep TOTAL| awk '{printf \"COVERAGE: %.2f%%\", (1-$3/$2)*100 }'"
    coverage: '/COVERAGE:\s*([0-9]*\.[0-9]*%)/'
    artifacts:
        paths:
            - reports/coverage


build-docs:
    image: git.km3net.de:4567/common/dockerfiles/base/python:3.12
    stage: doc
    script:
        - *virtualenv_definition
        - cd doc && make clean && make html
    cache: {}

pages:
    image: git.km3net.de:4567/common/dockerfiles/base/python:3.12
    stage: doc
    script:
        - *virtualenv_definition
        - cd doc && make clean && make html
        - mv _build/html ../public/
        - cd .. && mv reports/coverage public/coverage
    artifacts:
        paths:
            - public
    cache: {}
    only:
        - tags
        - master


pypi:
    image: git.km3net.de:4567/common/dockerfiles/base/python:3.12
    stage: release
    cache: {}
    script:
        - pip install -U twine wheel setuptools_scm
        - python setup.py sdist
        - python setup.py bdist_wheel
        - twine upload dist/*
    only:
        - tags
    allow_failure: true
