Skip to content

feat: use docker build cache #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

### Added

- New command line arg to `bake`: `--cache`. Requires cache backend configuration in conf.py ([#29])

[#29]: https://github.com/stackabletech/image-tools/pull/29

## [0.0.8] - 2024-07-02

### Added
Expand Down
5 changes: 5 additions & 0 deletions src/image_tools/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ def bake_args() -> Namespace:
help="Write target image tags to a text file. Useful for signing or other follow-up CI steps.",
),
)
parser.add_argument(
"--cache",
help="Enable distributed build cache",
action="store_true"
),

result = parser.parse_args()

Expand Down
25 changes: 21 additions & 4 deletions src/image_tools/bake.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"""

import sys
import copy
from typing import List, Dict, Any
from argparse import Namespace
from subprocess import run
Expand Down Expand Up @@ -59,7 +60,7 @@ def generate_bakefile(args: Namespace, conf) -> Dict[str, Any]:
product_name: str = product["name"]
product_targets = {}
for version_dict in product.get("versions", []):
product_targets.update(bakefile_product_version_targets(args, product_name, version_dict, product_names))
product_targets.update(bakefile_product_version_targets(args, product_name, version_dict, product_names, conf.cache))
groups[product_name] = {
"targets": list(product_targets.keys()),
}
Expand All @@ -85,6 +86,7 @@ def bakefile_product_version_targets(
product_name: str,
versions: Dict[str, str],
product_names: List[str],
cache: List[Dict[str, str]],
):
"""
Creates Bakefile targets defining how to build a given product version.
Expand All @@ -94,9 +96,10 @@ def bakefile_product_version_targets(
image_name = f"{args.registry}/{args.organization}/{product_name}"
tags = build_image_tags(image_name, args.image_version, versions["product"])
build_args = build_image_args(versions, args.image_version)
target_name = bakefile_target_name_for_product_version(product_name, versions["product"])

return {
bakefile_target_name_for_product_version(product_name, versions["product"]): {
result = {
target_name: {
"dockerfile": f"{product_name}/Dockerfile",
"tags": tags,
"args": build_args,
Expand All @@ -106,10 +109,14 @@ def bakefile_product_version_targets(
f"stackable/image/{name}": f"target:{bakefile_target_name_for_product_version(name, version)}"
for name, version in versions.items()
if name in product_names
},
}
},
}

if args.cache:
result[target_name]["cache-to"] = result[target_name]["cache-from"] = generate_cache_location(cache, target_name)

return result

def targets_for_selector(conf, selected_products: List[str]) -> List[str]:
targets = []
Expand Down Expand Up @@ -156,6 +163,16 @@ def bake_command(args: Namespace, targets: List[str], bakefile) -> Command:
stdin=json.dumps(bakefile),
)

def generate_cache_location(cache: List[Dict[str, str]], target_name: str) -> List[str]:
cache_copy = copy.deepcopy(cache)
result = []

for backend in cache_copy:
backend["ref"] = f"{backend['ref_prefix']}:{target_name}"
del backend["ref_prefix"]
result.append(",".join([f"{k}={v}" for k, v in backend.items()]))

return result

def main() -> int:
"""Generate a Docker bake file from conf.py and build the given args.product images."""
Expand Down
3 changes: 3 additions & 0 deletions src/image_tools/test/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,3 +441,6 @@
"trino": {"id": "62557c4a0030f6483318e203"},
"zookeeper": {"id": "62552b0aadd9d54d56cda11d"},
}

# The type hint is needed for mypy
cache: list[str] = []