# 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 == $CI_DEFAULT_BRANCH

variables:
  BUILDCACHE: "/scratch/richart/buildcache"
  SPACK_PATH: "/work/scitas-ge/richart/ci_cache/spack"
  STACK_PATH: "/work/scitas-ge/richart/ci_cache/stack"
  SPACK_DISABLE_LOCAL_CONFIG: "true"
  SPACK_USER_CACHE_PATH: "/tmp/spack_cache"
  SPACK_SITE_CONFIG_PATH: "/stack/spack/etc"
# ------------------------------------------------------------------------------
.parallel_job:
  image: registry.c4science.ch/scitas-stack/rhel:9.3
  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 --bind ${SPACK_PATH}:/spack:rw --bind ${STACK_PATH}:/stack:rw --writable-tmpfs

# ------------------------------------------------------------------------------
spack:setup:
  stage: environment
  extends:
    - .parallel_job
  script:
    - SPACK_VERSION=$(jq -r .spack.version config.json)

    - |
      if [ ! -d /spack/spack ]; then
        git clone https://github.com/spack/spack.git -b $SPACK_VERSION /spack/spack
      else
        cd /spack/spack
        git fetch
        git checkout $SPACK_VERSION
        cd -
      fi

    - /spack/spack/bin/spack gpg trust $GPG_PRIVATE_KEY

    - |
      mkdir -p /stack/extra_repos/

      EXTERNAL_REPOS=$(jq -r '.spack.repos | keys[]' config.json)

      echo "Repos: ${EXTERNAL_REPOS}"

      for repo in ${EXTERNAL_REPOS}
      do
        repo_branch=$(jq -r ".spack.repos.\"${repo}\".branch" config.json)
        if [ ! -d /stack/extra_repos/${repo} ]; then
          url_branch=$(jq -r ".spack.repos.\"${repo}\".url" config.json)
          git clone -b ${repo_branch} ${url_branch} /stack/extra_repos/$repo
        else
          cd /stack/extra_repos/$repo
          git fetch
          git checkout ${repo_branch}
          cd -
        fi
      done

    - |
      if [ ! -d /buildcache/build_cache ]; then
        /spack/spack/bin/spack gpg publish -d /buildcache
      fi

    - mkdir /stack/spack/etc


spack:install_compilers:
  stage: environment
  extends:
    - .parallel_job
  script:
    - COMPILER_LIST_SPECS=$(jq -r '.stack.compilers | map("\(.spec)@\(.version)")[]' config.json)
    - /spack/spack/bin/spack install
      --log-file spack-install.xml
      --log-format junit
      --fail-fast
      --show-log-on-error
      --use-cache
      ${COMPILER_LIST_SPECS}

    - echo "definitions: [ compilers: [ ${COMPILER_LIST_SPECS} ]]" > compiler_definitions.yaml

    - /spack/spack/bin/spack compiler find --scope site /usr

    - |
      for compiler in ${COMPILER_LIST_SPECS}; do
        /spack/spack/bin/spack compiler find --scope site $(/spack/spack/bin/spack location -i ${compiler})
      done

  artifacts:
    reports:
      junit: spack-install.xml

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

spack:install:
  stage: install
  extends:
    - .parallel_job
  script:
    - /spack/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
  artifacts:
    reports:
      junit: spack-install.xml
