Skip to content

Commit 0810170

Browse files
committed
Use a separate push-cache flag for bakery build
1 parent c81d0f5 commit 0810170

File tree

6 files changed

+27
-62
lines changed

6 files changed

+27
-62
lines changed

posit-bakery/posit_bakery/cli/build.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ def build(
8080
rich_help_panel=RichHelpPanelEnum.BUILD_CONFIGURATION_AND_OUTPUTS,
8181
),
8282
] = False,
83+
push_cache: Annotated[
84+
Optional[bool],
85+
typer.Option(
86+
help="Push the build cache to the cache registry without pushing the image.",
87+
rich_help_panel=RichHelpPanelEnum.BUILD_CONFIGURATION_AND_OUTPUTS,
88+
),
89+
] = False,
8390
clean: Annotated[
8491
Optional[bool],
8592
typer.Option(
@@ -205,6 +212,7 @@ def build(
205212
config.build_targets(
206213
load=load,
207214
push=push,
215+
push_cache=push_cache,
208216
cache=cache,
209217
platforms=image_platform,
210218
strategy=strategy,

posit-bakery/posit_bakery/config/config.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,7 @@ def build_targets(
734734
self,
735735
load: bool = True,
736736
push: bool = False,
737+
push_cache: bool = False,
737738
cache: bool = True,
738739
platforms: list[str] | None = None,
739740
strategy: ImageBuildStrategy = ImageBuildStrategy.BAKE,
@@ -744,6 +745,7 @@ def build_targets(
744745
745746
:param load: If True, load the built images into the local Docker daemon.
746747
:param push: If True, push the built images to the configured registries.
748+
:param push_cache: If True, push the build cache to the cache registry without pushing images.
747749
:param cache: If True, use the build cache when building images.
748750
:param platforms: Optional list of platforms to build for. If None, builds for the configuration specified
749751
platform.
@@ -752,7 +754,9 @@ def build_targets(
752754
:param fail_fast: If True, stop building targets on the first failure.
753755
"""
754756
if strategy == ImageBuildStrategy.BAKE:
755-
bake_plan = BakePlan.from_image_targets(context=self.base_path, image_targets=self.targets, push=push)
757+
bake_plan = BakePlan.from_image_targets(
758+
context=self.base_path, image_targets=self.targets, push_cache=push_cache
759+
)
756760
set_opts = None
757761
if self.settings.temp_registry is not None and push:
758762
set_opts = {

posit-bakery/posit_bakery/image/bake/bake.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def serialize_path(value: Path | str) -> str:
9090
return str(value)
9191

9292
@classmethod
93-
def from_image_target(cls, image_target: ImageTarget, push: bool = False) -> "BakeTarget":
93+
def from_image_target(cls, image_target: ImageTarget, push_cache: bool = False) -> "BakeTarget":
9494
"""Create a BakeTarget from an ImageTarget."""
9595
kwargs = {"tags": image_target.tags}
9696
platforms = image_target.image_os.platforms if image_target.image_os is not None else DEFAULT_PLATFORMS
@@ -101,7 +101,7 @@ def from_image_target(cls, image_target: ImageTarget, push: bool = False) -> "Ba
101101
platform_suffix = "-".join(p.removeprefix("linux/").replace("/", "-") for p in platforms)
102102
cache_name = f"{cache_name}-{platform_suffix}"
103103
kwargs["cache_from"] = [{"type": "registry", "ref": cache_name}]
104-
if push:
104+
if push_cache:
105105
kwargs["cache_to"] = [{"type": "registry", "ref": cache_name, "mode": "max"}]
106106

107107
if image_target.temp_name is not None:
@@ -158,12 +158,14 @@ def update_groups(
158158
return groups
159159

160160
@classmethod
161-
def from_image_targets(cls, context: Path, image_targets: list[ImageTarget], push: bool = False) -> "BakePlan":
161+
def from_image_targets(
162+
cls, context: Path, image_targets: list[ImageTarget], push_cache: bool = False
163+
) -> "BakePlan":
162164
"""Create a BakePlan from a list of ImageTarget objects.
163165
164166
:param context: The absolute path to the build context directory.
165167
:param image_targets: A list of ImageTarget objects to include in the bake plan.
166-
:param push: Whether images will be pushed. Controls cache_to behavior.
168+
:param push_cache: Whether to push build cache to the cache registry.
167169
168170
:return: A BakePlan object containing the context, groups, and targets.
169171
"""
@@ -173,7 +175,7 @@ def from_image_targets(cls, context: Path, image_targets: list[ImageTarget], pus
173175
targets: dict[str, BakeTarget] = {}
174176

175177
for image_target in image_targets:
176-
bake_target = BakeTarget.from_image_target(image_target=image_target, push=push)
178+
bake_target = BakeTarget.from_image_target(image_target=image_target, push_cache=push_cache)
177179
groups = cls.update_groups(
178180
groups=groups,
179181
uid=image_target.uid,

posit-bakery/test/image/bake/testdata/cache_registry/barebones_plan.json

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,7 @@
4040
"cache_from": [
4141
{
4242
"type": "registry",
43-
"ref": "ghcr.io/posit-dev/scratch/cache:1.0.0-scratch"
44-
}
45-
],
46-
"cache_to": [
47-
{
48-
"type": "registry",
49-
"ref": "ghcr.io/posit-dev/scratch/cache:1.0.0-scratch",
50-
"mode": "max"
43+
"ref": "ghcr.io/posit-dev/scratch/cache:1.0.0-scratch-amd64"
5144
}
5245
]
5346
}

posit-bakery/test/image/bake/testdata/cache_registry/basic_plan.json

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,7 @@
5858
"cache_from": [
5959
{
6060
"type": "registry",
61-
"ref": "ghcr.io/posit-dev/test-image/cache:1.0.0-ubuntu-22.04-min"
62-
}
63-
],
64-
"cache_to": [
65-
{
66-
"type": "registry",
67-
"ref": "ghcr.io/posit-dev/test-image/cache:1.0.0-ubuntu-22.04-min",
68-
"mode": "max"
61+
"ref": "ghcr.io/posit-dev/test-image/cache:1.0.0-ubuntu-22.04-min-amd64"
6962
}
7063
]
7164
},
@@ -111,14 +104,7 @@
111104
"cache_from": [
112105
{
113106
"type": "registry",
114-
"ref": "ghcr.io/posit-dev/test-image/cache:1.0.0-ubuntu-22.04-std"
115-
}
116-
],
117-
"cache_to": [
118-
{
119-
"type": "registry",
120-
"ref": "ghcr.io/posit-dev/test-image/cache:1.0.0-ubuntu-22.04-std",
121-
"mode": "max"
107+
"ref": "ghcr.io/posit-dev/test-image/cache:1.0.0-ubuntu-22.04-std-amd64"
122108
}
123109
]
124110
}

posit-bakery/test/image/bake/testdata/cache_registry/multiplatform_plan.json

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,7 @@
5858
"cache_from": [
5959
{
6060
"type": "registry",
61-
"ref": "ghcr.io/posit-dev/test-multi/cache:1.0.0-ubuntu-22.04-min"
62-
}
63-
],
64-
"cache_to": [
65-
{
66-
"type": "registry",
67-
"ref": "ghcr.io/posit-dev/test-multi/cache:1.0.0-ubuntu-22.04-min",
68-
"mode": "max"
61+
"ref": "ghcr.io/posit-dev/test-multi/cache:1.0.0-ubuntu-22.04-min-amd64"
6962
}
7063
]
7164
},
@@ -100,14 +93,7 @@
10093
"cache_from": [
10194
{
10295
"type": "registry",
103-
"ref": "ghcr.io/posit-dev/test-multi/cache:1.0.0-ubuntu-24.04-min"
104-
}
105-
],
106-
"cache_to": [
107-
{
108-
"type": "registry",
109-
"ref": "ghcr.io/posit-dev/test-multi/cache:1.0.0-ubuntu-24.04-min",
110-
"mode": "max"
96+
"ref": "ghcr.io/posit-dev/test-multi/cache:1.0.0-ubuntu-24.04-min-amd64-arm64"
11197
}
11298
]
11399
},
@@ -141,14 +127,7 @@
141127
"cache_from": [
142128
{
143129
"type": "registry",
144-
"ref": "ghcr.io/posit-dev/test-multi/cache:1.0.0-ubuntu-22.04-std"
145-
}
146-
],
147-
"cache_to": [
148-
{
149-
"type": "registry",
150-
"ref": "ghcr.io/posit-dev/test-multi/cache:1.0.0-ubuntu-22.04-std",
151-
"mode": "max"
130+
"ref": "ghcr.io/posit-dev/test-multi/cache:1.0.0-ubuntu-22.04-std-amd64"
152131
}
153132
]
154133
},
@@ -187,14 +166,7 @@
187166
"cache_from": [
188167
{
189168
"type": "registry",
190-
"ref": "ghcr.io/posit-dev/test-multi/cache:1.0.0-ubuntu-24.04-std"
191-
}
192-
],
193-
"cache_to": [
194-
{
195-
"type": "registry",
196-
"ref": "ghcr.io/posit-dev/test-multi/cache:1.0.0-ubuntu-24.04-std",
197-
"mode": "max"
169+
"ref": "ghcr.io/posit-dev/test-multi/cache:1.0.0-ubuntu-24.04-std-amd64-arm64"
198170
}
199171
]
200172
}

0 commit comments

Comments
 (0)