#
# This file is a part of the CaosDB Project.
#
# Copyright (C) 2018 Research Group Biomedical Physics,
# Max-Planck-Institute for Dynamics and Self-Organization Göttingen
# Copyright (C) 2019 Henrik tom Wörden
# Copyright (C) 2020 Indiscale GmbH <info@indiscale.com>
# Copyright (C) 2020 Daniel Hornung <d.hornung@indiscale.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

variables:
  DEPLOY_REF: dev
  CI_REGISTRY_IMAGE: $CI_REGISTRY/caosdb/src/caosdb-pylib/testenv:latest
  # When using dind, it's wise to use the overlayfs driver for
  # improved performance.

image: $CI_REGISTRY_IMAGE

stages:
  - code_style
  - linting
  - setup
  - test
  - deploy

# check code style
code_style:
  tags: [ docker ]
  stage: code_style
  needs: [ ]
  script:
    - make style
  allow_failure: true

# pylint tests for pycaosdb
pylint:
  tags: [ docker ]
  stage: linting
  needs: [ ]
  script:
    - make lint
  allow_failure: true

# run unit tests
unittest_py3.7:
  tags: [ docker ]
  stage: test
  needs: [ ]
  image: python:3.7
  script: &python_test_script
    # Python docker has problems with tox and pip so use plain pytest here
    - touch ~/.pycaosdb.ini
    - pip install nose pytest pytest-cov python-dateutil jsonschema>=4.4.0
    - pip install .
    - python -m pytest unittests

unittest_py3.8:
  tags: [ docker ]
  stage: test
  needs: [ ]
  image: python:3.8
  script: *python_test_script

# This needs to be changed once Python 3.9 isn't the standard Python in Debian
# anymore.
unittest_py3.9:
  tags: [ docker ]
  stage: test
  needs: [ ]
  script:
    # verify that this actually is Python 3.9
    - python3 -c "import sys; assert sys.version.startswith('3.9')"
    - touch ~/.pycaosdb.ini
    - make unittest


unittest_py3.10:
  tags: [ docker ]
  stage: test
  needs: [ ]
  image: python:3.10
  script: *python_test_script

unittest_py3.11:
  tags: [ docker ]
  stage: test
  needs: [ ]
  image: python:3.11
  script: *python_test_script

# Trigger building of server image and integration tests
trigger_build:
  stage: deploy
  inherit:
    variables:
      - DEPLOY_REF
  variables:
    # Renaming variables.
    F_BRANCH: $CI_COMMIT_REF_NAME
    PYLIB: $CI_COMMIT_REF_NAME
    TRIGGERED_BY_REPO: PYLIB
    TRIGGERED_BY_REF: $CI_COMMIT_REF_NAME
    TRIGGERED_BY_HASH: $CI_COMMIT_SHORT_SHA

  trigger:
    project: caosdb/src/caosdb-deploy
    branch: $DEPLOY_REF
    strategy: depend

# Build a docker image in which tests for this repository can run
build-testenv:
  tags: [ cached-dind ]
  image: docker:19.03
  stage: setup
  only:
      - schedules
  script:
    - cd unittests/docker
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
      # use here general latest or specific branch latest...
    - docker pull $CI_REGISTRY_IMAGE|| true
    - docker build
      --pull
      --build-arg COMMIT=$CI_COMMIT_SHORT_SHA
      --cache-from $CI_REGISTRY_IMAGE
      -t $CI_REGISTRY_IMAGE .
    - docker push $CI_REGISTRY_IMAGE

# Build the sphinx documentation and make it ready for deployment by Gitlab Pages
# Special job for serving a static website. See https://docs.gitlab.com/ee/ci/yaml/README.html#pages
pages_prepare: &pages_prepare
  tags: [ cached-dind ]
  stage: deploy
  needs: [ code_style, pylint, unittest_py3.8, unittest_py3.9, unittest_py3.10 ]
  only:
    refs:
      - /^release-.*$/i
  script:
    - echo "Deploying"
    - make doc
    - cp -r build/doc/html public
  artifacts:
    paths:
      - public
pages:
  <<: *pages_prepare
  only:
    refs:
      - main