Set build environment variables (source deploy)

Build environment variables are key-value pairs that let you pass configuration information to buildpacks when deploying a function from source code. For example, at build time, you might want to customize compiler options, specify build-time certificates, configure parameters, and so forth.

This page shows how to set build environment variables that are available at build time, and is relevant for platform developers who are deploying functions in Cloud Run.

Before you begin

Enable the Cloud Build API:

gcloud services enable cloudbuild.googleapis.com

Required roles

To get the permissions that you need to deploy from source, ask your administrator to grant you the following IAM roles on your project:

For a list of IAM roles and permissions that are associated with Cloud Run, see Cloud Run IAM roles and Cloud Run IAM permissions. If your Cloud Run service interfaces with Google Cloud APIs, such as Cloud Client Libraries, see the service identity configuration guide. For more information about granting roles, see deployment permissions and manage access.

Set build environment variables

You can set build environment variables to establish new variables or completely replace existing build variables.

gcloud

You can set a build environment variables when deploying a function from source code:

gcloud beta run deploy SERVICE \
  --source . \
  --function FUNCTION_ENTRY_POINT \
  --set-build-env-vars KEY1=VALUE1,KEY2=VALUE2

Replace:

  • SERVICE with name of your Cloud Run function.
  • FUNCTION_ENTRY_POINT with the entry point to your function in your source code.
  • KEY1=VALUE1,KEY2=VALUE2 with the comma-separated list of variable names and their values that are deployed alongside a function that let you pass configuration information to buildpacks.

Update build environment variables

You can update build environment variables for existing functions. This is a non-destructive approach that changes or adds build environment variables, but does not delete the build environment variables.

gcloud

To update a build environment variable for existing functions:

gcloud beta run deploy SERVICE \
  --source . \
  --function FUNCTION_ENTRY_POINT \
  --update-build-env-vars KEY1=VALUE1,KEY2=VALUE2

Delete build environment variables

You can delete build environment variables for existing functions.

gcloud

To remove build environment variables, use the --remove-build-env-vars for existing functions:

gcloud beta run deploy SERVICE \
  --source . \
  --function FUNCTION_ENTRY_POINT \
  --remove-build-env-vars KEY1=VALUE1,KEY2=VALUE2

Alternatively, you can clear build environment variables by using the --clear-build-env-vars for existing functions:

gcloud beta run deploy SERVICE \
  --source . \
  --function FUNCTION_ENTRY_POINT \
  --clear-build-env-vars KEY1=VALUE1,KEY2=VALUE2

Build environment variables file

You use a build environment variables file for existing functions.

gcloud

To set build environment variables from a file:

gcloud beta run deploy SERVICE \
  --source . \
  --function FUNCTION_ENTRY_POINT \
  --build-env-vars-file FILE_NAME.yaml

Replace FILE_NAME.yaml where the contents of the file looks as follows:

 KEY1: VALUE1
 KEY2: VALUE2