# 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_job:
  image: registry.c4science.ch/scitas-stack/rhel:9.3
  parallel:
    matrix:
      # - environment: [jed]
      #   slurm_options: ["-c 72 -p jed -q jed"]
      #   stack: ["pinot-noir"]
      # - environment: [izar]
      #   slurm_options: ['-c 40 --gres gpu:2']
      #   apptainer_options: ['-nv']
      - environment: [helvetios]
        slurm_options: ["-c 36"]
        stack: ["pinot-noir"]
  tags:
    - ${environment}
    - stack
  variables:
    COMMAND_OPTIONS_SBATCH: ${slurm_options}
    STACK_CONFIG: ${CI_PROJECT_DIR}/stacks/${stack}/config.json
    STACK_LOCATION: $(jq -r .stack.mount_point ${STACK_CONFIG})
    APPTAINER_EXEC_OPTIONS: ${apptainer_options} --bind ${BUILDCACHE}:/buildcache:rw --bind ${CI_PROJECT_DIR}:${STACK_LOCATION}
    SPACK_USER_CACHE_PATH: "/tmp/spack_cache_${CI_JOB_ID}"
    SPACK_USER_CONFIG_PATH: "/tmp/spack_user_${CI_JOB_ID}"
    BUILDCACHE: "/work/scitas-ge/richart/ci/buildcache"
    LOG_LEVEL: 2

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

# ------------------------------------------------------------------------------
spack:setup:
  stage: environment
  extends:
    - .spack_cache
    - .parallel_job
  before_script:
    - git config --global --add --bool advice.detachedHead false
  script:
    - ci/setup_spack.sh

spack:install_compilers:
  stage: compilers
  extends:
    - .spack_cache
    - .parallel_job
  script:
    - ci/install_compilers.sh
    - spack/bin/spack -e stacks/${stack} config blame compilers
  after_script:
    - if [ ! -e spack-install.xml ]; then
      echo "{}" > spack-install.xml
      fi
  artifacts:
    reports:
      junit: spack-install.xml

  needs:
    - job: spack:setup
  timeout: 5h

spack:concretize:
  stage: concretize
  extends:
    - .spack_cache
    - .parallel_job
  script:
    - source stack_env.sh
    - spack/bin/spack -e ${SPACK_SYSTEM_CONFIG_PATH} config blame
    - spack/bin/spack -e ${SPACK_SYSTEM_CONFIG_PATH} concretize
  needs:
    - job: spack:install_compilers
  artifacts:
    paths:
      - stacks/${stack}/spack.lock
  timeout: 2h

spack:install:
  stage: install
  extends:
    - .spack_cache
    - .parallel_job
  script:
    - source stack_env.sh
    - spack/bin/spack -e ${SPACK_SYSTEM_CONFIG_PATH} install
      --log-file spack-install.xml
      --log-format junit
      --only-concrete
      --fail-fast
      --show-log-on-error
    - spack/bin/spack -e ${SPACK_SYSTEM_CONFIG_PATH} buildcache create
      --update-index
      --key EDC904DCE3D2E84E
      /buildcache
  after_script:
    - if [ ! -e spack-install.xml ]; then
      echo "{}" > spack-install.xml
      fi
  needs:
    - job: spack:concretize
  artifacts:
    reports:
      junit: spack-install.xml
  timeout: 10h

spack:mksquashfs:
  stage: deploy
  extends:
    - .spack_cache
    - .parallel_job
  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
