Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 43 additions & 20 deletions src/google/adk/cli/cli_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def to_cloud_run(
artifact_service_uri: Optional[str] = None,
memory_service_uri: Optional[str] = None,
a2a: bool = False,
extra_gcloud_args: Optional[tuple[str, ...]] = None,
):
"""Deploys an agent to Google Cloud Run.

Expand Down Expand Up @@ -222,26 +223,48 @@ def to_cloud_run(
click.echo('Deploying to Cloud Run...')
region_options = ['--region', region] if region else []
project = _resolve_project(project)
subprocess.run(
[
'gcloud',
'run',
'deploy',
service_name,
'--source',
temp_folder,
'--project',
project,
*region_options,
'--port',
str(port),
'--verbosity',
log_level.lower() if log_level else verbosity,
'--labels',
'created-by=adk',
],
check=True,
)

# Build the command with extra gcloud args
gcloud_cmd = [
'gcloud',
'run',
'deploy',
service_name,
'--source',
temp_folder,
'--project',
project,
*region_options,
'--port',
str(port),
'--verbosity',
log_level.lower() if log_level else verbosity,
]

# Handle labels specially - merge user labels with ADK label
user_labels = []
extra_args_without_labels = []

if extra_gcloud_args:
for arg in extra_gcloud_args:
if arg.startswith('--labels='):
# Extract user-provided labels
user_labels_value = arg[9:] # Remove '--labels=' prefix
user_labels.append(user_labels_value)
else:
extra_args_without_labels.append(arg)

# Combine ADK label with user labels
all_labels = ['created-by=adk']
all_labels.extend(user_labels)
labels_arg = ','.join(all_labels)

gcloud_cmd.extend(['--labels', labels_arg])

# Add any remaining extra passthrough args
gcloud_cmd.extend(extra_args_without_labels)

subprocess.run(gcloud_cmd, check=True)
finally:
click.echo(f'Cleaning up the temp folder: {temp_folder}')
shutil.rmtree(temp_folder)
Expand Down
5 changes: 4 additions & 1 deletion src/google/adk/cli/cli_tools_click.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ def cli_api_server(
server.run()


@deploy.command("cloud_run")
@deploy.command("cloud_run", context_settings={"allow_extra_args": True, "allow_interspersed_args": False})
@click.option(
"--project",
type=str,
Expand Down Expand Up @@ -971,7 +971,9 @@ def cli_api_server(
# TODO: Add eval_storage_uri option back when evals are supported in Cloud Run.
@adk_services_options()
@deprecated_adk_services_options()
@click.pass_context
def cli_deploy_cloud_run(
ctx,
agent: str,
project: Optional[str],
region: Optional[str],
Expand Down Expand Up @@ -1029,6 +1031,7 @@ def cli_deploy_cloud_run(
artifact_service_uri=artifact_service_uri,
memory_service_uri=memory_service_uri,
a2a=a2a,
extra_gcloud_args=ctx.args,
)
except Exception as e:
click.secho(f"Deploy failed: {e}", fg="red", err=True)
Expand Down
43 changes: 43 additions & 0 deletions tests/unittests/cli/utils/test_cli_tools_click.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,49 @@ def _boom(*_a: Any, **_k: Any) -> None: # noqa: D401
assert "Deploy failed: boom" in result.output


def test_cli_deploy_cloud_run_passthrough_args(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Extra args should be passed through to the gcloud command."""
rec = _Recorder()
monkeypatch.setattr(cli_tools_click.cli_deploy, "to_cloud_run", rec)

agent_dir = tmp_path / "agent_passthrough"
agent_dir.mkdir()
runner = CliRunner()
result = runner.invoke(
cli_tools_click.main,
[
"deploy",
"cloud_run",
"--project",
"test-project",
"--region",
"us-central1",
str(agent_dir),
"--labels=test-label=test",
"--memory=1Gi",
"--cpu=1",
],
)
# Print debug information if the test fails
if result.exit_code != 0:
print(f"Exit code: {result.exit_code}")
print(f"Output: {result.output}")
print(f"Exception: {result.exception}")

assert result.exit_code == 0
assert rec.calls, "cli_deploy.to_cloud_run must be invoked"

# Check that extra_gcloud_args were passed correctly
called_kwargs = rec.calls[0][1]
extra_args = called_kwargs.get("extra_gcloud_args")
assert extra_args is not None
assert "--labels=test-label=test" in extra_args
assert "--memory=1Gi" in extra_args
assert "--cpu=1" in extra_args


# cli deploy agent_engine
def test_cli_deploy_agent_engine_success(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
Expand Down
Loading