Skip to content

Commit

Permalink
[trial-builds] Use buildkit caching (#7535)
Browse files Browse the repository at this point in the history
Do this to make trial builds more interactive. By using buildkit caching, we won't need to rebuild every single image when a change is made to the PR.
  • Loading branch information
jonathanmetzman committed Apr 8, 2022
1 parent cacd58c commit 2fe1b08
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
19 changes: 12 additions & 7 deletions infra/build/functions/build_and_push_test_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,20 @@ def build_and_push_image(image, test_image_suffix):
main_tag = TAG_PREFIX + image
testing_tag = main_tag + '-' + test_image_suffix
tags = [main_tag, testing_tag]
build_image(image, tags)
build_image(image, tags, testing_tag)
push_image(testing_tag)


def build_image(image, tags):
def build_image(image, tags, cache_from_tag):
"""Builds |image| and tags it with |tags|."""
logging.info('Building: %s', image)
command = ['docker', 'build']
for tag in tags:
command.extend(['--tag', tag])
path = os.path.join(IMAGES_DIR, image)
command.extend([
'--build-arg', 'BUILDKIT_INLINE_CACHE=1', '--cache-from', cache_from_tag
])
command.append(path)
subprocess.run(command, check=True)
logging.info('Built: %s', image)
Expand All @@ -74,7 +77,8 @@ def gcb_build_and_push_images(test_image_suffix):
test_images.append(test_image_name)
directory = os.path.join('infra', 'base-images', base_image)
step = build_lib.get_docker_build_step([image_name, test_image_name],
directory)
directory,
buildkit_cache_image=test_image_name)
steps.append(step)

overrides = {'images': test_images}
Expand Down Expand Up @@ -103,13 +107,14 @@ def build_and_push_images(test_image_suffix):
'base-builder-rust',
],
]
os.environ['DOCKER_BUILDKIT'] = '1'
max_parallelization = max([len(image_list) for image_list in images])
proc_count = min(multiprocessing.cpu_count(), max_parallelization)
logging.info('Using %d parallel processes.', proc_count)
pool = multiprocessing.Pool(proc_count)
for image_list in images:
args_list = [(image, test_image_suffix) for image in image_list]
pool.starmap(build_and_push_image, args_list)
with multiprocessing.Pool(proc_count) as pool:
for image_list in images:
args_list = [(image, test_image_suffix) for image in image_list]
pool.starmap(build_and_push_image, args_list)


def main():
Expand Down
19 changes: 16 additions & 3 deletions infra/build/functions/build_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,20 +319,33 @@ def get_git_clone_step(repo_url='https://github.com/google/oss-fuzz.git',
return clone_step


def get_docker_build_step(image_names, directory):
def get_docker_build_step(image_names, directory, buildkit_cache_image=None):
"""Returns the docker build step."""
assert len(image_names) >= 1
directory = os.path.join('oss-fuzz', directory)
args = ['build']
for image_name in image_names:
args.extend(['--tag', image_name])

args.append('.')
return {
step = {
'name': 'gcr.io/cloud-builders/docker',
'args': args,
'dir': directory,
}
# Note that we mutate "args" after making it a value in step.

if buildkit_cache_image is not None:
env = ['DOCKER_BUILDKIT=1']
step['env'] = env
assert buildkit_cache_image in args
additional_args = [
'--build-arg', 'BUILDKIT_INLINE_CACHE=1', '--cache-from',
buildkit_cache_image
]
args.extend(additional_args)
args.append('.')

return step


def project_image_steps(name,
Expand Down

0 comments on commit 2fe1b08

Please sign in to comment.