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

stages:
  - environment
  - 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 == "release/*/v*"

variables:
  BUILDCACHE: /scratch/richart/buildcache

# ------------------------------------------------------------------------------
.parallel_job:
  parallel:
    matrix:
      - environment: [helvetios]
        slurm_options: ["-c 36"]
      # - environment: [jed]
      # - environment: [izar]
      #   slurm_options: ['-c 40 --gres gpu:2']
      #   apptainer_options: ['-nv']
  tags:
    - ${environment}
    - stack
  variables:
    COMMAND_OPTIONS_SBATCH: ${slurm_options}
    APPTAINER_EXEC_OPTIONS: ${apptainer_options} --bind ${BUILDCACHE}:/buildcache:rw --writable-tmpfs

.setup_spack:
  cache:
    key: extra-repos-${environment}
    paths:
      - /stack/extra_repos
  before_script:
    - /opt/spack/bin/spack gpg trust $GPG_PRIVATE_KEY

# ------------------------------------------------------------------------------
spack:setup:
  stage:
    - environment
  extends:
    - .parallel_job
    - .setup_spack
  script:
    - >
      if [ ! -d /buildcache/build_cache ]; then
        /opt/spack/bin/spack gpg publish -d /buildcache
      fi

    - mkdir -p /stack/extra_repos
    - cd /stack/extra_repos

    - git clone https://gitlab.epfl.ch/SCITAS/software-stack/spack-repo-externals.git
    - git clone https://gitlab.epfl.ch/SCITAS/software-stack/scitas-spack-packages.git

spack:concretize:
  stage:
    - concretize
  extends:
    - .parallel_job
    - .setup_spack
  script:
    - /opt/spack/bin/spack -e . concretize
  needs:
    - job: spack:setup
  artefacts:
    paths:
      - spack.lock

spack:install:
  stage:
    - install
  extends:
    - .parallel_job
    - .setup_spack
  script:
    - /opt/spack/bin/spack -e . install
      --log-file spack-install.xml
      --log-format junit
      --only-concrete
      --fail-fast
      --show-log-on-error
      --use-cache
  needs:
    - job: spack:concretize
