variables:
  GIT_VERSION: "2.41.0"
  PYTHON_VERSION: "3.10.8"
  DEFAULT_HELP_FORMAT: "man"

stages:
  - build
  - test
  - package

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

.build:
  stage: build
  script:
    # Install recent version of python required to build meson that is a requirement for building chain of git
    - curl -s -L -o python.zip https://gitlab.synchrotron-soleil.fr/software-control-system/devtools/python3/-/jobs/artifacts/${PYTHON_VERSION}/download?job=${PLATFORM}
    - unzip -q python.zip
    - rm python.zip
    - export PATH=$(readlink -e .)/python-${PYTHON_VERSION}-${PLATFORM}/bin:${PATH}
    - python --version | grep ${PYTHON_VERSION}
    - python -m pip install --upgrade pip 
    - python -m pip install conan
    # Install prerequisite for git-gui and gitk
    - sudo yum -y install tcl-devel tk-devel
    # Build git with conan
    - conan profile detect --force
    - conan create . --version ${GIT_VERSION} -s compiler.libcxx=libstdc++11 --build="*" 
    - conan install --requires git/${GIT_VERSION} -s compiler.libcxx=libstdc++11 --deployer=direct_deploy
    - mv direct_deploy/git ${CI_PROJECT_DIR}/${NAME}
  artifacts:
    name: ${NAME}
    paths:
      - ${NAME}

.test:
  stage: test
  script:
    - ${NAME}/bin/git --version | grep ${GIT_VERSION}

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

linux-x86_64:
  extends: [".build"]
  image: gitlab-registry.synchrotron-soleil.fr/software-control-system/containers/conan-docker-tools/conan-el6-gcc91
  variables:
    PLATFORM: linux-x86_64
    NAME: "git-${GIT_VERSION}-${PLATFORM}"

test-x86_64:
  extends: [".test"]
  image: gitlab-registry.synchrotron-soleil.fr/software-control-system/containers/conan-docker-tools/conan-el6-gcc91
  variables:
    PLATFORM: linux-x86_64
    NAME: "git-${GIT_VERSION}-${PLATFORM}"
  needs:
    - linux-x86_64

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

linux-i686:
  extends: [".build"]
  image: gitlab-registry.synchrotron-soleil.fr/software-control-system/containers/conan-docker-tools/conan-el6-gcc91-x86
  variables:
    PLATFORM: linux-i686
    NAME: "git-${GIT_VERSION}-${PLATFORM}"

test-i686:
  extends: [".test"]
  image: gitlab-registry.synchrotron-soleil.fr/software-control-system/containers/conan-docker-tools/conan-el6-gcc91-x86
  variables:
    PLATFORM: linux-i686
    NAME: "git-${GIT_VERSION}-${PLATFORM}"
  needs:
    - linux-i686

package-i686:
  extends: [".package"]
  image: gitlab-registry.synchrotron-soleil.fr/software-control-system/containers/conan-docker-tools/conan-el6-gcc91-x86
  variables:
    PLATFORM: linux-i686
    NAME: "git-${GIT_VERSION}-${PLATFORM}"
  needs:
    - linux-i686
    - test-i686