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


before_script:
  - python3 ./bob/devtools/bootstrap.py -vv build
  - source ${CONDA_ROOT}/etc/profile.d/conda.sh
  - conda activate base

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


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


.build_linux_template:
  extends: .build_template
  variables:
    BUILD_EGG: "true"
  tags:
    - bob
    - docker
  image: quay.io/condaforge/linux-anvil-cos7-x86_64
  before_script:
    - git config --global --add safe.directory ${CI_PROJECT_DIR}
    - python3 ./bob/devtools/bootstrap.py -vv build
    - source ${CONDA_ROOT}/etc/profile.d/conda.sh
    - conda activate base
  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_macos_intel_template:
  extends: .build_template
  tags:
    - bob
    - macos
    - intel
  artifacts:
    paths:
      - ${CONDA_ROOT}/conda-bld/osx-64/*.conda
      - ${CONDA_ROOT}/conda-bld/osx-64/*.tar.bz2
  cache:
    key: "macos-intel-cache"


.build_macos_arm_template:
  extends: .build_template
  tags:
    - macos
    - arm
  artifacts:
    paths:
      - ${CONDA_ROOT}/conda-bld/osx-arm64/*.conda
      - ${CONDA_ROOT}/conda-bld/osx-arm64/*.tar.bz2
  cache:
    key: "macos-arm-cache"


code_quality:
  tags:
    - docker
  stage: qa
  image: quay.io/condaforge/linux-anvil-cos7-x86_64
  cache:
    key: "$CI_JOB_STAGE"
    paths:
      - cache/pre-commit

  before_script:
    - git config --global --add safe.directory ${CI_PROJECT_DIR}

  script:
    - sudo yum install -y git python3-pip
    - pip3 install pre-commit
    - pre-commit run --all-files --show-diff-on-failure


build_linux_bob_devel:
  extends: .build_linux_template
  variables:
    # The version of cuda at Idiap
    CONDA_OVERRIDE_CUDA: "11.6"
  # run bob-devel test only when bob/devtools/data/conda_build_config.yaml changes
  only:
    changes:
      - bob/devtools/data/conda_build_config.yaml
  script:
    - sudo yum install -y mesa-libGL-devel  # need this for pyopengl
    - python3 ./bob/devtools/build.py -vv build-bob-devel

build_linux_deps:
  extends: .build_linux_template
  # run deps test only when deps changes
  only:
    changes:
      - deps/**/*
  script:
    - python3 ./bob/devtools/build.py -vv build-deps

build_linux_bob_devtools:
  extends: .build_linux_template
  script:
    - python3 ./bob/devtools/build.py -vv build-devtools --twine-check

build_macos_intel_bob_devel:
  extends: .build_macos_intel_template
  # run bob-devel test only when bob/devtools/data/conda_build_config.yaml changes
  only:
    changes:
      - bob/devtools/data/conda_build_config.yaml
  script:
    - python3 ./bob/devtools/build.py -vv build-bob-devel

build_macos_arm_bob_devel:
  extends: .build_macos_arm_template
  # run bob-devel test only when bob/devtools/data/conda_build_config.yaml changes
  only:
    changes:
      - bob/devtools/data/conda_build_config.yaml
  script:
    - python3 ./bob/devtools/build.py -vv build-bob-devel

build_macos_intel_deps:
  extends: .build_macos_intel_template
  # run deps test only when deps changes
  only:
    changes:
      - deps/**/*
  script:
    - python3 ./bob/devtools/build.py -vv build-deps

build_macos_arm_deps:
  extends: .build_macos_arm_template
  # run deps test only when deps changes
  only:
    changes:
      - deps/**/*
  script:
    - python3 ./bob/devtools/build.py -vv build-deps

build_macos_intel_bob_devtools:
  extends: .build_macos_intel_template
  script:
    - python3 ./bob/devtools/build.py -vv build-devtools

build_macos_arm_bob_devtools:
  extends: .build_macos_arm_template
  script:
    - python3 ./bob/devtools/build.py -vv build-devtools


# Deploy targets
.release_template:
  image: quay.io/condaforge/linux-anvil-cos7-x86_64
  before_script:
    - git config --global --add safe.directory ${CI_PROJECT_DIR}
    - python3 ./bob/devtools/bootstrap.py -vv local base
    - source ${CONDA_ROOT}/etc/profile.d/conda.sh
    - conda activate base

  after_script:
    - python3 ./bob/devtools/bootstrap.py -vv local base
    - source ${CONDA_ROOT}/etc/profile.d/conda.sh
    - conda activate base
    - bdt ci clean -vv

  dependencies:
    - build_linux_bob_devel
    - build_linux_deps
    - build_linux_bob_devtools
    - build_macos_intel_bob_devel
    - build_macos_intel_deps
    - build_macos_intel_bob_devtools
    - build_macos_arm_bob_devel
    - build_macos_arm_deps
    - build_macos_arm_bob_devtools

  tags:
    - docker
    - bob
  cache:
    paths:
      - miniconda.sh


.deploy_template:
  extends: .release_template

  stage: deploy
  script:
    - bdt ci base-deploy -vv
    - bdt ci deploy -vv


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:
  extends: .release_template
  stage: pypi
  environment: pypi
  only:
    - /^v\d+\.\d+\.\d+([abc]\d*)?$/  # PEP-440 compliant version (tags)
  except:
    - branches
  script:
    - bdt ci pypi -vv dist/*.zip