# SPDX-FileCopyrightText: 2024 Sarah Vaupel <sarah.vaupel@uniworx.de>
#
# SPDX-License-Identifier: AGPL-3.0-or-later

### IMPORTANT NOTICE ###
# Our pipeline consists of static and dynamic parts.
# 
# This file only contains the dynamic backend parts of our pipeline.
# Static jobs are defined in .gitlab-ci.yml.
# 
# The marker "#dyn#" (without quotes) will be replaced by concrete values.
###

variables:
  BACKEND_IMAGE_VERSION: #dyn#
  PARENT_PIPELINE_ID: #dyn#

stages:
  - compile
  - lint
  - test
  - release

default:
  image:
    name: ${CI_REGISTRY_IMAGE}/${CI_COMMIT_REF_SLUG}/backend:${BACKEND_IMAGE_VERSION}
    entrypoint: [""]
    docker:
      platform: x86_64
  before_script:
    - ./.gitlab-ci/pull-frontend-artifacts.pl "${REGISTRY_AUTH_TOKEN}" "${PARENT_PIPELINE_ID}"
    - unzip ./.artifacts.tmp/artifacts.zip
  artifacts:
    name: "${CI_JOB_NAME}-${CI_COMMIT_SHORT_SHA}"
    expire_in: "1 day"
  retry: 2
  interruptible: true


compile:
  stage: compile
  script:
    - make -- compile${PROD_BUILD}-backend IN_CI=true IN_CONTAINER=true
  artifacts:
    paths:
      - ${CI_PROJECT_DIR}/.stack-work/
      - bin/
  cache: &backend-cache

lint:
  stage: lint
  script:
    - make -- lint${PROD_BUILD}-backend IN_CI=true IN_CONTAINER=true
  cache: *backend-cache

test:
  stage: test
  needs:
    - compile
  script:
    - make -- test${PROD_BUILD}-backend IN_CI=true IN_CONTAINER=true
  cache: *backend-cache

container:
  stage: release
  image: ${IMAGE_BUILDER}
  needs:
    - compile
    - lint
    - test
  script:
    - IMAGE_TAG=`./.gitlab-ci/version.pl`
    - buildah bud -t ${IMAGE_TAG} --volume ${CI_PROJECT_DIR}/bin/:/tmp/uniworx-bin --file docker/fradrive/Dockerfile
    - buildah push --creds "${CI_REGISTRY_USER}:${CI_JOB_TOKEN}" ${IMAGE_TAG} ${CI_REGISTRY_IMAGE}/${IMAGE_TAG}
  rules:
    - if: '"${PROD_BUILD}" == "-prod"'
      when: always