variables:
  CONDA_ROOT: "${CI_PROJECT_DIR}/miniconda"
  BOOTSTRAP: "https://gitlab.idiap.ch/bob/bob.devtools/raw/master/bob/devtools/bootstrap.py"


# This build does not deploy to PyPI
stages:
  - build
  - deploy


build_linux_36:
  stage: build
  variables:
    PYTHON_VERSION: "3.6"
    TEST_PREFIX_PATH_FILE: 'test_prefix_$CI_JOB_ID.txt'
  tags:
    - docker-build
  script:
    - curl --silent "${BOOTSTRAP}" --output "bootstrap.py"
    - python3 bootstrap.py -vv channel base
    - source ${CONDA_ROOT}/etc/profile.d/conda.sh
    - export BEAT_TEST_PREFIX=`mktemp -d --tmpdir=/var/tmp beat_test_prefix.XXXXXXXXX`
    - echo $BEAT_TEST_PREFIX > $TEST_PREFIX_PATH_FILE
    - conda env create -f dev.yml
    - conda activate beat-web
    - buildout
    - python -c "from beat.core.test.utils import pull_docker_test_images as f; f()"
    - export COVERAGE_FILE=.coverage.django
    - ./bin/coverage run --source=${CI_PROJECT_NAME} ./bin/django test --settings=beat.web.settings.ci -v 2 --noinput
    - export BEAT_CMDLINE_TEST_PLATFORM="django://beat.web.settings.ci"
    - export COVERAGE_FILE=.coverage.cmdline
    - export NOSE_WITH_COVERAGE=1
    - export NOSE_COVER_PACKAGE=beat.web
    - ./bin/coverage run --source=beat.cmdline ./bin/nosetests -sv beat.cmdline
    - unset COVERAGE_FILE
    - ./bin/coverage combine .coverage.django .coverage.cmdline
    - ./bin/coverage report
    - ./bin/sphinx-apidoc --separate -d 2 --output=doc/api ${CI_PROJECT_NAMESPACE} beat/web/*/migrations beat/web/*/tests
    - ./bin/sphinx-build doc/api sphinx/api
    - ./bin/sphinx-build doc/admin sphinx/admin
    - ./bin/sphinx-build doc/user sphinx/user
    - conda deactivate
    - export BEAT_TEST_PREFIX=`cat $TEST_PREFIX_PATH_FILE`
    - rm -rf ${BEAT_TEST_PREFIX} ${TEST_PREFIX_PATH_FILE}
    - conda activate base
    - bdt ci clean -vv
  artifacts:
    expire_in: 1 week
    paths:
      - sphinx
  cache: &build_caches
    key: "linux-cache"
    paths:
      - miniconda.sh
      - ${CONDA_ROOT}/pkgs/*.tar.bz2
      - ${CONDA_ROOT}/pkgs/urls.txt


# Deploy targets
.deploy_template: &deploy_job
  stage: deploy
  script:
    - curl --silent "${BOOTSTRAP}" --output "bootstrap.py"
    - python3 bootstrap.py channel base
    - source ${CONDA_ROOT}/etc/profile.d/conda.sh
    - conda activate base
    - bdt ci deploy -vv
    - bdt ci clean -vv
  dependencies:
    - build_linux_36
  cache:
    <<: *build_caches
  tags:
    - docker


deploy_beta:
  <<: *deploy_job
  environment: beta
  only:
    - master


deploy_stable:
  <<: *deploy_job
  environment: stable
  only:
    - /^v\d+\.\d+\.\d+([abc]\d*)?$/  # PEP-440 compliant version (tags)
  except:
    - branches
