variable "REGISTRY" {
  default = "repo.nstart.cloud"
}

variable "NAMESPACE" {
  default = "nstart"
}

variable "VERSION" {
  default = "latest"
}

variable "IMAGE_NAME" {
  default = "pcp-tgu-ui-service"
}

function "oci_labels" {
  params = []
  result = {
    "org.opencontainers.image.version"     = VERSION,
    "org.opencontainers.image.vendor"      = "New Start",
    "org.opencontainers.image.title"       = "ТГУ — Операции",
    "org.opencontainers.image.description" = "ТГУ — Операции frontend application.",
    "org.opencontainers.image.licenses"    = "LicenseRef-Proprietary",
    "org.opencontainers.image.ref.name"    = VERSION
  }
}

function "image_ref" {
  params = [name]
  result = REGISTRY != "" ? "${REGISTRY}/${NAMESPACE}/${name}" : "${NAMESPACE}/${name}"
}

target "common" {
  platforms = ["linux/amd64"]
  output    = ["type=image"]

  attest = [
    "type=sbom,generator=${REGISTRY}/docker/buildkit-syft-scanner:stable-1",
    "type=provenance,mode=max"
  ]
}

target "app" {
  inherits   = ["common"]
  context    = "."
  dockerfile = "Dockerfile"

  tags = [
    "${image_ref(IMAGE_NAME)}:${VERSION}",
    "${image_ref(IMAGE_NAME)}:latest"
  ]

  labels = oci_labels()
}

target "save" {
  inherits = ["app"]

  output = [
    "type=oci,dest=./dist/${NAMESPACE}-${IMAGE_NAME}-${VERSION}.tar"
  ]
}

group "all" {
  targets = ["app"]
}
