From 839b52f443a38c0149dd5cf52e776ce83d46eb8f Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Tue, 31 Mar 2026 13:06:07 +0200 Subject: [PATCH] make bake definitions extensible for downstream overrides Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- README.md | 49 +++++++++++++++++++++++++++++++ docker-bake.hcl | 78 ++++++++++++++++++++++++++++++++++--------------- 2 files changed, 103 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 4f3e1358..b10daa8c 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ ___ * [Build](#build) * [Requirements](#requirements) * [Usage](#usage) + * [Remote override](#remote-override) * [Contributing](#contributing) ## Release @@ -81,6 +82,54 @@ $ PKG_REF=v0.24.0 docker buildx bake pkg-buildx-* $ docker buildx bake --push --set *.tags=dockereng/packaging:buildx-v0.24.0 release-buildx ``` +### Remote override + +Downstream repositories can reuse this repository as a remote Bake definition +and extend it with a local `docker-bake.override.hcl` file instead of forking +the full `docker-bake.hcl`. + +Example local override: + +```hcl +variable "PKGS_EXTRA" { + default = ["my-package"] +} + +variable "PKG_PLATFORMS_EXTRA" { + default = { + my-package = ["linux/amd64"] + } +} + +variable "PKG_CONTEXTS_EXTRA" { + default = { + my-package = "cwd://pkg/my-package" + } +} + +target "_pkg-my-package" { + args = { + PKG_NAME = "my-package" + PKG_REPO = "https://github.com/example/my-package.git" + PKG_REF = "main" + } +} +``` + +Example invocation from the downstream repository: + +```shell +$ docker buildx bake \ + -f docker-bake.hcl \ + -f cwd://docker-bake.override.hcl \ + "https://github.com/docker/packaging.git#main" \ + pkg-my-package-debian12 +``` + +Use the `cwd://` prefix for local files and directories when combining a local +override with a remote Bake definition. Without it, Bake resolves paths relative +to the remote context instead of the current working directory. + ## Contributing Want to contribute? Awesome! You can find information about contributing to diff --git a/docker-bake.hcl b/docker-bake.hcl index 62d24561..64a9dac8 100644 --- a/docker-bake.hcl +++ b/docker-bake.hcl @@ -42,8 +42,8 @@ variable "DISTROS" { ] } -variable "PKGS" { - description = "List of packages to build from ./pkg directory. Don't forget to update _pkg-* target if you add/remove a package." +variable "PKGS_BASE" { + description = "Base list of packages built from this repo. Use PKGS_EXTRA in an override file to add downstream packages." default = [ "buildx", "compose", @@ -56,6 +56,49 @@ variable "PKGS" { ] } +variable "PKGS_EXTRA" { + description = "Additional packages to append from an override bake definition." + default = [] +} + +variable "PKGS" { + description = "Full list of packages to build from ./pkg directory or an override-provided package context." + default = concat(PKGS_BASE, PKGS_EXTRA) +} + +variable "PKG_PLATFORMS_BASE" { + description = "Base package platform mapping. Use PKG_PLATFORMS_EXTRA in an override bake definition to add downstream packages." + default = { + # https://github.com/docker/buildx/blob/0c747263ef1426f5fa217fcdb616eddf33da6c2d/docker-bake.hcl#L156-L174 + buildx = ["darwin/amd64", "darwin/arm64", "linux/amd64", "linux/arm/v6", "linux/arm/v7", "linux/arm64", "linux/ppc64le", "linux/riscv64", "linux/s390x", "windows/amd64", "windows/arm64"] + # https://github.com/docker/compose/blob/c626befee1596abcc74578cb10dd96ae1667f76f/docker-bake.hcl#L112-L124 + compose = ["darwin/amd64", "darwin/arm64", "linux/amd64", "linux/arm/v6", "linux/arm/v7", "linux/arm64", "linux/ppc64le", "linux/riscv64", "linux/s390x", "windows/amd64", "windows/arm64"] + # https://github.com/containerd/containerd/blob/87742bd35f6ddc47c638a448c271b7ccf8df9010/.github/workflows/ci.yml#L145-L165 + # https://github.com/containerd/containerd/blob/87742bd35f6ddc47c638a448c271b7ccf8df9010/.github/workflows/ci.yml#L135-L137 + containerd = ["linux/amd64", "linux/arm/v6", "linux/arm/v7", "linux/arm64", "linux/ppc64le", "linux/s390x", "windows/amd64", "windows/arm64", "windows/arm/v7"] + # https://github.com/docker/docker-credential-helpers/blob/f9d3010165b642df37215b1be945552f2c6f0e3b/docker-bake.hcl#L56-L66 + credential-helpers = ["darwin/amd64", "darwin/arm64", "linux/amd64", "linux/arm/v6", "linux/arm/v7", "linux/arm64", "linux/ppc64le", "linux/s390x", "windows/amd64"] + # https://github.com/docker/cli/blob/84038691220e7ba3329a177e4e3357b4ee0e3a52/docker-bake.hcl#L30-L42 + docker-cli = ["darwin/amd64", "darwin/arm64", "linux/386", "linux/amd64", "linux/arm/v6", "linux/arm/v7", "linux/arm64", "linux/ppc64le", "linux/riscv64", "linux/s390x", "windows/amd64", "windows/arm64"] + # https://github.com/moby/moby/blob/83264918d3e1c61341511e360a7277150b914b3f/docker-bake.hcl#L82-L91 + docker-engine = ["linux/amd64", "linux/arm/v6", "linux/arm/v7", "linux/arm64", "linux/ppc64le", "linux/s390x", "windows/amd64", "windows/arm64"] + # https://github.com/docker/model-runner/blob/039f7a31c0365f9161c9b9b6bb3888161d16e388/cmd/cli/Makefile#L39-L43 + model = ["darwin/amd64", "darwin/arm64", "linux/amd64", "linux/arm64", "linux/arm/v7", "windows/amd64", "windows/arm64"] + # https://github.com/docker/docker-agent/blob/5b9feaabe743a5ad577f2247ed55d5dcb2678e8b/Taskfile.yml#L79 + agent = ["darwin/amd64", "darwin/arm64", "linux/amd64", "linux/arm64", "windows/amd64", "windows/arm64"] + } +} + +variable "PKG_PLATFORMS_EXTRA" { + description = "Additional package platform mapping to merge from an override bake definition." + default = {} +} + +variable "PKG_CONTEXTS_EXTRA" { + description = "Additional package context mapping to merge from an override bake definition." + default = {} +} + variable "DISTRO_NAME" { description = "Name of the distro." default = null @@ -589,25 +632,12 @@ target "_pkg-agent" { # Returns the list of supported platforms for a given package. function "pkgPlatforms" { params = [pkg] - result = lookup({ - # https://github.com/docker/buildx/blob/0c747263ef1426f5fa217fcdb616eddf33da6c2d/docker-bake.hcl#L156-L174 - buildx = ["darwin/amd64", "darwin/arm64", "linux/amd64", "linux/arm/v6", "linux/arm/v7", "linux/arm64", "linux/ppc64le", "linux/riscv64", "linux/s390x", "windows/amd64", "windows/arm64"] - # https://github.com/docker/compose/blob/c626befee1596abcc74578cb10dd96ae1667f76f/docker-bake.hcl#L112-L124 - compose = ["darwin/amd64", "darwin/arm64", "linux/amd64", "linux/arm/v6", "linux/arm/v7", "linux/arm64", "linux/ppc64le", "linux/riscv64", "linux/s390x", "windows/amd64", "windows/arm64"] - # https://github.com/containerd/containerd/blob/87742bd35f6ddc47c638a448c271b7ccf8df9010/.github/workflows/ci.yml#L145-L165 - # https://github.com/containerd/containerd/blob/87742bd35f6ddc47c638a448c271b7ccf8df9010/.github/workflows/ci.yml#L135-L137 - containerd = ["linux/amd64", "linux/arm/v6", "linux/arm/v7", "linux/arm64", "linux/ppc64le", "linux/s390x", "windows/amd64", "windows/arm64", "windows/arm/v7"] - # https://github.com/docker/docker-credential-helpers/blob/f9d3010165b642df37215b1be945552f2c6f0e3b/docker-bake.hcl#L56-L66 - credential-helpers = ["darwin/amd64", "darwin/arm64", "linux/amd64", "linux/arm/v6", "linux/arm/v7", "linux/arm64", "linux/ppc64le", "linux/s390x", "windows/amd64"] - # https://github.com/docker/cli/blob/84038691220e7ba3329a177e4e3357b4ee0e3a52/docker-bake.hcl#L30-L42 - docker-cli = ["darwin/amd64", "darwin/arm64", "linux/386", "linux/amd64", "linux/arm/v6", "linux/arm/v7", "linux/arm64", "linux/ppc64le", "linux/riscv64", "linux/s390x", "windows/amd64", "windows/arm64"] - # https://github.com/moby/moby/blob/83264918d3e1c61341511e360a7277150b914b3f/docker-bake.hcl#L82-L91 - docker-engine = ["linux/amd64", "linux/arm/v6", "linux/arm/v7", "linux/arm64", "linux/ppc64le", "linux/s390x", "windows/amd64", "windows/arm64"] - # https://github.com/docker/model-runner/blob/039f7a31c0365f9161c9b9b6bb3888161d16e388/cmd/cli/Makefile#L39-L43 - model = ["darwin/amd64", "darwin/arm64", "linux/amd64", "linux/arm64", "linux/arm/v7", "windows/amd64", "windows/arm64"] - # https://github.com/docker/docker-agent/blob/5b9feaabe743a5ad577f2247ed55d5dcb2678e8b/Taskfile.yml#L79 - agent = ["darwin/amd64", "darwin/arm64", "linux/amd64", "linux/arm64", "windows/amd64", "windows/arm64"] - }, pkg, []) + result = lookup(merge(PKG_PLATFORMS_BASE, PKG_PLATFORMS_EXTRA), pkg, []) +} + +function "pkgContext" { + params = [pkg] + result = lookup(PKG_CONTEXTS_EXTRA, pkg, "./pkg/${pkg}") } # @@ -652,7 +682,7 @@ target "pkg" { pkg = PKGS distro = DISTROS } - context = "./pkg/${pkg}" + context = pkgContext(pkg) contexts = { scripts = "./hack/scripts" } @@ -675,7 +705,7 @@ target "verify" { pkg = PKGS distro = DISTROS } - context = "./pkg/${pkg}" + context = pkgContext(pkg) dockerfile = "verify.Dockerfile" contexts = { scripts = "./hack/scripts" @@ -708,7 +738,7 @@ target "metadata" { matrix = { pkg = PKGS } - context = "./pkg/${pkg}" + context = pkgContext(pkg) contexts = { scripts = "./hack/scripts" }