Authentication

In general, the google-cloud-run-client library usesService Accountcredentials to connect to Google Cloud services. When running withinGoogle Cloud Platform environments thecredentials will be discovered automatically. When running on otherenvironments, the Service Account credentials can be specified by providing thepath to theJSON keyfilefor the account (or the JSON itself) inenvironment variables. Additionally, Cloud SDKcredentials can also be discovered automatically, but this is only recommendedduring development.

Quickstart

  1. Create a service account and credentials.
  2. Set theenvironment variable.
export GOOGLE_CLOUD_CREDENTIALS=path/to/keyfile.json
  1. Initialize the client.
require"google/cloud/run"client=Google::Cloud::Run.executions

Credential Lookup

The google-cloud-run-client library aims to make authenticationas simple as possible, and provides several mechanisms to configure your systemwithout requiringService Account Credentials directly in code.

Credentials are discovered in the following order:

  1. Specify credentials in method arguments
  2. Specify credentials in configuration
  3. Discover credentials path in environment variables
  4. Discover credentials JSON in environment variables
  5. Discover credentials file in the Cloud SDK's path
  6. Discover GCP credentials

Google Cloud Platform environments

When running on Google Cloud Platform (GCP), including Google Compute Engine(GCE), Google Kubernetes Engine (GKE), Google App Engine (GAE), Google CloudFunctions (GCF) and Cloud Run,Credentials are discovered automatically.Code should be written as if already authenticated.

Environment Variables

TheCredentials JSON can be placed in environment variables instead ofdeclaring them directly in code. Each service has its own environment variable,allowing for different service accounts to be used for different services. (Seethe READMEs for the individual service gems for details.) The path to theCredentials JSON file can be stored in the environment variable, or theCredentials JSON itself can be stored for environments such as Dockercontainers where writing files is difficult or not encouraged.

The environment variables that google-cloud-run-clientchecks for credentials are configured on the service Credentials class (such as::Google::Cloud::Run::V2::Executions::Credentials):

  • GOOGLE_CLOUD_CREDENTIALS - Path to JSON file, or JSON contents
  • GOOGLE_CLOUD_KEYFILE - Path to JSON file, or JSON contents
  • GOOGLE_APPLICATION_CREDENTIALS - Path to JSON file
require"google/cloud/run"ENV["GOOGLE_CLOUD_CREDENTIALS"]="path/to/keyfile.json"client=Google::Cloud::Run.executions

Configuration

The path to theCredentials JSON file can be configured instead of storingit in an environment variable. Either on an individual client initialization:

require"google/cloud/run"client=Google::Cloud::Run.executionsdo|config|config.credentials="path/to/keyfile.json"end

Or globally for all clients:

require"google/cloud/run"Google::Cloud::Run.configuredo|config|config.credentials="path/to/keyfile.json"endclient=Google::Cloud::Run.executions

Cloud SDK

This option allows for an easy way to authenticate during development. Ifcredentials are not provided in code or in environment variables, then Cloud SDKcredentials are discovered.

To configure your system for this, simply:

  1. Download and install the Cloud SDK
  2. Authenticate using OAuth 2.0$ gcloud auth application-default login
  3. Write code as if already authenticated.

NOTE: This isnot recommended for running in production. The Cloud SDKshould only be used during development.

Creating a Service Account

Google Cloud requiresService Account Credentials toconnect to the APIs. You will use theJSON key file toconnect to most services with google-cloud-run-client.

If you are not running this client withinGoogle Cloud Platform environments, youneed a Google Developers service account.

  1. Visit theGoogle Cloud Console.
  2. Create a new project or click on an existing project.
  3. Activate the menu in the upper left and selectAPIs & Services. Fromhere, you will enable the APIs that your application requires.

    Note: You may need to enable billing in order to use these services.

  4. SelectCredentials from the side navigation.

    Find the "Create credentials" drop down near the top of the page, and select"Service account" to be guided through downloading a new JSON key file.

    If you want to re-use an existing service account, you can easily generate anew key file. Just select the account you wish to re-use, click the penciltool on the right side to edit the service account, select theKeys tab,and then selectAdd Key.

    The key file you download will be used by this library to authenticate APIrequests and should be stored in a secure location.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-10-30 UTC.