set_env.sh


Overview

set_env.sh is a shell script designed to initialize and export essential Google Cloud environment variables required for deploying and managing services within a Google Cloud project. Its primary role is to verify authentication status, read and set the active Google Cloud project, and export several key environment variables such as project ID, project number, default service account, region, and identity tokens. The script must be sourced rather than executed directly to ensure that the exported variables persist in the current shell session.

This script is a critical component in the Environment Initialization phase within the Cloud Run Deployment Automation workflow, providing a consistent and authenticated environment context for subsequent deployment and operational scripts.


Purpose and Functionality

By exporting these variables, set_env.sh prepares the shell environment for deployment scripts like cloudrun.sh or cloudrun-secure.sh and runtime components that rely on consistent project and authentication context.


Detailed Breakdown

Script Configuration Section

PROJECT_FILE="~/project_id.txt"
GOOGLE_CLOUD_LOCATION="europe-west1"

Authentication Check

if gcloud auth print-access-token > /dev/null 2>&1; then
  echo "gcloud is authenticated."
else
  echo "Error: gcloud is not authenticated."
  echo "Please log in by running: gcloud auth login"
  return 1
fi

Project File Validation and Setting

PROJECT_FILE_PATH=$(eval echo $PROJECT_FILE) # Expands ~
if [ ! -f "$PROJECT_FILE_PATH" ]; then
  echo "Error: Project file not found at $PROJECT_FILE_PATH"
  echo "Please create $PROJECT_FILE_PATH containing your Google Cloud project ID."
  return 1
fi

PROJECT_ID_FROM_FILE=$(cat "$PROJECT_FILE_PATH")
gcloud config set project "$PROJECT_ID_FROM_FILE" --quiet

Exporting Core Google Cloud Variables

export PROJECT_ID=$(gcloud config get project)
export GOOGLE_CLOUD_PROJECT=$(gcloud config get project)
export PROJECT_NUMBER=$(gcloud projects describe ${PROJECT_ID} --format="value(projectNumber)")
export SERVICE_ACCOUNT_NAME=$(gcloud compute project-info describe --format="value(defaultServiceAccount)")

The script echoes the exported values for user confirmation.


Exporting Location and Region

export GOOGLE_CLOUD_LOCATION="$GOOGLE_CLOUD_LOCATION"
export REGION="$GOOGLE_CLOUD_LOCATION"

Exporting Application-Specific Variables

export ID_TOKEN=$(gcloud auth print-identity-token)
export GOOGLE_GENAI_USE_VERTEXAI="TRUE"

Usage Notes


Interaction with Other Components


Example Usage

source ./set_env.sh
# Output:
# --- Setting Google Cloud Environment Variables ---
# Checking gcloud authentication status...
# gcloud is authenticated.
# Setting gcloud config project to: your-project-id
# Exported PROJECT_ID=your-project-id
# Exported PROJECT_NUMBER=123456789012
# Exported SERVICE_ACCOUNT_NAME=your-project-number-compute@developer.gserviceaccount.com
# Exported GOOGLE_CLOUD_PROJECT=your-project-id
# Exported GOOGLE_CLOUD_LOCATION=europe-west1
# Exported REGION=europe-west1
# Exported GOOGLE_GENAI_USE_VERTEXAI=TRUE
#
# --- Environment setup complete ---

After sourcing, the environment variables like PROJECT_ID, REGION, and ID_TOKEN are available for the current shell session.


Implementation Details and Algorithms


Mermaid Flowchart: Script Workflow

flowchart TD
A[Start: source set_env.sh] --> B[Check gcloud Authentication]
B -->|Authenticated| C[Expand and Verify project_id.txt]
B -->|Not Authenticated| E[Print error and return]
C -->|File Exists| D[Set gcloud project from file]
C -->|File Missing| F[Print error and return]
D --> G[Export PROJECT_ID, PROJECT_NUMBER, SERVICE_ACCOUNT_NAME]
G --> H[Export REGION and GOOGLE_CLOUD_LOCATION]
H --> I[Export ID_TOKEN and VertexAI flag]
I --> J[Print success messages]
J --> K[End: Environment ready]

References to Related Topics


This documentation covers all critical aspects of set_env.sh, providing detailed insight into its purpose, usage, and role within the broader Google Cloud deployment automation framework.