From 69a3c4ed5d746bd50275fca550717d505b292432 Mon Sep 17 00:00:00 2001 From: mikemikimike <13286568797@163.com> Date: Fri, 28 Aug 2026 06:21:36 +0800 Subject: [PATCH] fix(cli): preserve Docker dependency cache --- src/google/adk/cli/cli_deploy.py | 30 ++++++++++++++++--- .../cli/utils/test_cli_deploy_to_cloud_run.py | 18 +++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/google/adk/cli/cli_deploy.py b/src/google/adk/cli/cli_deploy.py index df31bfe0b5d..467b66a96df 100644 --- a/src/google/adk/cli/cli_deploy.py +++ b/src/google/adk/cli/cli_deploy.py @@ -175,6 +175,18 @@ def _ensure_agent_engine_dependency(requirements_txt_path: str) -> None: f.write(f'google-adk[a2a]=={__version__}\n') +def _get_agent_requirements_copy( + app_name: str, requirements_txt_path: str +) -> str: + """Returns the Dockerfile step that copies an agent's requirements file.""" + if not os.path.exists(requirements_txt_path): + return '' + return ( + f'COPY --chown=myuser:myuser "agents/{app_name}/requirements.txt" ' + f'"/app/agents/{app_name}/requirements.txt"' + ) + + _DOCKERFILE_TEMPLATE: Final[str] = """ FROM python:3.11-slim WORKDIR /app @@ -200,6 +212,11 @@ def _ensure_agent_engine_dependency(requirements_txt_path: str) -> None: RUN python -c "import os, glob, google.adk.cli as cli; d = os.path.dirname(cli.__file__); [os.remove(f) for f in glob.glob(os.path.join(d, 'dev_server*'))]; [os.remove(f) for f in glob.glob(os.path.join(d, '__pycache__', 'dev_server*'))]" || true # Install ADK - End +# Install Agent Deps - Start +{agent_requirements_copy} +{install_agent_deps} +# Install Agent Deps - End + # Copy agent - Start # Set permission @@ -207,10 +224,6 @@ def _ensure_agent_engine_dependency(requirements_txt_path: str) -> None: {extra_packages_copy} # Copy agent - End -# Install Agent Deps - Start -{install_agent_deps} -# Install Agent Deps - End - EXPOSE {port} CMD adk {command} --port={port} {host_option} {service_option} {trace_to_cloud_option} {otel_to_cloud_option} {allow_origins_option} {a2a_option} {trigger_sources_option} {gemini_enterprise_option}{express_mode_option} "/app/agents" @@ -855,6 +868,9 @@ def to_cloud_run( app_name=app_name, port=port, command='api_server --with_ui' if with_ui else 'api_server', + agent_requirements_copy=_get_agent_requirements_copy( + app_name, requirements_txt_path + ), install_agent_deps=install_agent_deps, service_option=_get_service_option_by_adk_version( adk_version, @@ -1380,6 +1396,9 @@ def create_dockerfile_for_agent_engine(resource_name: str) -> None: app_name=app_name, port=8080, command='api_server', + agent_requirements_copy=_get_agent_requirements_copy( + app_name, requirements_txt_path + ), install_agent_deps=install_agent_deps, service_option=_get_service_option_by_adk_version( adk_version, @@ -1560,6 +1579,9 @@ def to_gke( app_name=app_name, port=port, command='api_server --with_ui' if with_ui else 'api_server', + agent_requirements_copy=_get_agent_requirements_copy( + app_name, requirements_txt_path + ), install_agent_deps=install_agent_deps, service_option=_get_service_option_by_adk_version( adk_version, diff --git a/tests/unittests/cli/utils/test_cli_deploy_to_cloud_run.py b/tests/unittests/cli/utils/test_cli_deploy_to_cloud_run.py index 35ebd636ab7..17c408cd5c2 100644 --- a/tests/unittests/cli/utils/test_cli_deploy_to_cloud_run.py +++ b/tests/unittests/cli/utils/test_cli_deploy_to_cloud_run.py @@ -158,11 +158,29 @@ def test_to_cloud_run_happy_path( # Check agent dependencies installation based on include_requirements if include_requirements: + requirements_copy = ( + 'COPY --chown=myuser:myuser "agents/agent/requirements.txt" ' + '"/app/agents/agent/requirements.txt"' + ) + agent_copy = ( + 'COPY --chown=myuser:myuser "agents/agent/" "/app/agents/agent/"' + ) + assert requirements_copy in dockerfile_content assert ( 'RUN pip install -r "/app/agents/agent/requirements.txt"' in dockerfile_content ) + assert ( + dockerfile_content.index(requirements_copy) + < dockerfile_content.index( + 'RUN pip install -r "/app/agents/agent/requirements.txt"' + ) + < dockerfile_content.index(agent_copy) + ) else: + assert 'COPY --chown=myuser:myuser "agents/agent/requirements.txt"' not in ( + dockerfile_content + ) assert "# No requirements.txt found." in dockerfile_content assert (