default:
  image: sbtscala/scala-sbt:eclipse-temurin-jammy-8u352-b08_1.8.3_2.13.10

  cache:
    key: "$CI_COMMIT_REF_SLUG"
    paths:
      - "sbt-cache"
      - "target/streams"
      - "lib/ocelot-brain/target"

variables:
  GIT_SUBMODULE_STRATEGY: normal
  SBT_OPTS: "-Dsbt.global.base=sbt-cache/.sbtboot -Dsbt.boot.directory=sbt-cache/.boot -Dsbt.ivy.home=sbt-cache/.ivy"
  SBT_CACHE_DIR: "sbt-cache/.ivy/cache"
  PACKAGE_REGISTRY_URL: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/ocelot-desktop/${CI_COMMIT_TAG}"
  PACKAGE_NAME: "ocelot-desktop-${CI_COMMIT_TAG}.jar"

stages:
  - build
  - test
  - upload
  - deploy
  - release

test:
  stage: test
  before_script:
    - sbt -v sbtVersion
  script:
    - sbt test

# Rest in piece.
#scalafmt:
#  stage: test
#  allow_failure: true
#  before_script:
#    - sbt -v sbtVersion
#  script:
#    - sbt scalafmtCheckAll

build:
  stage: build
  before_script:
    - sbt -v sbtVersion
  script:
    - sbt assembly
  artifacts:
    paths:
      - target/scala-2.13/ocelot-desktop.jar

upload:
  stage: upload
  image: curlimages/curl:latest
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - |
      curl --fail-with-body --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file target/scala-2.13/ocelot-desktop.jar "${PACKAGE_REGISTRY_URL}/${PACKAGE_NAME}"

pages:
  stage: deploy
  before_script: []
  rules:
    - if: $CI_COMMIT_BRANCH == "master" || $CI_COMMIT_BRANCH == "develop"
  script:
    - rm -rf public
    - mkdir public
    - cp target/scala-2.13/ocelot-desktop.jar public/ocelot.jar
  cache: {}
  artifacts:
    paths:
      - public

release:
  stage: release
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  rules:
    - if: $CI_COMMIT_TAG
  before_script:
    - apk add git
  script:
    - echo "Creating a new release for tag $CI_COMMIT_TAG."
    - |
      release-cli create \
        --name "Release $CI_COMMIT_TAG" \
        --tag-name "$CI_COMMIT_TAG" \
        --description "$(git tag -l "${CI_COMMIT_TAG}" --format="%(contents:body)")" \
        --assets-link "{\"name\":\"${PACKAGE_NAME}\",\"link_type\":\"package\",\"url\":\"${PACKAGE_REGISTRY_URL}/${PACKAGE_NAME}\"}"
