blob: 52259ef989a097bc79c28ef9945e1eb68bfb39f6 [file] [log] [blame] [edit]
import os
import pathlib
from slave import base_recipe
from slave.step import shell_step
def GetValidBuildNames():
return [
'abbey_container',
'container_building_container',
'ghp_partner_container',
'gotham_container',
'monotainer',
'monotainer_debug',
'recipe_bookworm',
'recipe_bullseye',
]
def CreateRecipe(build_name: str, **kwargs):
del build_name
return ContainerBuilderRecipe(**kwargs)
class ContainerBuilderRecipe(base_recipe.BaseRecipe):
"""Recipe to build and optionally push a container image."""
def get_steps(self):
steps = [ContainerBuildStep(
directory=str(self._eureka_root),
halt_on_failure=True,
**self._step_kwargs)]
if 'container_push_url' in self._properties:
steps += [ContainerPushStep(
directory=str(self._eureka_root),
halt_on_failure=True, **self._step_kwargs)]
return steps
class ContainerBuildStep(shell_step.ShellStep):
def __init__(self, **kwargs):
super().__init__(name='container build', **kwargs)
self._git_config_path = pathlib.Path.home() / 'containers' / 'gitconfig'
self._pip_config_path = pathlib.Path.home() / 'containers' / 'pip.conf'
self._gcs_mirror_path = pathlib.Path.home() / 'gcs-mirror'
self._git_creds_path = pathlib.Path.home() / '.git-credential-cache'
def get_commands(self):
build_id = self.get_property('basil_build_id')
creds_path = self._git_creds_path
return [
self.ShellCommand([
'podman',
'build',
'--format=oci',
'--arch=amd64',
f'--tag={build_id}',
'--no-cache',
'--isolation=chroot',
f'--label=build_id={build_id}',
'--volume=/etc/gitconfig:/etc/gitconfig:ro',
'--volume=/etc/pip.conf:/etc/pip.conf:ro',
'--volume=/workspace/gcs-mirror:/workspace/gcs-mirror:ro',
('--volume=/workspace/git-credential-cache:'
'/workspace/git-credential-cache:ro'),
self.get_property('container_context_path'),
]),
]
class ContainerPushStep(shell_step.ShellStep):
def __init__(self, **kwargs):
super().__init__(name='container push', **kwargs)
def get_commands(self):
build_id = self.get_property('basil_build_id')
push_url = self.get_property('container_push_url')
image_tag = self.get_property('buildset')
builder_name = self.get_property('buildername')
return [
self.ShellCommand([
'podman',
'push',
f'localhost/{build_id}:latest',
f'{push_url}/{builder_name}:{image_tag}',
], num_retries=3),
]