buildx icon indicating copy to clipboard operation
buildx copied to clipboard

Proposal: target pattern rule with bake

Open crazy-max opened this issue 3 years ago • 1 comments

Let's take the following definition with repeated and almost similar targets:

group "validate" {
  targets = ["validate-lint", "validate-vendor", "validate-docs", "validate-authors"]
}

target "validate-lint" {
  dockerfile = "./hack/dockerfiles/lint.Dockerfile"
  output = ["type=cacheonly"]
}

target "validate-vendor" {
  dockerfile = "./hack/dockerfiles/vendor.Dockerfile"
  target = "validate"
  output = ["type=cacheonly"]
}


target "validate-docs" {
  dockerfile = "./hack/dockerfiles/docs.Dockerfile"
  target = "validate"
  output = ["type=cacheonly"]
}

target "validate-authors" {
  dockerfile = "./hack/dockerfiles/authors.Dockerfile"
  target = "validate"
  output = ["type=cacheonly"]
}

A common pattern with bake is the ability to group and run targets in parallel. Atm we don't have a nice way of defining implicit target by using a pattern rule like GNU Make.

A target pattern contains the character % (exactly one of them) in the target; otherwise, it looks exactly like an ordinary target. The % matches any nonempty substring, while other characters match only themselves. A target pattern is composed of a % between a prefix and a suffix, either or both of which may be empty. The text between the prefix and the suffix is called the stem. Thus, when the pattern validate-% matches the target name validate-vendor, the stem is vendor.

Like GNU Make automatic vairables we can also have values computed afresh for each rule that is executed, based on the target and prerequisites of the rule within its own recipe. By taking the example above we could then write:

group "validate" {
  targets = ["validate-lint", "validate-vendor", "validate-docs", "validate-authors"]
}

target "validate-%" {
  dockerfile = "./hack/dockerfiles/${*}.Dockerfile"
  target = "${*}" != "lint" ? "validate" : ""
  output = ["type=cacheonly"]
}
  • ${*} defines the stem with which an implicit rule matches as explained above. (e.g. for validate-vendor would be vendor)
  • ${@} defines the target name. (e.g. for validate-vendor would be validate-vendor)

crazy-max avatar Sep 11 '22 18:09 crazy-max

Seems to be same issue as #1150

tonistiigi avatar Sep 12 '22 04:09 tonistiigi