# Definition of global variables (all stages)
variables:
  PYTHONUNBUFFERED: "1"
  CONDA_ROOT: "${CI_PROJECT_DIR}/miniconda"


# Definition of our build pipeline order
stages:
  - build
  - deploy
  - pypi


# Build targets
.build_template:
  stage: build
  artifacts:
    expire_in: 1 week
  cache:
    paths:
      - miniconda.sh


build_linux:
  extends: .build_template
  variables:
    BUILD_EGG: "true"
  tags:
    - docker
  image: continuumio/conda-concourse-ci
  before_script:
    - rm -f /root/.condarc
    - rm -rf /root/.conda
  script:
    - python3 ./bob/devtools/bootstrap.py -vv build
    - source ${CONDA_ROOT}/etc/profile.d/conda.sh
    - conda activate base
    - python3 ./bob/devtools/build.py -vv --twine-check
  artifacts:
    paths:
      - dist/*.zip
      - sphinx
      - ${CONDA_ROOT}/conda-bld/linux-64/*.conda
      - ${CONDA_ROOT}/conda-bld/noarch/*.conda
      - ${CONDA_ROOT}/conda-bld/linux-64/*.tar.bz2
      - ${CONDA_ROOT}/conda-bld/noarch/*.tar.bz2
  cache:
    key: "linux-cache"


build_macosx:
  extends: .build_template
  tags:
    - macosx
  script:
    - python3 ./bob/devtools/bootstrap.py -vv build
    - source ${CONDA_ROOT}/etc/profile.d/conda.sh
    - conda activate base
    - python3 ./bob/devtools/build.py -vv
  artifacts:
    paths:
      - ${CONDA_ROOT}/conda-bld/osx-64/*.conda
      - ${CONDA_ROOT}/conda-bld/noarch/*.conda
      - ${CONDA_ROOT}/conda-bld/osx-64/*.tar.bz2
      - ${CONDA_ROOT}/conda-bld/noarch/*.tar.bz2
  cache:
    key: "macosx-cache"


# Deploy targets
.deploy_template:
  stage: deploy
  before_script:
    - rm -f /root/.condarc
    - rm -rf /root/.conda
  script:
    - python3 ./bob/devtools/bootstrap.py -vv local base
    - source ${CONDA_ROOT}/etc/profile.d/conda.sh
    - conda activate base
    - bdt ci base-deploy -vv
    - bdt ci deploy -vv
    - bdt ci clean -vv
  dependencies:
    - build_linux
    - build_macosx
  tags:
    - docker
  cache:
    paths:
      - miniconda.sh


deploy_beta:
  extends: .deploy_template
  environment: beta
  only:
    - master


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


pypi:
  stage: pypi
  environment: pypi
  only:
    - /^v\d+\.\d+\.\d+([abc]\d*)?$/  # PEP-440 compliant version (tags)
  except:
    - branches
  before_script:
    - rm -f /root/.condarc
    - rm -rf /root/.conda
  script:
    - python3 ./bob/devtools/bootstrap.py -vv local base
    - source ${CONDA_ROOT}/etc/profile.d/conda.sh
    - conda activate base
    - bdt ci pypi -vv dist/*.zip
    - bdt ci clean -vv
  dependencies:
    - build_linux
    - build_macosx
  tags:
    - docker
  cache:
    paths:
      - miniconda.sh