Skip to content

Commit

Permalink
Merge pull request #151 from shanejbrown/get_source_image_update
Browse files Browse the repository at this point in the history
Add buildx builder support to get_source_image method
  • Loading branch information
shanejbrown authored Aug 13, 2024
2 parents bf6390d + f87c136 commit bd5c10e
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 102 deletions.
39 changes: 28 additions & 11 deletions buildrunner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def __init__(
self._source_image = None
self._source_archive = None
self._log = None
self._step_runner = None

try:
vcs = detect_vcs(self.build_dir)
Expand Down Expand Up @@ -276,15 +277,31 @@ def get_source_image(self):
SOURCE_DOCKERFILE: "Dockerfile",
}
buildrunner_config = BuildRunnerConfig.get_instance()
image = legacy_builder.build_image(
temp_dir=buildrunner_config.global_config.temp_dir,
inject=inject,
timeout=self.docker_timeout,
docker_registry=buildrunner_config.global_config.docker_registry,
nocache=True,
pull=False,
)
self._source_image = image
if buildrunner_config.run_config.use_legacy_builder:
image = legacy_builder.build_image(
temp_dir=buildrunner_config.global_config.temp_dir,
inject=inject,
timeout=self.docker_timeout,
docker_registry=buildrunner_config.global_config.docker_registry,
nocache=True,
pull=False,
)
self._source_image = image
else:
# Use buildx builder
native_platform = self._step_runner.multi_platform.get_native_platform()
LOGGER.info(f"Setting platforms to [{native_platform}]")
platforms = [native_platform]
built_images_info = (
self._step_runner.multi_platform.build_multiple_images(
platforms=platforms,
inject=inject,
cache=False,
pull=False,
)
)
assert len(built_images_info.built_images) == 1
self._source_image = built_images_info.built_images[0].trunc_digest

return self._source_image

Expand Down Expand Up @@ -384,10 +401,10 @@ def run(self): # pylint: disable=too-many-statements,too-many-branches,too-many
)
multi_platform.set_cache_to(step_config.build.cache_to)

build_step_runner = BuildStepRunner(
self._step_runner = BuildStepRunner(
self, step_name, step_config, image_config, multi_platform
)
build_step_runner.run()
self._step_runner.run()

self.log.write(
"\nFinalizing build\n________________________________________\n"
Expand Down
8 changes: 7 additions & 1 deletion buildrunner/docker/multiplatform_image_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,13 @@ def _build_with_inject(
else:
shutil.copy(src_path, dest_path)

# Dockerfile listed in inject will overwrite the dockerfile passed in
if dockerfile and "Dockerfile" not in inject.values():
LOGGER.info(
f"Injecting Dockerfile {dockerfile} to context directory '{context_dir}'"
)
shutil.copy(dockerfile, f"{context_dir}/Dockerfile")

assert os.path.isdir(
context_dir
), f"Failed to create context dir {context_dir}"
Expand All @@ -252,7 +259,6 @@ def _build_with_inject(
tags=[image_ref],
platforms=[platform],
load=True,
file=dockerfile,
target=target,
builder=builder,
build_args=build_args,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from setuptools import setup, find_packages


BASE_VERSION = "3.11"
BASE_VERSION = "3.12"

SOURCE_DIR = os.path.dirname(os.path.abspath(__file__))
BUILDRUNNER_DIR = os.path.join(SOURCE_DIR, "buildrunner")
Expand Down
100 changes: 100 additions & 0 deletions tests/test-files/test-push-artifact-buildx.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
use-legacy-builder: false
steps:

test-no-artifacts:
run:
image: {{ DOCKER_REGISTRY }}/rockylinux:8.5
cmds: [
'echo "hello"'
]

test-no-artifact-properties:
run:
image: {{ DOCKER_REGISTRY }}/rockylinux:8.5
cmds: [
'mkdir test-no-artifact-properties-dir',
'echo "hello" > test-no-artifact-properties-dir/test1.txt',
'echo "hello" > test-no-artifact-properties-dir/test2.txt',
'echo "hello" > test-no-artifact-properties.txt',
]

test-no-push-properties:
run:
image: {{ DOCKER_REGISTRY }}/rockylinux:8.5
cmds: [
'mkdir test-no-push-properties-dir',
'echo "hello" > test-no-push-properties-dir/test1.txt',
'echo "hello" > test-no-push-properties-dir/test2.txt',
'echo "hello" > test-no-push-properties.txt',
]
artifacts:
'test-no-push-properties-dir': {format: 'uncompressed', prop1: 'hello'}
'test-no-push-properties.txt': {format: 'file'}

test-push-true:
run:
image: {{ DOCKER_REGISTRY }}/rockylinux:8.5
cmds: [
'mkdir test-push-true-dir',
'echo "hello" > test-push-true-dir/test1.txt',
'echo "hello" > test-push-true-dir/test2.txt',
'echo "hello" > test-push-true.txt',
]
artifacts:
'test-push-true-dir': {format: 'uncompressed', prop1: 'hello', push: True}
'test-push-true.txt': {format: 'file', push: True}

test-push-false:
run:
image: {{ DOCKER_REGISTRY }}/rockylinux:8.5
cmds: [
'mkdir test-push-false-dir',
'echo "hello" > test-push-false-dir/test1.txt',
'echo "hello" > test-push-false-dir/test2.txt',
'echo "hello" > test-push-false.txt',
]
artifacts:
'test-push-false-dir': {format: 'uncompressed', prop1: 'hello', push: False}
'test-push-false.txt': {format: 'file', push: False}

single-file-rename:
run:
image: ubuntu:20.10
cmds:
- mkdir -p dir1/dir2
- echo "single-file-artifacts - Hello, world!!" > hello.txt
- echo "single-file-artifacts - Hello, world! 1/" > dir1/hello.txt
- echo "single-file-artifacts - Hello, world!! 1/2/" > dir1/dir2/hello.txt
artifacts:
hello.txt:
rename: hello-world.txt
dir1/hello.txt:
format: uncompressed
rename: hello-world1.txt
dir1/dir2/hello.txt:
format: uncompressed
rename: hello-world2.txt

archive-file-rename:
run:
image: ubuntu:latest
cmds:
- mkdir -p dir1/dir2
- mkdir -p dir3/dir2
- mkdir -p uncompressed-dir
- echo "directory-archiver - Hello, world!!" > hello.txt
- echo "directory-archiver - Hello, world! 1" > dir1/hello.txt
- echo "directory-archiver - Hello, world! 2" > dir1/hello1.txt
- echo "directory-archiver - Hello, world! 3" > dir1/hello2.txt
- echo "directory-archiver - Hello, world!! 1/2/" > dir1/dir2/hello.txt
- echo "directory-archiver - Hello, world! 3-2" > dir3/dir2/hello1.txt
- echo "directory-archiver - Hello, world! 3-2" > dir3/dir2/hello2.txt
artifacts:
dir1:
compression: tar
dir1/dir2:
compression: tar
rename: dir1-dir2
dir3/dir2:
compression: tar
rename: dir3-dir2
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use-legacy-builder: true
steps:

test-no-artifacts:
Expand Down
Loading

0 comments on commit bd5c10e

Please sign in to comment.