Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions src/google/adk/cli/cli_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,7 @@ def run(
port=port,
command='api_server --with_ui' if with_ui else 'api_server',
install_agent_deps=install_agent_deps,
requirements_txt_path=requirements_txt_path,
service_options=_get_service_options_by_adk_version(
adk_version,
session_service_uri,
Expand Down Expand Up @@ -2100,6 +2101,7 @@ def create_dockerfile_for_agent_engine(resource_name: str) -> None:
port=8080,
command='api_server',
install_agent_deps=install_agent_deps,
requirements_txt_path=requirements_txt_path,
service_options=_get_service_options_by_adk_version(
adk_version,
session_service_uri or agent_engine_uri,
Expand Down Expand Up @@ -2316,6 +2318,7 @@ def to_gke(
port=port,
command='api_server --with_ui' if with_ui else 'api_server',
install_agent_deps=install_agent_deps,
requirements_txt_path=requirements_txt_path,
service_options=_get_service_options_by_adk_version(
adk_version,
session_service_uri,
Expand Down
74 changes: 69 additions & 5 deletions src/google/adk/cli/deployers/_dockerfile_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
from typing import Final

import click
from packaging.requirements import InvalidRequirement
from packaging.requirements import Requirement

# app_name is interpolated into the generated Dockerfile (COPY/RUN instructions
# and CMD) by _DOCKERFILE_TEMPLATE. It defaults to the basename of the agent
Expand Down Expand Up @@ -154,16 +156,14 @@ def _to_exec_form(instruction: str, args: list[str]) -> str:
{install_adk}
# Install ADK - End

# Copy agent - Start
# Stage extra packages before installing agent dependencies
{extra_packages_copy}

# Set permission
COPY --chown=myuser:myuser "agents/{app_name}/" "/app/agents/{app_name}/"
# Copy agent - End

# Install Agent Deps - Start
{agent_copy_before_deps}
{install_agent_deps}
{reinstall_extra_packages}
{agent_copy_after_deps}
# Remove dev_server.py to ensure production-safe endpoints only (disabling dev endpoints in production)
# Shell form on purpose: this instruction interpolates nothing, and `|| true`
# is shell syntax. Do not add a `{{...}}` placeholder to it without moving it to
Expand Down Expand Up @@ -281,6 +281,52 @@ def _render_install_agent_deps(
)


def _can_cache_agent_deps(requirements_txt_path: str) -> bool:
"""Returns whether requirements can be installed without the agent source.

Only ordinary named requirements are accepted. Pip supports additional
syntax, including local paths, nested requirements and constraints, editable
installs, and package search directories. Preserve copy-before-install for
these and any unrecognized syntax rather than risk a deployment regression.
"""
try:
with open(requirements_txt_path, encoding='utf-8-sig') as requirements:
for line in requirements:
# Pip joins continuations before stripping comments. Do not interpret
# a continued comment or requirement as independent physical lines.
if line.rstrip().endswith('\\'):
return False
line = re.split(r'(^|\s+)#', line, maxsplit=1)[0].strip()
if not line:
continue
try:
requirement = Requirement(line)
except InvalidRequirement:
return False
# A bare wheel/archive filename can also parse as a package name,
# but pip treats it as a local file rather than an index requirement.
if requirement.url is not None or requirement.name.lower().endswith((
'.whl',
'.zip',
'.tar',
'.gz',
'.bz2',
'.xz',
'.tgz',
'.tbz',
'.tbz2',
'.txz',
'.tlz',
'.lz',
'.lzma',
'.z',
)):
return False
except (OSError, UnicodeError):
return False
return True


def _render_dockerfile(
*,
app_name: str,
Expand All @@ -304,6 +350,7 @@ def _render_dockerfile(
extra_env_vars: str = '',
python_version: str = _DEFAULT_PYTHON_VERSION,
validate_agent_import: bool = False,
requirements_txt_path: str | None = None,
) -> str:
"""Renders the Dockerfile used by every `adk deploy` target.

Expand Down Expand Up @@ -344,6 +391,8 @@ def _render_dockerfile(
validate_agent_import: Whether to wrap `CMD` so `app_name` is preloaded on
the server's `AgentLoader` at container startup before binding `PORT`
(default: False).
requirements_txt_path: Staged requirements file to inspect for dependency
caching. If absent or not safely classifiable, copy agent source first.

Returns:
The Dockerfile contents.
Expand Down Expand Up @@ -388,11 +437,26 @@ def _render_dockerfile(
['pip', 'install', f'google-adk[{",".join(extras)}]=={adk_version}'],
)

agent_copy = (
f'COPY --chown=myuser:myuser "agents/{app_name}/"'
f' "/app/agents/{app_name}/"'
)
agent_copy_before_deps = agent_copy
agent_copy_after_deps = ''
if requirements_txt_path and _can_cache_agent_deps(requirements_txt_path):
agent_copy_before_deps = (
f'COPY --chown=myuser:myuser "agents/{app_name}/requirements.txt"'
f' "/app/agents/{app_name}/requirements.txt"'
)
agent_copy_after_deps = agent_copy

return _DOCKERFILE_TEMPLATE.format(
python_version=_validate_python_version(python_version),
app_name=app_name,
port=port,
install_adk=install_adk,
agent_copy_before_deps=agent_copy_before_deps,
agent_copy_after_deps=agent_copy_after_deps,
install_agent_deps=install_agent_deps,
extra_packages_copy=extra_packages_copy,
reinstall_extra_packages=reinstall_extra_packages,
Expand Down
64 changes: 64 additions & 0 deletions tests/unittests/cli/utils/test_cli_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3406,6 +3406,70 @@ def test_to_agent_engine_rejects_invalid_python_version_before_copying(
assert not (src_dir.parent / "tmp").exists()


@pytest.mark.parametrize("target", ["agent_engine", "gke"])
@pytest.mark.parametrize(
"requirements,cacheable",
[("pytest\n", True), ("./helper\n", False), ("-r extra.txt\n", False)],
)
def test_cloud_deployments_preserve_dependency_copy_order(
monkeypatch: pytest.MonkeyPatch,
agent_dir: Callable[[bool, bool], Path],
tmp_path: Path,
target: str,
requirements: str,
cacheable: bool,
) -> None:
"""Each cloud-specific generator selects the compatible source-copy order."""
source = agent_dir(True, False)
(source / "requirements.txt").write_text(requirements)
build = tmp_path / "build"
monkeypatch.setattr(shutil, "rmtree", lambda *args, **kwargs: None)
if target == "agent_engine":
recording_agentplatform = _make_recording_agentplatform([])
monkeypatch.setitem(sys.modules, "agentplatform", recording_agentplatform)
monkeypatch.setattr(
"src.google.adk.dependencies._agentplatform.agentplatform",
recording_agentplatform,
)

cli_deploy.to_agent_engine(
agent_folder=str(source),
temp_folder=str(build),
project="project",
region="us-central1",
adk_version="2.14.0",
)
else:
monkeypatch.setattr(
subprocess,
"run",
lambda *args, **kwargs: types.SimpleNamespace(stdout="deployed"),
)

cli_deploy.to_gke(
agent_folder=str(source),
temp_folder=str(build),
project="project",
region="us-central1",
cluster_name="cluster",
service_name="service",
app_name="agent",
port=8080,
trace_to_cloud=False,
otel_to_cloud=False,
with_ui=False,
adk_version="2.14.0",
log_level="info",
)

content = (build / "Dockerfile").read_text()
source_copy = (
'COPY --chown=myuser:myuser "agents/agent/" "/app/agents/agent/"'
)
install = 'RUN ["pip", "install", "-r", "/app/agents/agent/requirements.txt"]'
assert (content.index(install) < content.index(source_copy)) == cacheable


def test_to_gke_renders_python_version(
monkeypatch: pytest.MonkeyPatch,
agent_dir: Callable[[bool, bool], Path],
Expand Down
93 changes: 93 additions & 0 deletions tests/unittests/cli/utils/test_cli_deploy_to_cloud_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,99 @@ def test_dockerfile_template_preserves_multiline_ca_certificates_run() -> None:
assert "update-ca-certificates; \\\n" in content


@pytest.mark.parametrize("provider", ["docker", "cloud_run"])
@pytest.mark.parametrize(
"requirements,cacheable",
[
("requests==2.32.4\npandas>=2\n", True),
('requests[security]>=2; python_version >= "3.11"\n', True),
("# Dependencies\n\n requests==2.32.4 # HTTP client\n", True),
("\ufeffrequests==2.32.4\n", True),
("", True),
(".\n", False),
("./packages/helper\n", False),
("../helper\n", False),
("/app/agents/agent/helper\n", False),
("-e ./helper\n", False),
("--editable=./helper\n", False),
("helper @ file:///app/agents/agent/helper\n", False),
("helper @ ./helper\n", False),
("helper-1.0-py3-none-any.whl\n", False),
("helper-1.0.tar.gz\n", False),
("helper.zip\n", False),
("-r extra.txt\n", False),
("--requirement=extra.txt\n", False),
("-c constraints.txt\n", False),
("--constraint=constraints.txt\n", False),
("--find-links ./wheels\nrequests\n", False),
("requests\n--index-url https://example.com/simple\n", False),
("helper @ https://example.com/helper.whl\n", False),
("${LOCAL_DEPENDENCY}\n", False),
("requests \\\n ==2.32.4\n", False),
("# Continued comment \\\nrequests\n./helper\n", False),
(b"# coding: latin-1\n# \xe9\nrequests\n", False),
(None, False),
],
)
def test_deploy_orders_dependency_installation_safely(
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
provider: str,
requirements: str | bytes | None,
cacheable: bool,
) -> None:
"""Source-independent requirements install before source; others after it."""
source = tmp_path / "source"
source.mkdir()
(source / "agent.py").write_text("# Agent source\n")
if requirements is not None:
(source / "requirements.txt").write_bytes(
requirements.encode("utf-8")
if isinstance(requirements, str)
else requirements
)
build = tmp_path / "build"
monkeypatch.setattr(subprocess, "run", _Recorder())
monkeypatch.setattr(shutil, "rmtree", lambda *args, **kwargs: None)

cli_deploy.run(
agent_folder=str(source),
provider=provider,
project="project",
region="us-central1",
service_name="service",
app_name="agent",
temp_folder=str(build),
port=8080,
trace_to_cloud=False,
otel_to_cloud=False,
with_ui=False,
log_level="info",
verbosity="info",
adk_version="1.3.0",
)

content = (build / "Dockerfile").read_text()
source_copy = (
'COPY --chown=myuser:myuser "agents/agent/" "/app/agents/agent/"'
)
requirements_copy = (
'COPY --chown=myuser:myuser "agents/agent/requirements.txt"'
' "/app/agents/agent/requirements.txt"'
)
install = 'RUN ["pip", "install", "-r", "/app/agents/agent/requirements.txt"]'
assert content.count(source_copy) == 1
if requirements is None:
assert requirements_copy not in content
assert install not in content
elif cacheable:
assert content.index(requirements_copy) < content.index(install)
assert content.index(install) < content.index(source_copy)
else:
assert requirements_copy not in content
assert content.index(source_copy) < content.index(install)


# Helpers
def _parse_cmd_argv(dockerfile_content: str) -> List[str]:
"""Extracts and parses the JSON array from a Dockerfile CMD instruction."""
Expand Down