Skip to content

Adds functionality to build Docker images using gcloud build. This do…#83

Closed
brp-hiverge wants to merge 1 commit into
mainfrom
simplest-gcloud-build
Closed

Adds functionality to build Docker images using gcloud build. This do…#83
brp-hiverge wants to merge 1 commit into
mainfrom
simplest-gcloud-build

Conversation

@brp-hiverge

Copy link
Copy Markdown
Contributor

…esn't use caching.

@hiverge-robot hiverge-robot added needs-triage Indicates an issue or PR lacks a label and requires one. needs-priority Indicates a PR lacks a label and requires one. do-not-merge/needs-kind Indicates a PR lacks a label and requires one. approved Indicates a PR has been approved by an approver from all required OWNERS files. labels Dec 4, 2025

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_TEMPLATE constant 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.

Comment on lines +75 to +77
build_yaml_path = f"{context}/cloudbuild.yaml"
with open(build_yaml_path, "w", encoding="utf-8") as f:
f.write(build_yaml)

Copilot AI Dec 4, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
with open(build_yaml_path, "w", encoding="utf-8") as f:
f.write(build_yaml)

cmd = ["gcloud", "builds", "submit", "--config", build_yaml_path, "."]

Copilot AI Dec 4, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The command hardcodes '.' as the source directory, but should use the context parameter to maintain consistency with the function's design. Change '.' to context.

Suggested change
cmd = ["gcloud", "builds", "submit", "--config", build_yaml_path, "."]
cmd = ["gcloud", "builds", "submit", "--config", build_yaml_path, context]

Copilot uses AI. Check for mistakes.
subprocess.run(
cmd,
check=True,
capture_output=True,

Copilot AI Dec 4, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
capture_output=True,

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:
- '{}'
"""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@ky-hiverge

Copy link
Copy Markdown
Member

/close

As not planned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. do-not-merge/needs-kind Indicates a PR lacks a label and requires one. needs-priority Indicates a PR lacks a label and requires one. needs-triage Indicates an issue or PR lacks a label and requires one.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants