stages:
  - build
  - test
  - publish

#--------------------------
# Build templates
#--------------------------

.build-template: &build-template_definition
  stage: build
  script:
    - ci/fedora-packages.sh
    - ci/common-rpm-build.sh
    - mkdir ${CI_JOB_NAME}
    - cp -rv build/RPMS build/SRPMS ${CI_JOB_NAME}
    - tree ${CI_JOB_NAME}
  variables:
    BRANCH: ${CI_COMMIT_REF_NAME}
  artifacts:
    paths:
      - "$CI_JOB_NAME"

#--------------------------
# Build jobs
#--------------------------

alma8:
  image: gitlab-registry.cern.ch/linuxsupport/alma8-base
  <<: *build-template_definition

alma9:
  image: gitlab-registry.cern.ch/linuxsupport/alma9-base
  <<: *build-template_definition

fedora-39:
  image: fedora:39
  <<: *build-template_definition

fedora-40:
  image: fedora:40
  <<: *build-template_definition

fedora-rawhide:
  image: fedora:rawhide
  <<: *build-template_definition
  allow_failure: true
  when: manual

#--------------------------
# Test templates
#--------------------------

.test-template: &test-template_definition
  stage: test
  script:
    - dnf install -y git
    - PLATFORM=${CI_JOB_NAME%-*}
    - ci/write-repo-file.sh
    - dnf install -y ${PLATFORM}/RPMS/*/*.rpm
    - python3 test/functional/test_all.py
  variables:
    BRANCH: ${CI_COMMIT_REF_NAME}

#--------------------------
# Test jobs
#--------------------------

alma8-test:
  image: gitlab-registry.cern.ch/linuxsupport/alma8-base
  needs:
    - job: alma8
  before_script:
    - dnf install -y epel-release
  <<: *test-template_definition

alma9-test:
  image: gitlab-registry.cern.ch/linuxsupport/alma9-base
  needs:
    - job: alma9
  before_script:
    - dnf install -y epel-release
  <<: *test-template_definition

fedora-39-test:
  image: fedora:39
  needs:
    - job: fedora-39
  <<: *test-template_definition

fedora-40-test:
  image: fedora:40
  needs:
    - job: fedora-40
  <<: *test-template_definition

#--------------------------
# Publish templates
#--------------------------

.publish-template: &publish-template_definition
  stage: publish
  image: gitlab-registry.cern.ch/eos/gitlab-eos/alma9:latest
  dependencies:
    - alma8
    - alma9
    - fedora-39
    - fedora-40
    - fedora-rawhide
  script:
    - automount
    - cat "$repo_passwd" | kinit "$repo_user"
    - |
        for platform in alma8 alma9 fedora-39 fedora-40 fedora-rawhide; do
          if [[ "${platform}" == "fedora-rawhide" ]] && [[ ! -d ${platform} ]] ; then
            echo "Skipping ${platform} packages..." ;
            continue ;
          fi
          packaging/gfal2-repo-manager.py --action add --base /eos/project-d/dmc/www/repos/ --arch-dir x86_64 --ref ${CI_COMMIT_REF_NAME} --packages ${platform}/RPMS/*/*.rpm ${platform}/SRPMS/*
        done
  tags:
    - docker-privileged-xl
  retry: 2

#--------------------------
# Publish jobs
#--------------------------

rpms:
  <<: *publish-template_definition
  rules:
    - if: '$CI_PROJECT_NAMESPACE != "dmc"'
      when: never
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      when: never
    - if: '$CI_COMMIT_REF_NAME == "develop" || $CI_COMMIT_TAG != null'
    - if: '$CI_COMMIT_REF_NAME != "develop" && $CI_COMMIT_TAG == null'
      when: manual
