stages:
  - test
  - build
  - release

variables:
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
cache:
  paths:
    - .cache/pip

linter:
  stage: test
  image: python:3

  cache:
    paths:
      - .cache/pip
      - .cache/pre-commit

  except:
    - schedules

  variables:
    PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
    PRE_COMMIT_HOME: "$CI_PROJECT_DIR/.cache/pre-commit"

  before_script:
    - pip install pre-commit

  script:
    - pre-commit run -a

tests:
  stage: test
  image: python:3.7

  cache:
    paths:
      - .cache/pip

  except:
    - schedules

  variables:
    PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"

  before_script:
    - pip install tox

  script:
    - tox -e py37-unit

release-pypi:
  stage: release
  only:
    - tags
  environment:
    name: pypi
    url: https://pypi.org/project/teklia-toolbox

  before_script:
    - pip install twine setuptools wheel
    - echo "[distutils]" > ~/.pypirc
    - echo "index-servers =" >> ~/.pypirc
    - echo "  pypi" >> ~/.pypirc
    - echo "[pypi]" >> ~/.pypirc
    - echo "repository=https://upload.pypi.org/legacy/" >> ~/.pypirc
    - echo "username=$PYPI_release_USERNAME" >> ~/.pypirc
    - echo "password=$PYPI_release_PASSWORD" >> ~/.pypirc
  script:
    - python setup.py sdist bdist_wheel
    - twine upload dist/* -r pypi

release-notes:
  stage: release
  image: registry.gitlab.com/teklia/devops:latest

  only:
    - tags

  script:
    - devops release-notes

bump-python-deps:
  stage: build
  image: registry.gitlab.com/teklia/devops:latest

  only:
    - schedules

  script:
    - devops python-deps requirements.txt base/requirements.txt tests-requirements.txt
