setup_env.sh fails silently when gcloud commands error, creating invalid .env file
- Dominant language
- No language data
- Stars
- 4.6k
- Forks
- 531
- Avg merge
- 1m
- Merged PRs (30d)
- 1
Description
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`:**
```bash
#!/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`
---
Contributor guide
Assessment
This issue has not been assessed yet.