variables:
  CONAN_VERSION: "2.4.1"
  PYTHON_VERSION: "3.6.13"

workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == "schedule"
    - if: $CI_PIPELINE_SOURCE == "web"
    - if: $CI_COMMIT_TAG

stages:
  - build
  - test
  - package

.build:
  stage: build
  script:
    # Install prerequisites for pyinstaller
    - yum install -y zlib-devel
    # Install python 3
    - curl -s -L -o ${PYTHON_ROOT}.zip https://gitlab.synchrotron-soleil.fr/software-control-system/devtools/python3/-/jobs/artifacts/3.6.13/download?job=${PLATFORM}
    - unzip -q ${PYTHON_ROOT}.zip -d ${CI_PROJECT_DIR}
    - rm -f ${PYTHON_ROOT}.zip
    - export PATH=${CI_PROJECT_DIR}/${PYTHON_ROOT}/bin:$PATH
    - export LD_LIBRARY_PATH=${CI_PROJECT_DIR}/${PYTHON_ROOT}/lib
    # Install source of conan
    - curl -s -L -o conan-${CONAN_VERSION}.zip https://github.com/conan-io/conan/archive/refs/tags/${CONAN_VERSION}.zip
    - unzip -q conan-${CONAN_VERSION}.zip
    - cd ${CI_PROJECT_DIR}/conan-${CONAN_VERSION}
    - pip install -e .
    - pip install pyinstaller
    - python pyinstaller.py
    - mv ${CI_PROJECT_DIR}/conan-${CONAN_VERSION}/pyinstaller/dist/conan ${CI_PROJECT_DIR}/${CONAN_ROOT}
    # Add Sectigo CA certificates 
    - cat ${CI_PROJECT_DIR}/conan_synchrotron-soleil_fr_interm.cer >> ${CI_PROJECT_DIR}/${CONAN_ROOT}/certifi/cacert.pem
  artifacts:
    name: ${CONAN_ROOT}
    paths:
      - ${CONAN_ROOT}

.test:
  stage: test
  script:
    - ${CI_PROJECT_DIR}/${CONAN_ROOT}/conan --version | grep ${CONAN_VERSION}

.package:
  stage: package
  script:
    - cd ${CI_PROJECT_DIR}/${CONAN_ROOT}
    - zip -r --symlinks ../${CONAN_ROOT}.zip *
    - 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file ../${CONAN_ROOT}.zip "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/conan/${CONAN_VERSION}/${CONAN_ROOT}"'

linux-x86_64:
  extends: [".build"]
  image: gitlab-registry.synchrotron-soleil.fr/software-control-system/containers/dev-docker-tools/dev-el6-gcc44
  variables:
    PLATFORM: "linux-x86_64"
    CONAN_ROOT: "conan-${CONAN_VERSION}-${PLATFORM}"
    PYTHON_ROOT: "python-${PYTHON_VERSION}-${PLATFORM}"

test-x86_64:
  extends: [".test"]
  image: gitlab-registry.synchrotron-soleil.fr/software-control-system/containers/dev-docker-tools/dev-el6-gcc44
  variables:
    PLATFORM: "linux-x86_64"
    CONAN_ROOT: "conan-${CONAN_VERSION}-${PLATFORM}"
    PYTHON_ROOT: "python-${PYTHON_VERSION}-${PLATFORM}"
  needs:
    - linux-x86_64

package-x86_64:
  extends: [".package"]
  image: gitlab-registry.synchrotron-soleil.fr/software-control-system/containers/dev-docker-tools/dev-el6-gcc44
  variables:
    PLATFORM: "linux-x86_64"
    CONAN_ROOT: "conan-${CONAN_VERSION}-${PLATFORM}"
  needs:
    - linux-x86_64
    - test-x86_64

linux-i686:
  extends: [".build"]
  image: gitlab-registry.synchrotron-soleil.fr/software-control-system/containers/dev-docker-tools/dev-el6-gcc44-x86
  variables:
    PLATFORM: "linux-i686"
    CONAN_ROOT: "conan-${CONAN_VERSION}-${PLATFORM}"
    PYTHON_ROOT: "python-${PYTHON_VERSION}-${PLATFORM}"

test-i686:
  extends: [".test"]
  image: gitlab-registry.synchrotron-soleil.fr/software-control-system/containers/dev-docker-tools/dev-el6-gcc44-x86
  variables:
    PLATFORM: "linux-i686"
    CONAN_ROOT: "conan-${CONAN_VERSION}-${PLATFORM}"
    PYTHON_ROOT: "python-${PYTHON_VERSION}-${PLATFORM}"
  needs:
    - linux-i686

package-i686:
  extends: [".package"]
  image: gitlab-registry.synchrotron-soleil.fr/software-control-system/containers/dev-docker-tools/dev-el6-gcc44-x86
  variables:
    PLATFORM: "linux-i686"
    CONAN_ROOT: "conan-${CONAN_VERSION}-${PLATFORM}"
    PYTHON_ROOT: "python-${PYTHON_VERSION}-${PLATFORM}"
  needs:
    - linux-i686
    - test-i686
