fixes #427: Handle empty strings in elements enabling conditional logic by AnthonyPoschen · Pull Request #428 · docker/buildx (original) (raw)

this change aims to resolve #427 by allowing either the compact function to be used and also allowing empty strings to be present in the resulting bake file and stripping them out of the final file before actioning.

example file 1 with compact.

variable "TAG" {default="" }

group "default" {
    targets = [
    "app",
  ]
}

target "app" {
  context="."
  dockerfile="Dockerfile"
  tags = "${compact([
      "my-image:latest",
      notequal("",TAG) ? "my-image:${TAG}-1": "",
      notequal("",TAG) ? "my-image:${TAG}-2": null,
      null,
    ])}"
}

Example 2 with allowing empty strings in arrays will no longer cause any issues with docker push as they are stripped.


group "default" {
    targets = [
    "app",
  ]
}

target "app" {
  context="."
  dockerfile="Dockerfile"
  tags = [
      "my-image:latest",
      notequal("",TAG) ? "my-image:${TAG}-1": "",
      notequal("",TAG) ? "my-image:${TAG}-2": "",
  ]
}

the above two config files results in the below print

❯ docker buildx bake --print
{
   "target": {
      "app": {
         "context": ".",
         "dockerfile": "Dockerfile",
         "tags": [
            "my-image:latest"
         ]
      }
   }
}

❯ TAG="ok" docker buildx bake --print
{
   "target": {
      "app": {
         "context": ".",
         "dockerfile": "Dockerfile",
         "tags": [
            "my-image:latest",
            "my-image:ok-1",
            "my-image:ok-2"
         ]
      }
   }
}```