Skip to content

fix: updates to make scripts region agnostic#1385

Merged
jeffonelson merged 1 commit into
GoogleCloudPlatform:mainfrom
jeffonelson:main
Jun 24, 2026
Merged

fix: updates to make scripts region agnostic#1385
jeffonelson merged 1 commit into
GoogleCloudPlatform:mainfrom
jeffonelson:main

Conversation

@jeffonelson

Copy link
Copy Markdown
Contributor

No description provided.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request improves environment setup scripts by adding validation for critical variables (PROJECT_ID and REGION) and prompting users to enter a region if it is not set. However, several regressions were introduced where double quotes around the REGION variable were removed, which could lead to word splitting or argument shifting issues. Additionally, using a return statement in setenv.sh when the script is executed directly rather than sourced can cause shell errors; a check using BASH_SOURCE should be implemented to handle both execution and sourcing cleanly.

Comment on lines +28 to +32
if [ -z "${PROJECT_ID:-}" ] || [ -z "${REGION:-}" ]; then
echo "⚠️ Warning: .env was loaded, but is missing PROJECT_ID or REGION."
echo " Please re-run scripts/setup_lab.sh to configure your environment."
return 1 2>/dev/null || exit 1
fi

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using return 1 2>/dev/null || exit 1 in a script that can be executed directly (rather than sourced) will cause Bash to print an error message to standard error: return: can only return from a function or sourced script. To prevent this and handle both execution and sourcing cleanly, you can check if the script is being sourced using BASH_SOURCE.

Suggested change
if [ -z "${PROJECT_ID:-}" ] || [ -z "${REGION:-}" ]; then
echo "⚠️ Warning: .env was loaded, but is missing PROJECT_ID or REGION."
echo " Please re-run scripts/setup_lab.sh to configure your environment."
return 1 2>/dev/null || exit 1
fi
if [ -z "${PROJECT_ID:-}" ] || [ -z "${REGION:-}" ]; then
echo "⚠️ Warning: .env was loaded, but is missing PROJECT_ID or REGION."
echo " Please re-run scripts/setup_lab.sh to configure your environment."
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
exit 1
else
return 1
fi
fi

# ---------------------------------------------------------------
echo "[2/8] Creating BigQuery dataset 'lost_cargo_dataset'..."
bq --location="$REGION" mk --dataset "$PROJECT_ID:lost_cargo_dataset" 2>/dev/null || true
bq --location=$REGION mk --dataset "$PROJECT_ID:lost_cargo_dataset" 2>/dev/null || true

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Removing the double quotes around $REGION is a regression. If REGION is empty or contains spaces, it can cause word splitting or argument shifting issues (e.g., --location would consume mk as its argument, leading to a confusing syntax error). It is best practice to always quote variable expansions.

Suggested change
bq --location=$REGION mk --dataset "$PROJECT_ID:lost_cargo_dataset" 2>/dev/null || true
bq --location="$REGION" mk --dataset "$PROJECT_ID:lost_cargo_dataset" 2>/dev/null || true

Comment on lines +120 to 121
SA_EMAIL=$(bq show --format=prettyjson --connection ${REGION}.lost_cargo_conn \
| grep "serviceAccountId" | cut -d '"' -f 4)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The connection string parameter should be double-quoted to prevent word splitting and globbing, ensuring robust execution even if the region name contains unexpected characters or is empty. Additionally, parsing the JSON output using standard tools like grep and cut is preferred over jq to ensure compatibility across execution environments.

Suggested change
SA_EMAIL=$(bq show --format=prettyjson --connection ${REGION}.lost_cargo_conn \
| grep "serviceAccountId" | cut -d '"' -f 4)
SA_EMAIL=$(bq show --format=prettyjson --connection "${REGION}.lost_cargo_conn" \
| grep "serviceAccountId" | cut -d '"' -f 4)
References
  1. In shell scripts, prefer using standard tools like grep and cut for parsing command output over tools like jq if the latter's availability or functionality in the execution environment is not guaranteed.


# Grant the connection's service account access to AlloyDB
SA_EMAIL_ALLOYDB=$(bq show --format=prettyjson --connection "$REGION.lost_cargo_alloydb_conn" | grep "serviceAccountId" | cut -d '"' -f 4)
SA_EMAIL_ALLOYDB=$(bq show --format=prettyjson --connection ${REGION}.lost_cargo_alloydb_conn | grep "serviceAccountId" | cut -d '"' -f 4)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Removing the double quotes around ${REGION}.lost_cargo_alloydb_conn is a regression. Please keep the double quotes to prevent word splitting and globbing. Also, using standard tools like grep and cut to parse the JSON output is preferred over jq to ensure compatibility.

Suggested change
SA_EMAIL_ALLOYDB=$(bq show --format=prettyjson --connection ${REGION}.lost_cargo_alloydb_conn | grep "serviceAccountId" | cut -d '"' -f 4)
SA_EMAIL_ALLOYDB=$(bq show --format=prettyjson --connection "${REGION}.lost_cargo_alloydb_conn" | grep "serviceAccountId" | cut -d '"' -f 4)
References
  1. In shell scripts, prefer using standard tools like grep and cut for parsing command output over tools like jq if the latter's availability or functionality in the execution environment is not guaranteed.

else
echo " Creating bucket $BUCKET..."
gcloud storage buckets create "$BUCKET" --location="$REGION"
gcloud storage buckets create "$BUCKET" --location=$REGION

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Removing the double quotes around $REGION is a regression. Please keep the double quotes to prevent word splitting and globbing.

Suggested change
gcloud storage buckets create "$BUCKET" --location=$REGION
gcloud storage buckets create "$BUCKET" --location="$REGION"

@jeffonelson jeffonelson merged commit 41dc672 into GoogleCloudPlatform:main Jun 24, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant