7
0
mirror of https://github.com/ChristianLight/tutor.git synced 2024-06-27 09:03:28 +00:00

feat: faster builds with registry cache

Automatically pull Docker build cache from remote registry. This
considerably improves build performances, as discovered here:
https://github.com/overhangio/test-docker-build/
This commit is contained in:
Régis Behmo 2023-04-27 16:55:42 +02:00
parent 5c426257fb
commit 253b69ffe3
2 changed files with 33 additions and 7 deletions

View File

@ -0,0 +1 @@
- [Improvement] Automatically pull Docker image cache from the remote registry. Again, this will considerably improve image build-time, particularly in "cold-start" scenarios, where the images need to be built from scratch. The registry cache can be disabled with the `tutor images build --no-registry-cache` option. (by @regisb)

View File

@ -6,7 +6,7 @@ import click
from tutor import config as tutor_config from tutor import config as tutor_config
from tutor import env as tutor_env from tutor import env as tutor_env
from tutor import exceptions, hooks, images from tutor import exceptions, hooks, images, utils
from tutor.commands.context import Context from tutor.commands.context import Context
from tutor.core.hooks import Filter from tutor.core.hooks import Filter
from tutor.types import Config from tutor.types import Config
@ -68,14 +68,21 @@ def images_command() -> None:
pass pass
@click.command( @click.command()
short_help="Build docker images",
help="Build the docker images necessary for an Open edX platform.",
)
@click.argument("image_names", metavar="image", nargs=-1) @click.argument("image_names", metavar="image", nargs=-1)
@click.option( @click.option(
"--no-cache", is_flag=True, help="Do not use cache when building the image" "--no-cache", is_flag=True, help="Do not use cache when building the image"
) )
@click.option(
"--no-registry-cache",
is_flag=True,
help="Do not use registry cache when building the image",
)
@click.option(
"--cache-to-registry",
is_flag=True,
help="Push the build cache to the remote registry. You should only enable this option if you have push rights to the remote registry.",
)
@click.option( @click.option(
"-a", "-a",
"--build-arg", "--build-arg",
@ -105,11 +112,19 @@ def build(
context: Context, context: Context,
image_names: list[str], image_names: list[str],
no_cache: bool, no_cache: bool,
no_registry_cache: bool,
cache_to_registry: bool,
build_args: list[str], build_args: list[str],
add_hosts: list[str], add_hosts: list[str],
target: str, target: str,
docker_args: list[str], docker_args: list[str],
) -> None: ) -> None:
"""
Build docker images
Build the docker images necessary for an Open edX platform. By default, the remote
registry cache will be used for better performance.
"""
config = tutor_config.load(context.root) config = tutor_config.load(context.root)
command_args = [] command_args = []
if no_cache: if no_cache:
@ -120,15 +135,25 @@ def build(
command_args += ["--add-host", add_host] command_args += ["--add-host", add_host]
if target: if target:
command_args += ["--target", target] command_args += ["--target", target]
if utils.is_buildkit_enabled():
# Export image to docker.
command_args.append("--output=type=image")
if docker_args: if docker_args:
command_args += docker_args command_args += docker_args
for image in image_names: for image in image_names:
for _name, path, tag, custom_args in find_images_to_build(config, image): for _name, path, tag, custom_args in find_images_to_build(config, image):
image_build_args = [*command_args, *custom_args]
if not no_registry_cache:
# Use registry cache
image_build_args.append(f"--cache-from=type=registry,ref={tag}-cache")
if cache_to_registry:
image_build_args.append(
f"--cache-to=type=registry,mode=max,ref={tag}-cache"
)
images.build( images.build(
tutor_env.pathjoin(context.root, *path), tutor_env.pathjoin(context.root, *path),
tag, tag,
*command_args, *image_build_args,
*custom_args,
) )