Google Cloud Run Client Library

0.16.0 · active · verified Sun Mar 29

The `google-cloud-run` Python library is the official client library for interacting with the Google Cloud Run Admin API. It enables developers to programmatically manage Cloud Run services and jobs, including deployment, configuration, and monitoring. Currently at version 0.16.0, this library is part of the larger `google-cloud-python` monorepo and receives frequent updates, typically with a release cadence aligning with other Google Cloud client libraries.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate the `ServicesClient` and list existing Cloud Run services within a specified Google Cloud project and location. Ensure your environment is authenticated (e.g., via `gcloud auth application-default login` or by setting `GOOGLE_APPLICATION_CREDENTIALS`) and that `GOOGLE_CLOUD_PROJECT_ID` and `GOOGLE_CLOUD_LOCATION` environment variables are set.

import os
from google.cloud.run_v2 import ServicesClient

def list_cloud_run_services(project_id: str, location: str):
    """Lists Cloud Run services in a given project and location."""
    client = ServicesClient()
    parent = f"projects/{project_id}/locations/{location}"

    try:
        # Paged iteration over services
        print(f"Fetching services for parent: {parent}")
        for service in client.list_services(parent=parent):
            print(f"Service Name: {service.name}")
            print(f"  URI: {service.uri}")
            # You can access more service attributes here, e.g., service.traffic
            print("-" * 20)
    except Exception as e:
        print(f"Error listing services: {e}")

if __name__ == "__main__":
    # Set your Google Cloud Project ID and desired location
    # Ensure GOOGLE_APPLICATION_CREDENTIALS is set or you are running in a GCP environment
    # e.g., 'export GOOGLE_CLOUD_PROJECT_ID="your-project"'
    # e.g., 'export GOOGLE_CLOUD_LOCATION="us-central1"'
    project_id = os.environ.get("GOOGLE_CLOUD_PROJECT_ID", "your-project-id")
    location = os.environ.get("GOOGLE_CLOUD_LOCATION", "us-central1")

    if project_id == "your-project-id":
        print("Please set the GOOGLE_CLOUD_PROJECT_ID environment variable or replace 'your-project-id' in the script.")
    else:
        list_cloud_run_services(project_id, location)

view raw JSON →