Google Cloud Build
The `google-cloud-build` client library provides Python APIs to interact with Google Cloud Build, a service that executes your builds on Google Cloud Platform infrastructure. It allows users to define custom workflows for building, testing, and deploying across various environments. The library is currently at version 3.35.0 and is part of the Google Cloud Python client libraries, which receive frequent updates across their various service clients, often on a weekly or bi-weekly basis for individual clients.
Warnings
- gotcha Application Default Credentials (ADC) is the recommended authentication method. Avoid hardcoding service account key files in your code, especially in production environments. Use `gcloud auth application-default login` for local development or rely on attached service accounts for GCP deployments.
- breaking Cloud Build has undergone changes to how it uses service accounts for new projects and the handling of the 'Service Account User' role. This might impact permission configurations, requiring explicit configuration for service account impersonation for managed services.
- gotcha Build steps in Cloud Build have a default timeout of 10 minutes. Long-running operations may fail with a 'Step exceeded maximum allowed runtime' error if this limit is reached.
- gotcha The logging events from `google-cloud-build` (and other Google Cloud Python client libraries) may contain sensitive information, and their occurrence, level, and content can change without being flagged as a breaking change.
Install
-
pip install google-cloud-build
Imports
- CloudBuildClient
from google.cloud import build_v1 client = build_v1.CloudBuildClient()
Quickstart
import os
from google.cloud import build_v1
from google.api_core.exceptions import GoogleAPIError
project_id = os.environ.get('GOOGLE_CLOUD_PROJECT', 'your-gcp-project-id')
try:
# Initialize the Cloud Build client
client = build_v1.CloudBuildClient()
# List recent builds for the specified project
print(f"Listing recent builds for project: {project_id}")
request = build_v1.ListBuildsRequest(project_id=project_id)
builds = client.list_builds(request=request)
if not builds.builds:
print("No builds found.")
else:
for build in builds.builds:
print(f" Build ID: {build.id}, Status: {build.status.name}, Create Time: {build.create_time.isoformat()}")
except GoogleAPIError as e:
print(f"An API error occurred: {e}")
print("Please ensure the Cloud Build API is enabled and your credentials have sufficient permissions.")
except Exception as e:
print(f"An unexpected error occurred: {e}")