Skip to content

Commit 0c40542

Browse files
yeesiancopybara-github
authored andcommitted
fix: Handle project and location in the .env properly when deploying to Agent Engine
PiperOrigin-RevId: 770161930
1 parent c9e2655 commit 0c40542

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

src/google/adk/cli/cli_deploy.py

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,10 @@ def to_agent_engine(
242242
agent_folder: str,
243243
temp_folder: str,
244244
adk_app: str,
245-
project: str,
246-
region: str,
247245
staging_bucket: str,
248246
trace_to_cloud: bool,
247+
project: Optional[str] = None,
248+
region: Optional[str] = None,
249249
display_name: Optional[str] = None,
250250
description: Optional[str] = None,
251251
requirements_file: Optional[str] = None,
@@ -287,7 +287,9 @@ def to_agent_engine(
287287
If not specified, the `requirements.txt` file in the `agent_folder` will
288288
be used.
289289
env_file (str): The filepath to the `.env` file for environment variables.
290-
If not specified, the `.env` file in the `agent_folder` will be used.
290+
If not specified, the `.env` file in the `agent_folder` will be used. The
291+
values of `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` will be
292+
overridden by `project` and `region` if they are specified.
291293
"""
292294
# remove temp_folder if it exists
293295
if os.path.exists(temp_folder):
@@ -306,13 +308,7 @@ def to_agent_engine(
306308
from vertexai import agent_engines
307309

308310
sys.path.append(temp_folder)
309-
310-
vertexai.init(
311-
project=_resolve_project(project),
312-
location=region,
313-
staging_bucket=staging_bucket,
314-
)
315-
click.echo('Vertex AI initialized.')
311+
project = _resolve_project(project)
316312

317313
click.echo('Resolving files and dependencies...')
318314
if not requirements_file:
@@ -333,6 +329,37 @@ def to_agent_engine(
333329

334330
click.echo(f'Reading environment variables from {env_file}')
335331
env_vars = dotenv_values(env_file)
332+
if 'GOOGLE_CLOUD_PROJECT' in env_vars:
333+
env_project = env_vars.pop('GOOGLE_CLOUD_PROJECT')
334+
if env_project:
335+
if project:
336+
click.secho(
337+
'Ignoring GOOGLE_CLOUD_PROJECT in .env as `--project` was'
338+
' explicitly passed and takes precedence',
339+
fg='yellow',
340+
)
341+
else:
342+
project = env_project
343+
click.echo(f'{project=} set by GOOGLE_CLOUD_PROJECT in {env_file}')
344+
if 'GOOGLE_CLOUD_LOCATION' in env_vars:
345+
env_region = env_vars.pop('GOOGLE_CLOUD_LOCATION')
346+
if env_region:
347+
if region:
348+
click.secho(
349+
'Ignoring GOOGLE_CLOUD_LOCATION in .env as `--region` was'
350+
' explicitly passed and takes precedence',
351+
fg='yellow',
352+
)
353+
else:
354+
region = env_region
355+
click.echo(f'{region=} set by GOOGLE_CLOUD_LOCATION in {env_file}')
356+
357+
vertexai.init(
358+
project=project,
359+
location=region,
360+
staging_bucket=staging_bucket,
361+
)
362+
click.echo('Vertex AI initialized.')
336363

337364
adk_app_file = f'{adk_app}.py'
338365
with open(

src/google/adk/cli/cli_tools_click.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -835,12 +835,18 @@ def cli_deploy_cloud_run(
835835
@click.option(
836836
"--project",
837837
type=str,
838-
help="Required. Google Cloud project to deploy the agent.",
838+
help=(
839+
"Required. Google Cloud project to deploy the agent. It will override"
840+
" GOOGLE_CLOUD_PROJECT in the .env file (if it exists)."
841+
),
839842
)
840843
@click.option(
841844
"--region",
842845
type=str,
843-
help="Required. Google Cloud region to deploy the agent.",
846+
help=(
847+
"Required. Google Cloud region to deploy the agent. It will override"
848+
" GOOGLE_CLOUD_LOCATION in the .env file (if it exists)."
849+
),
844850
)
845851
@click.option(
846852
"--staging_bucket",

0 commit comments

Comments
 (0)