Thanks to visit codestin.com
Credit goes to github.com

Skip to content

setup_env.sh fails silently when gcloud commands error, creating invalid .env file #15

@URAYUSHJAIN

Description

@URAYUSHJAIN

The setup/setup_env.sh script currently lacks error handling for critical gcloud operations. If API enablement or API key creation fails (common for users with restricted IAM permissions), the script continues execution and generates a .env file with missing or empty values.

This leads to a poor developer experience where the setup appears successful, but the agent fails during adk web execution with cryptic errors regarding the MAPS_API_KEY.

Steps to Reproduce

  1. Run ./setup/setup_env.sh using a service account or user lacking the roles/serviceusage.apiKeysAdmin role.
  2. Observe that the script attempts to create an API key, fails, but still creates a .env file and reports no error.
  3. Run adk web.
  4. The agent fails to initialize the Maps toolset because the environment variable is invalid.

Expected Behavior

The script should exit immediately upon any command failure with a descriptive error message indicating which command failed and which IAM roles might be required (e.g., roles/serviceusage.serviceUsageAdmin or roles/serviceusage.apiKeysAdmin).

Proposed Solution

I can submit a Pull Request to implement set -e for global error handling and explicit validation for the API key generation variable.

Suggested changes to setup/setup_env.sh:

#!/bin/bash
# Exit on any error
set -e

echo "Enabling required Google Cloud APIs..."
# Enable APIs with validation
gcloud services enable compute.googleapis.com \
    bigquery.googleapis.com \
    maps-backend.googleapis.com --quiet \
    || { echo "❌ Failed to enable APIs. Check permissions (roles/serviceusage.serviceUsageAdmin required)."; exit 1; }

echo "Creating API key..."
# Create API key with validation
MAPS_API_KEY=$(gcloud alpha services api-keys create \
    --display-name="MCP Bakery Key" \
    --format="value(name)")

if [ -z "$MAPS_API_KEY" ]; then
    echo "❌ Failed to create MAPS_API_KEY. Check permissions (roles/serviceusage.apiKeysAdmin required)."
    exit 1
fi

# Create the .env file only if the above steps succeeded
echo "GOOGLE_CLOUD_PROJECT=$(gcloud config get-value project)" > .env
echo "MAPS_API_KEY=$MAPS_API_KEY" >> .env
echo "✅ Environment configured successfully."

Files Affected

  • google/mcp/examples/launchmybakery/setup/setup_env.sh

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions