1515from lib .sonar .sonar import create_ecr_repository
1616from scripts .evergreen .release .images_signing import sign_image , verify_signature
1717
18+
1819# TODO: self review the PR
1920def ecr_login_boto3 (region : str , account_id : str ):
2021 """
@@ -45,36 +46,38 @@ def ecr_login_boto3(region: str, account_id: str):
4546
4647# TODO: don't do it every time ? Check for existence without relying on Exception
4748def ensure_buildx_builder (builder_name : str = "multiarch" ) -> str :
48- """
49- Ensures a Docker Buildx builder exists for multi-platform builds.
50-
51- :param builder_name: Name for the buildx builder
52- :return: The builder name that was created or reused
53- """
54- docker = python_on_whales .docker
55-
56- try :
57- docker .buildx .create (
58- name = builder_name ,
59- driver = "docker-container" ,
60- use = True ,
61- bootstrap = True ,
62- )
63- logger .info (f"Created new buildx builder: { builder_name } " )
64- except DockerException as e :
65- if f'existing instance for "{ builder_name } "' in str (e ):
66- logger .info (f"Builder '{ builder_name } ' already exists – reusing it." )
67- # Make sure it's the current one:
68- docker .buildx .use (builder_name )
69- else :
70- # Some other failure happened
71- logger .error (f"Failed to create buildx builder: { e } " )
72- raise
73-
74- return builder_name
75-
76-
77- def build_image (tag : str , dockerfile : str , path : str , args : Dict [str , str ] = {}, push : bool = True , platforms : list [str ] = None ):
49+ """
50+ Ensures a Docker Buildx builder exists for multi-platform builds.
51+
52+ :param builder_name: Name for the buildx builder
53+ :return: The builder name that was created or reused
54+ """
55+ docker = python_on_whales .docker
56+
57+ try :
58+ docker .buildx .create (
59+ name = builder_name ,
60+ driver = "docker-container" ,
61+ use = True ,
62+ bootstrap = True ,
63+ )
64+ logger .info (f"Created new buildx builder: { builder_name } " )
65+ except DockerException as e :
66+ if f'existing instance for "{ builder_name } "' in str (e ):
67+ logger .info (f"Builder '{ builder_name } ' already exists – reusing it." )
68+ # Make sure it's the current one:
69+ docker .buildx .use (builder_name )
70+ else :
71+ # Some other failure happened
72+ logger .error (f"Failed to create buildx builder: { e } " )
73+ raise
74+
75+ return builder_name
76+
77+
78+ def build_image (
79+ tag : str , dockerfile : str , path : str , args : Dict [str , str ] = {}, push : bool = True , platforms : list [str ] = None
80+ ):
7881 """
7982 Build a Docker image using python_on_whales and Docker Buildx for multi-architecture support.
8083
@@ -86,25 +89,25 @@ def build_image(tag: str, dockerfile: str, path: str, args: Dict[str, str] = {},
8689 :param platforms: List of target platforms (e.g., ["linux/amd64", "linux/arm64"])
8790 """
8891 docker = python_on_whales .docker
89-
92+
9093 try :
9194 # Convert build args to the format expected by python_on_whales
9295 build_args = {k : str (v ) for k , v in args .items ()} if args else {}
93-
96+
9497 # Set default platforms if not specified
9598 if platforms is None :
9699 platforms = ["linux/amd64" ]
97-
100+
98101 logger .info (f"Building image: { tag } " )
99102 logger .info (f"Platforms: { platforms } " )
100103 logger .info (f"Dockerfile: { dockerfile } " )
101104 logger .info (f"Build context: { path } " )
102105 logger .debug (f"Build args: { build_args } " )
103-
106+
104107 # Use buildx for multi-platform builds
105108 if len (platforms ) > 1 :
106109 logger .info (f"Multi-platform build for { len (platforms )} architectures" )
107-
110+
108111 # We need a special driver to handle multi platform builds
109112 builder_name = ensure_buildx_builder ("multiarch" )
110113
@@ -117,18 +120,17 @@ def build_image(tag: str, dockerfile: str, path: str, args: Dict[str, str] = {},
117120 builder = builder_name ,
118121 build_args = build_args ,
119122 push = push ,
120- provenance = False , # To not get an untagged image for single platform builds
123+ provenance = False , # To not get an untagged image for single platform builds
121124 pull = False , # Don't always pull base images
122125 )
123-
126+
124127 logger .info (f"Successfully built { 'and pushed' if push else '' } { tag } " )
125-
128+
126129 except Exception as e :
127130 logger .error (f"Failed to build image { tag } : { e } " )
128131 raise RuntimeError (f"Failed to build image { tag } : { str (e )} " )
129132
130133
131-
132134def process_image (
133135 image_name : str ,
134136 image_tag : str ,
@@ -141,7 +143,7 @@ def process_image(
141143 push : bool = True ,
142144):
143145 # Login to ECR using boto3
144- ecr_login_boto3 (region = "us-east-1" , account_id = "268558157000" ) # TODO: use environment variables
146+ ecr_login_boto3 (region = "us-east-1" , account_id = "268558157000" ) # TODO: use environment variables
145147
146148 # Helper to automatically create registry with correct name
147149 should_create_repo = False
@@ -157,15 +159,15 @@ def process_image(
157159
158160 docker_registry = f"{ base_registry } /{ image_name } "
159161 image_full_uri = f"{ docker_registry } :{ image_tag } "
160-
162+
161163 # Build image with docker buildx
162164 build_image (
163165 tag = image_full_uri ,
164166 dockerfile = dockerfile_path ,
165167 path = build_path ,
166168 args = dockerfile_args ,
167169 push = push ,
168- platforms = platforms
170+ platforms = platforms ,
169171 )
170172
171173 if sign :
0 commit comments