# yaml-language-server: $schema=gitlab-ci
# yaml-language-server: $format.enable=false

stages:
  - environment
  - compilers
  - concretize
  - install
  - deploy

workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
      when: never
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

variables:
  TERM: ansi

.parallel_definition:
  parallel:
    matrix:
      - environment: [jed]
        slurm_options: ["-c 72 -p jed -q jed"]
        stack: ["pinot-noir"]
        app_image: ["registry.c4science.ch/scitas-stack/rhel9-kuma"]
      - environment: [helvetios]
        slurm_options: ["-c 36"]
        stack: ["pinot-noir"]
        app_image: ["registry.c4science.ch/scitas-stack/rhel9-kuma"]
      # - environment: [izar]
      #   slurm_options: ['-c 40 --gpus 2']
      #   apptainer_options: ['-nv']
      #   stack: ["pinot-noir"]
      # - environment: [kuma-l40s]
      #   slurm_options: ['-c 64 --gres gpu:8']
      #   apptainer_options: ['-nv']
      #   stack: ["pinot-noir"]
      # - environment: [kuma-h100]
      #   slurm_options: ['-c 64 --gres gpu:4']
      #   apptainer_options: ['-nv']
      #   stack: ["pinot-noir"]
  tags:
    - ${environment}
    - stack
  variables:
    environment: ${environment}
  image: ${app_image}
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
.parallel_job:
  extends:
    - .parallel_definition
  variables:
    MOUNT_POINT: /stack
    BUILDCACHE: "/work/scitas-ge/richart/ci/buildcache"
    MIRROR: "/work/scitas-ge/richart/ci/mirror"
    COMMAND_OPTIONS_SBATCH: ${slurm_options}
    APPTAINER_EXEC_OPTIONS: ${apptainer_options} --cleanenv --no-home -H $(mktemp -d -p $CI_PROJECT_DIR/.tmp):$HOME --bind ${BUILDCACHE}:/buildcache:rw --bind ${MIRROR}:${MOUNT_POINT}/spack-mirror:rw --bind ${CI_PROJECT_DIR}:${MOUNT_POINT} --bind /dev/tty

.spack_cache:
  cache:
    key: ${stack}-${environment}-spack-cache
    policy: pull-push
    paths:
      - ${stack}

# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
spack:checkout:
  stage: .pre
  extends:
    - .parallel_definition
  before_script:
    - git config --global --add --bool advice.detachedHead false
  script:
    - ls
  timeout: 1h

spack:setup:
  stage: environment
  extends:
    - .parallel_job
    - .spack_cache
  script:
    - ci/setup_spack.sh
  needs:
    - job: spack:checkout
  after_script:
    - tar cvzf artifacts_${stack}_${environment}.tgz $stack/environment
  artifacts:
    paths:
      - artifacts_${stack}_${environment}.tgz

spack:install_compilers:
  stage: compilers
  extends:
    - .parallel_job
    - .spack_cache
  before_script:
    - if [ -e artifacts_${stack}_${environment}.tgz ];
      then
        tar xvzf artifacts_${stack}_${environment}.tgz;
      fi
  script:
    - ci/install_compilers.sh
    - source ci/stack_env.sh
    - ${STACK_LOCATION}/spack/bin/spack -e ${environment} config blame compilers
  after_script:
    - tar cvzf
        artifacts_${stack}_${environment}.tgz
        $stack/spack/var/spack/environments
  artifacts:
    paths:
      - artifacts_${stack}_${environment}.tgz
    reports:
      junit: spack-install-*.xml
  needs:
    - job: spack:setup
  timeout: 5h

spack:concretize:
  stage: concretize
  extends:
    - .parallel_job
    - .spack_cache
  before_script:
    - if [ -e artifacts_${stack}_${environment}.tgz ];
      then
        tar xvzf artifacts_${stack}_${environment}.tgz;
      fi
  script:
    - source ci/stack_env.sh
    - ${STACK_LOCATION}/spack/bin/spack -e ${environment} config blame
    - ${STACK_LOCATION}/spack/bin/spack -e ${environment} concretize --force
  after_script:
    - tar cvzf
        artifacts_${stack}_${environment}.tgz
        $stack/spack/var/spack/environments
  artifacts:
    paths:
      - artifacts_${stack}_${environment}.tgz
  needs:
    - job: spack:install_compilers
  timeout: 2h

spack:install:
  stage: install
  extends:
    - .parallel_job
    - .spack_cache
  before_script:
    - if [ -e artifacts_${stack}_${environment}.tgz ];
      then
        tar xvzf artifacts_${stack}_${environment}.tgz;
      fi
  script:
    - source ci/stack_env.sh
    - echo "{}" > spack-install-${environment}.xml

    - ${STACK_LOCATION}/spack/bin/spack -e ${environment} install
      --log-file spack-install-${environment}.xml
      --log-format junit
      --only-concrete
      --fail-fast
      --show-log-on-error

    - ${STACK_LOCATION}/spack/bin/spack -e ${environment}
      --update-index
      --key EDC904DCE3D2E84E
      /buildcache
  after_script:
    - tar cvzf
        artifacts_${stack}_${environment}.tgz
        $stack/spack/var/spack/environments
  needs:
    - job: spack:concretize
  artifacts:
    paths:
      - artifacts_${stack}_${environment}.tgz
    reports:
      junit: spack-install-*.xml
  timeout: 10h

spack:mksquashfs:
  stage: deploy
  extends:
    - .parallel_job
    - .spack_cache
  script:
    - "mksquashfs ${CI_PROJECT_DIR} stack-${stack}-${environment}-${CI_COMMIT_REF_SLUG}.sqfs"
    - 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file stack-${stack}-${environment}-${CI_COMMIT_REF_SLUG}.sqfs "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/stack/${stack}/stack-${stack}-${environment}-${CI_COMMIT_REF_SLUG}.sqfs"'
  needs:
    - job: spack:install
  timeout: 10h
