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.
pip install google-cloud-runVerified import paths — ran on the pinned version, not inferred.
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.
Consult the official client library documentation (e.g., `google.cloud.run_v2`) and the Cloud Run Admin API documentation to confirm the correct API version and client methods for your use case.
Always use f-strings or `.format()` to construct resource names dynamically, ensuring all path components (project ID, location, service ID) are correctly included and ordered. For example, `parent = f"projects/{project_id}/locations/{location}"`.To enable default logging, set the `GOOGLE_SDK_PYTHON_LOGGING_SCOPE` environment variable to `google` or `google.cloud.run_v2`. For advanced configuration, use Python's `logging` module to set up handlers and formatters, e.g., `logging.basicConfig(level=logging.INFO)`.
Grant the service account or user credentials used by your application the necessary IAM roles, such as 'Cloud Run Admin' (`roles/run.admin`) or more granular roles like 'Cloud Run Developer' (`roles/run.developer`) for deployment, or 'Cloud Run Viewer' (`roles/run.viewer`) for read-only access.
Ensure the Google Cloud Project ID is accessible to the client library. The most common methods are: 1. Setting the `GOOGLE_CLOUD_PROJECT` environment variable (or other recognized variables like `GOOGLE_CLOUD_PROJECT_ID`). 2. Explicitly passing `project='your-project-id'` to the client constructor (e.g., `run_v2.ServicesClient(project='your-project-id')`) or relevant method calls.
Set the `GOOGLE_CLOUD_PROJECT_ID` environment variable (or `GCLOUD_PROJECT`, `GOOGLE_CLOUD_PROJECT`) before running your application, or ensure the project ID is explicitly passed to relevant client methods or during client initialization.
Ensure your Python application binds to all network interfaces (`0.0.0.0`) and the port specified by the `PORT` environment variable. For example, using a common web framework like Flask: `app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 8080)))`. Check your application logs for specific startup errors.Add `google-cloud-run` (and any other necessary `google-cloud` libraries) to your `requirements.txt` file and ensure that `pip install -r requirements.txt` is run during your Docker image build process or by the Cloud Run buildpacks if deploying from source.
Grant the appropriate IAM roles to the deploying identity or the Cloud Run service agent. This often includes `roles/storage.objectViewer` for accessing the image, and `roles/run.admin` or `roles/run.developer` for managing Cloud Run services, on the respective project or repository.
Update your Google Cloud SDK components by running `gcloud components update`. If the issue persists, consider ensuring `gcloud` uses a compatible Python version, potentially by setting a specific Python executable in your environment or reinstalling the SDK.
Verify the `ENTRYPOINT` or `CMD` instruction in your Dockerfile, or the `--command` and `--args` parameters in your `gcloud run jobs deploy` command, to ensure they point to a valid, executable file within the container and provide the correct arguments for a standalone job execution.