Google Cloud Build

3.35.0 · active · verified Sun Mar 29

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

Install

Imports

Quickstart

This quickstart initializes the Cloud Build client and lists recent builds for a specified Google Cloud project. Ensure the `GOOGLE_CLOUD_PROJECT` environment variable is set or replace 'your-gcp-project-id' with your project ID. The Cloud Build API must be enabled, and your credentials must have the `cloudbuild.builds.list` permission.

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}")

view raw JSON →