diff --git a/README.md b/README.md index 5550fcfe140..768c9d3ee8a 100644 --- a/README.md +++ b/README.md @@ -741,6 +741,43 @@ $ docker buildx bake --print webapp } ``` +Example of only adding tags if a variable is not empty using an `notequal` function: + +``` +$ cat <<'EOF' > docker-bake.hcl +variable "TAG" {default="" } + +group "default" { + targets = [ + "webapp", + ] +} + +target "webapp" { + context="." + dockerfile="Dockerfile" + tags = [ + "my-image:latest", + notequal("",TAG) ? "my-image:${TAG}": "", + ] +} +EOF + +$ docker buildx bake --print webapp +{ + "target": { + "webapp": { + "context": ".", + "dockerfile": "Dockerfile", + "tags": [ + "my-image:latest" + ] + } + } +} +``` + + ### `buildx imagetools create [OPTIONS] [SOURCE] [SOURCE...]` Imagetools contains commands for working with manifest lists in the registry. These commands are useful for inspecting multi-platform build results. diff --git a/bake/bake.go b/bake/bake.go index e5ee0ca95f6..a8c7c37f6b9 100644 --- a/bake/bake.go +++ b/bake/bake.go @@ -526,6 +526,9 @@ func removeDupes(s []string) []string { if _, ok := seen[v]; ok { continue } + if v == "" { + continue + } seen[v] = struct{}{} s[i] = v i++ diff --git a/bake/hcl.go b/bake/hcl.go index 6f19390efae..510d2e60a52 100644 --- a/bake/hcl.go +++ b/bake/hcl.go @@ -31,6 +31,7 @@ var ( "csvdecode": stdlib.CSVDecodeFunc, "coalesce": stdlib.CoalesceFunc, "coalescelist": stdlib.CoalesceListFunc, + "compact": stdlib.CompactFunc, "concat": stdlib.ConcatFunc, "contains": stdlib.ContainsFunc, "distinct": stdlib.DistinctFunc,