Skip to content

Commit 89e26bf

Browse files
committed
make bake definitions extensible for downstream overrides
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
1 parent e3a290f commit 89e26bf

File tree

2 files changed

+103
-24
lines changed

2 files changed

+103
-24
lines changed

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ ___
1717
* [Build](#build)
1818
* [Requirements](#requirements)
1919
* [Usage](#usage)
20+
* [Remote override](#remote-override)
2021
* [Contributing](#contributing)
2122

2223
## Release
@@ -81,6 +82,54 @@ $ PKG_REF=v0.24.0 docker buildx bake pkg-buildx-*
8182
$ docker buildx bake --push --set *.tags=dockereng/packaging:buildx-v0.24.0 release-buildx
8283
```
8384

85+
### Remote override
86+
87+
Downstream repositories can reuse this repository as a remote Bake definition
88+
and extend it with a local `docker-bake.override.hcl` file instead of forking
89+
the full `docker-bake.hcl`.
90+
91+
Example local override:
92+
93+
```hcl
94+
variable "PKGS_EXTRA" {
95+
default = ["my-package"]
96+
}
97+
98+
variable "PKG_PLATFORMS_EXTRA" {
99+
default = {
100+
my-package = ["linux/amd64"]
101+
}
102+
}
103+
104+
variable "PKG_CONTEXTS_EXTRA" {
105+
default = {
106+
my-package = "cwd://pkg/my-package"
107+
}
108+
}
109+
110+
target "_pkg-my-package" {
111+
args = {
112+
PKG_NAME = "my-package"
113+
PKG_REPO = "https://github.com/example/my-package.git"
114+
PKG_REF = "main"
115+
}
116+
}
117+
```
118+
119+
Example invocation from the downstream repository:
120+
121+
```shell
122+
$ docker buildx bake \
123+
-f docker-bake.hcl \
124+
-f cwd://docker-bake.override.hcl \
125+
"https://github.com/docker/packaging.git#main" \
126+
pkg-my-package-debian12
127+
```
128+
129+
Use the `cwd://` prefix for local files and directories when combining a local
130+
override with a remote Bake definition. Without it, Bake resolves paths relative
131+
to the remote context instead of the current working directory.
132+
84133
## Contributing
85134

86135
Want to contribute? Awesome! You can find information about contributing to

docker-bake.hcl

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ variable "DISTROS" {
4242
]
4343
}
4444

45-
variable "PKGS" {
46-
description = "List of packages to build from ./pkg directory. Don't forget to update _pkg-* target if you add/remove a package."
45+
variable "PKGS_BASE" {
46+
description = "Base list of packages built from this repo. Use PKGS_EXTRA in an override file to add downstream packages."
4747
default = [
4848
"buildx",
4949
"compose",
@@ -56,6 +56,49 @@ variable "PKGS" {
5656
]
5757
}
5858

59+
variable "PKGS_EXTRA" {
60+
description = "Additional packages to append from an override bake definition."
61+
default = []
62+
}
63+
64+
variable "PKGS" {
65+
description = "Full list of packages to build from ./pkg directory or an override-provided package context."
66+
default = concat(PKGS_BASE, PKGS_EXTRA)
67+
}
68+
69+
variable "PKG_PLATFORMS_BASE" {
70+
description = "Base package platform mapping. Use PKG_PLATFORMS_EXTRA in an override bake definition to add downstream packages."
71+
default = {
72+
# https://github.com/docker/buildx/blob/0c747263ef1426f5fa217fcdb616eddf33da6c2d/docker-bake.hcl#L156-L174
73+
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"]
74+
# https://github.com/docker/compose/blob/c626befee1596abcc74578cb10dd96ae1667f76f/docker-bake.hcl#L112-L124
75+
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"]
76+
# https://github.com/containerd/containerd/blob/e373060a953aeaa35554e2f667043fed73ff6248/.github/workflows/ci.yml#L142-L162
77+
# https://github.com/containerd/containerd/blob/e373060a953aeaa35554e2f667043fed73ff6248/.github/workflows/ci.yml#L133-L134
78+
containerd = ["linux/amd64", "linux/arm/v6", "linux/arm/v7", "linux/arm64", "linux/ppc64le", "linux/s390x", "windows/amd64", "windows/arm64"]
79+
# https://github.com/docker/docker-credential-helpers/blob/f9d3010165b642df37215b1be945552f2c6f0e3b/docker-bake.hcl#L56-L66
80+
credential-helpers = ["darwin/amd64", "darwin/arm64", "linux/amd64", "linux/arm/v6", "linux/arm/v7", "linux/arm64", "linux/ppc64le", "linux/s390x", "windows/amd64"]
81+
# https://github.com/docker/cli/blob/84038691220e7ba3329a177e4e3357b4ee0e3a52/docker-bake.hcl#L30-L42
82+
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"]
83+
# https://github.com/moby/moby/blob/83264918d3e1c61341511e360a7277150b914b3f/docker-bake.hcl#L82-L91
84+
docker-engine = ["linux/amd64", "linux/arm/v6", "linux/arm/v7", "linux/arm64", "linux/ppc64le", "linux/s390x", "windows/amd64", "windows/arm64"]
85+
# https://github.com/docker/model-runner/blob/039f7a31c0365f9161c9b9b6bb3888161d16e388/cmd/cli/Makefile#L39-L43
86+
model = ["darwin/amd64", "darwin/arm64", "linux/amd64", "linux/arm64", "linux/arm/v7", "windows/amd64", "windows/arm64"]
87+
# https://github.com/docker/docker-agent/blob/5b9feaabe743a5ad577f2247ed55d5dcb2678e8b/Taskfile.yml#L79
88+
agent = ["darwin/amd64", "darwin/arm64", "linux/amd64", "linux/arm64", "windows/amd64", "windows/arm64"]
89+
}
90+
}
91+
92+
variable "PKG_PLATFORMS_EXTRA" {
93+
description = "Additional package platform mapping to merge from an override bake definition."
94+
default = {}
95+
}
96+
97+
variable "PKG_CONTEXTS_EXTRA" {
98+
description = "Additional package context mapping to merge from an override bake definition."
99+
default = {}
100+
}
101+
59102
variable "DISTRO_NAME" {
60103
description = "Name of the distro."
61104
default = null
@@ -589,25 +632,12 @@ target "_pkg-agent" {
589632
# Returns the list of supported platforms for a given package.
590633
function "pkgPlatforms" {
591634
params = [pkg]
592-
result = lookup({
593-
# https://github.com/docker/buildx/blob/0c747263ef1426f5fa217fcdb616eddf33da6c2d/docker-bake.hcl#L156-L174
594-
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"]
595-
# https://github.com/docker/compose/blob/c626befee1596abcc74578cb10dd96ae1667f76f/docker-bake.hcl#L112-L124
596-
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"]
597-
# https://github.com/containerd/containerd/blob/e373060a953aeaa35554e2f667043fed73ff6248/.github/workflows/ci.yml#L142-L162
598-
# https://github.com/containerd/containerd/blob/e373060a953aeaa35554e2f667043fed73ff6248/.github/workflows/ci.yml#L133-L134
599-
containerd = ["linux/amd64", "linux/arm/v6", "linux/arm/v7", "linux/arm64", "linux/ppc64le", "linux/s390x", "windows/amd64", "windows/arm64"]
600-
# https://github.com/docker/docker-credential-helpers/blob/f9d3010165b642df37215b1be945552f2c6f0e3b/docker-bake.hcl#L56-L66
601-
credential-helpers = ["darwin/amd64", "darwin/arm64", "linux/amd64", "linux/arm/v6", "linux/arm/v7", "linux/arm64", "linux/ppc64le", "linux/s390x", "windows/amd64"]
602-
# https://github.com/docker/cli/blob/84038691220e7ba3329a177e4e3357b4ee0e3a52/docker-bake.hcl#L30-L42
603-
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"]
604-
# https://github.com/moby/moby/blob/83264918d3e1c61341511e360a7277150b914b3f/docker-bake.hcl#L82-L91
605-
docker-engine = ["linux/amd64", "linux/arm/v6", "linux/arm/v7", "linux/arm64", "linux/ppc64le", "linux/s390x", "windows/amd64", "windows/arm64"]
606-
# https://github.com/docker/model-runner/blob/039f7a31c0365f9161c9b9b6bb3888161d16e388/cmd/cli/Makefile#L39-L43
607-
model = ["darwin/amd64", "darwin/arm64", "linux/amd64", "linux/arm64", "linux/arm/v7", "windows/amd64", "windows/arm64"]
608-
# https://github.com/docker/docker-agent/blob/5b9feaabe743a5ad577f2247ed55d5dcb2678e8b/Taskfile.yml#L79
609-
agent = ["darwin/amd64", "darwin/arm64", "linux/amd64", "linux/arm64", "windows/amd64", "windows/arm64"]
610-
}, pkg, [])
635+
result = lookup(merge(PKG_PLATFORMS_BASE, PKG_PLATFORMS_EXTRA), pkg, [])
636+
}
637+
638+
function "pkgContext" {
639+
params = [pkg]
640+
result = lookup(PKG_CONTEXTS_EXTRA, pkg, "./pkg/${pkg}")
611641
}
612642

613643
#
@@ -652,7 +682,7 @@ target "pkg" {
652682
pkg = PKGS
653683
distro = DISTROS
654684
}
655-
context = "./pkg/${pkg}"
685+
context = pkgContext(pkg)
656686
contexts = {
657687
scripts = "./hack/scripts"
658688
}
@@ -675,7 +705,7 @@ target "verify" {
675705
pkg = PKGS
676706
distro = DISTROS
677707
}
678-
context = "./pkg/${pkg}"
708+
context = pkgContext(pkg)
679709
dockerfile = "verify.Dockerfile"
680710
contexts = {
681711
scripts = "./hack/scripts"
@@ -708,7 +738,7 @@ target "metadata" {
708738
matrix = {
709739
pkg = PKGS
710740
}
711-
context = "./pkg/${pkg}"
741+
context = pkgContext(pkg)
712742
contexts = {
713743
scripts = "./hack/scripts"
714744
}

0 commit comments

Comments
 (0)