Adds functionality to build Docker images using gcloud build. This do…#83
Adds functionality to build Docker images using gcloud build. This do…#83brp-hiverge wants to merge 1 commit into
Conversation
…esn't use caching.
There was a problem hiding this comment.
Pull request overview
This PR adds functionality to build Docker images using Google Cloud Build instead of local Docker builds. The implementation creates a temporary cloudbuild.yaml configuration file and submits it to GCP for building and pushing images to Container Registry.
Key Changes
- Introduces a new
build_image_in_cloud()function that leverages gcloud CLI for cloud-based Docker image builds - Adds a
BUILD_TEMPLATEconstant containing the Cloud Build YAML configuration - Uses subprocess to execute gcloud commands since the Python SDK doesn't support building from local directories
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| build_yaml_path = f"{context}/cloudbuild.yaml" | ||
| with open(build_yaml_path, "w", encoding="utf-8") as f: | ||
| f.write(build_yaml) |
There was a problem hiding this comment.
The cloudbuild.yaml file is created but never cleaned up after the build completes. Consider using a try-finally block to ensure the temporary file is removed, or use tempfile.NamedTemporaryFile for automatic cleanup.
| with open(build_yaml_path, "w", encoding="utf-8") as f: | ||
| f.write(build_yaml) | ||
|
|
||
| cmd = ["gcloud", "builds", "submit", "--config", build_yaml_path, "."] |
There was a problem hiding this comment.
The command hardcodes '.' as the source directory, but should use the context parameter to maintain consistency with the function's design. Change '.' to context.
| cmd = ["gcloud", "builds", "submit", "--config", build_yaml_path, "."] | |
| cmd = ["gcloud", "builds", "submit", "--config", build_yaml_path, context] |
| subprocess.run( | ||
| cmd, | ||
| check=True, | ||
| capture_output=True, |
There was a problem hiding this comment.
Using capture_output=True suppresses real-time build output, making it difficult to monitor long-running cloud builds. Consider using capture_output=False or streaming output to provide better user feedback during the build process.
| capture_output=True, |
There was a problem hiding this comment.
Should we try to stream this output to logger? I think for build_image we previously had something like
if logger.isEnabledFor(logging.DEBUG):
capture_output = False
else:
capture_output = True
subprocess.run(
cmd,
check=True,
capture_output=capture_output,
text=True,
)
| subprocess.run( | ||
| cmd, | ||
| check=True, | ||
| capture_output=True, |
There was a problem hiding this comment.
Should we try to stream this output to logger? I think for build_image we previously had something like
if logger.isEnabledFor(logging.DEBUG):
capture_output = False
else:
capture_output = True
subprocess.run(
cmd,
check=True,
capture_output=capture_output,
text=True,
)
| - 'DOCKER_BUILDKIT=1' | ||
| images: | ||
| - '{}' | ||
| """ |
There was a problem hiding this comment.
We can label the {} fields with variable names to make it a bit more readable, e.g.,
BUILD_TEMPLATE = """
steps:
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', '{tag_name}', '.']
env:
- 'DOCKER_BUILDKIT=1'
images:
- '{image_name}'
"""
then do
BUILD_TEMPLATE.format(tag_name=image, image_name=image)
|
/close As not planned. |
…esn't use caching.