Skip to content

Commit ee79e71

Browse files
kotaitosharanrk
authored andcommitted
fix(cli): respect ignore files in adk deploy commands
Merge google#4187 **Please ensure you have read the [contribution guide](https://github.com/google/adk-python/blob/main/CONTRIBUTING.md) before creating a pull request.** ### Link to Issue or Description of Change **1. Link to an existing issue (if applicable):** - Closes: google#4183 **Problem:** The `adk deploy` commands (`cloud_run`, `agent_engine`, `gke`) were not properly respecting `.gitignore`, `.gcloudignore`, or `.ae_ignore` files. This caused all files in the source directory, including large or sensitive ones like `venv/`, `.git/`, and `.env`, to be copied to the temporary staging directory and subsequently uploaded to hosted environments. **Solution:** - Implemented a unified `_get_ignore_patterns_func` helper in `src/google/adk/cli/cli_deploy.py` that reads and combines patterns from `.gitignore`, `.gcloudignore`, and `.ae_ignore`. - Updated `to_cloud_run`, `to_agent_engine`, and `to_gke` to use this helper as an ignore filter in `shutil.copytree`. - This ensures that only the files intended by the user are staged and deployed. ### Testing Plan **Unit Tests:** - [x] I have added or updated unit tests for my change. - [x] All unit tests pass locally. Summary of `pytest` results: ```text tests/unittests/cli/utils/test_cli_deploy_ignore.py ... [100%] 3 passed in 2.20s ``` **Manual End-to-End (E2E) Tests:** Verified the fix by following these steps: 1. **Setup:** Created a dummy agent directory with a `.gitignore` file. ```bash mkdir -p verify_agent touch verify_agent/agent.py verify_agent/__init__.py touch verify_agent/ignored_file.txt echo "ignored_file.txt" > verify_agent/.gitignore ``` 2. **Execution:** Ran the deploy command pointing to a local temp folder. ```bash adk deploy cloud_run --temp_folder ./debug_staged_out ./verify_agent ``` 3. **Verification:** Temporarily disabled the `shutil.rmtree` cleanup in code to inspect `./debug_staged_out`. 4. **Result:** Confirmed that `agent.py` and `.gitignore` were present, but `ignored_file.txt` was correctly excluded from the staging area. ### Checklist - [x] I have read the [CONTRIBUTING.md](https://github.com/google/adk-python/blob/main/CONTRIBUTING.md) document. - [x] I have performed a self-review of my own code. - [x] I have commented my code, particularly in hard-to-understand areas. - [x] I have added tests that prove my fix is effective or that my feature works. - [x] New and existing unit tests pass locally with my changes. - [x] I have manually tested my changes end-to-end. - [x] Any dependent changes have been merged and published in downstream modules. Co-authored-by: Haran Rajkumar <haranrk@google.com> COPYBARA_INTEGRATE_REVIEW=google#4187 from kotaitos:fix/issue-4183-ignore-files 15f6d62 PiperOrigin-RevId: 934458832
1 parent a012bb7 commit ee79e71

2 files changed

Lines changed: 241 additions & 10 deletions

File tree

src/google/adk/cli/cli_deploy.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,34 @@ def _get_service_option_by_adk_version(
603603
return ' '.join(options)
604604

605605

606+
def _get_ignore_patterns_func(agent_folder: str):
607+
"""Returns a shutil.ignore_patterns function with combined patterns from .gitignore, .gcloudignore and .ae_ignore."""
608+
patterns = set()
609+
610+
for filename in ['.gitignore', '.gcloudignore', '.ae_ignore']:
611+
filepath = os.path.join(agent_folder, filename)
612+
if os.path.exists(filepath):
613+
click.echo(f'Reading ignore patterns from {filename}...')
614+
try:
615+
with open(filepath, 'r') as f:
616+
for line in f:
617+
line = line.strip()
618+
if line and not line.startswith('#'):
619+
# If it ends with /, remove it for fnmatch compatibility
620+
if line.endswith('/'):
621+
line = line[:-1]
622+
# Strip leading / from root-anchored patterns; shutil.ignore_patterns
623+
# matches basenames via fnmatch, so '/venv' would match nothing.
624+
if line.startswith('/'):
625+
line = line[1:]
626+
if line:
627+
patterns.add(line)
628+
except Exception as e:
629+
click.secho(f'Warning: Failed to read {filename}: {e}', fg='yellow')
630+
631+
return shutil.ignore_patterns(*patterns)
632+
633+
606634
def to_cloud_run(
607635
*,
608636
agent_folder: str,
@@ -679,7 +707,8 @@ def to_cloud_run(
679707
# copy agent source code
680708
click.echo('Copying agent source code...')
681709
agent_src_path = os.path.join(temp_folder, 'agents', app_name)
682-
shutil.copytree(agent_folder, agent_src_path)
710+
ignore_func = _get_ignore_patterns_func(agent_folder)
711+
shutil.copytree(agent_folder, agent_src_path, ignore=ignore_func)
683712
requirements_txt_path = os.path.join(agent_src_path, 'requirements.txt')
684713
install_agent_deps = (
685714
f'RUN pip install -r "/app/agents/{app_name}/requirements.txt"'
@@ -943,18 +972,12 @@ def to_agent_engine(
943972
shutil.rmtree(temp_folder_path)
944973

945974
try:
946-
ignore_patterns = None
947-
ae_ignore_path = os.path.join(agent_folder, '.ae_ignore')
948-
if os.path.exists(ae_ignore_path):
949-
click.echo(f'Ignoring files matching the patterns in {ae_ignore_path}')
950-
with open(ae_ignore_path, 'r') as f:
951-
patterns = [pattern.strip() for pattern in f.readlines()]
952-
ignore_patterns = shutil.ignore_patterns(*patterns)
975+
ignore_func = _get_ignore_patterns_func(agent_folder)
953976
click.echo('Copying agent source code...')
954977
shutil.copytree(
955978
agent_folder,
956979
agent_src_path,
957-
ignore=ignore_patterns,
980+
ignore=ignore_func,
958981
dirs_exist_ok=True,
959982
)
960983
os.chdir(temp_folder_path)
@@ -1302,7 +1325,8 @@ def to_gke(
13021325
# copy agent source code
13031326
click.echo(' - Copying agent source code...')
13041327
agent_src_path = os.path.join(temp_folder, 'agents', app_name)
1305-
shutil.copytree(agent_folder, agent_src_path)
1328+
ignore_func = _get_ignore_patterns_func(agent_folder)
1329+
shutil.copytree(agent_folder, agent_src_path, ignore=ignore_func)
13061330
requirements_txt_path = os.path.join(agent_src_path, 'requirements.txt')
13071331
install_agent_deps = (
13081332
f'RUN pip install -r "/app/agents/{app_name}/requirements.txt"'
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Tests for ignore file support in cli_deploy."""
16+
17+
from __future__ import annotations
18+
19+
import os
20+
from pathlib import Path
21+
import shutil
22+
import subprocess
23+
from unittest import mock
24+
25+
import click
26+
import pytest
27+
28+
import src.google.adk.cli.cli_deploy as cli_deploy
29+
30+
31+
@pytest.fixture(autouse=True)
32+
def _mute_click(monkeypatch: pytest.MonkeyPatch) -> None:
33+
"""Suppress click.echo to keep test output clean."""
34+
monkeypatch.setattr(click, "echo", lambda *_a, **_k: None)
35+
monkeypatch.setattr(click, "secho", lambda *_a, **_k: None)
36+
37+
38+
def test_to_cloud_run_respects_ignore_files(
39+
monkeypatch: pytest.MonkeyPatch,
40+
tmp_path: Path,
41+
) -> None:
42+
"""Test that to_cloud_run respects .gitignore and .gcloudignore."""
43+
agent_dir = tmp_path / "agent"
44+
agent_dir.mkdir()
45+
(agent_dir / "agent.py").write_text("# agent")
46+
(agent_dir / "__init__.py").write_text("")
47+
(agent_dir / "ignored_by_git.txt").write_text("ignored")
48+
(agent_dir / "ignored_by_gcloud.txt").write_text("ignored")
49+
(agent_dir / "ignored_rooted.txt").write_text("ignored")
50+
(agent_dir / "not_ignored.txt").write_text("keep")
51+
52+
# Use a root-anchored pattern (leading slash) to ensure it is honored.
53+
(agent_dir / ".gitignore").write_text(
54+
"ignored_by_git.txt\n/ignored_rooted.txt\n"
55+
)
56+
(agent_dir / ".gcloudignore").write_text("ignored_by_gcloud.txt\n")
57+
58+
temp_deploy_dir = tmp_path / "temp_deploy"
59+
60+
# Mock subprocess.run to avoid actual gcloud call
61+
monkeypatch.setattr(subprocess, "run", mock.Mock())
62+
# Mock shutil.rmtree to keep the temp folder for verification
63+
monkeypatch.setattr(
64+
shutil,
65+
"rmtree",
66+
lambda path, **kwargs: None
67+
if "temp_deploy" in str(path)
68+
else shutil.rmtree(path, **kwargs),
69+
)
70+
71+
cli_deploy.to_cloud_run(
72+
agent_folder=str(agent_dir),
73+
project="proj",
74+
region="us-central1",
75+
service_name="svc",
76+
app_name="app",
77+
temp_folder=str(temp_deploy_dir),
78+
port=8080,
79+
trace_to_cloud=False,
80+
otel_to_cloud=False,
81+
with_ui=False,
82+
log_level="info",
83+
verbosity="info",
84+
adk_version="1.0.0",
85+
)
86+
87+
agent_src_path = temp_deploy_dir / "agents" / "app"
88+
89+
assert (agent_src_path / "agent.py").exists()
90+
assert (agent_src_path / "not_ignored.txt").exists()
91+
92+
# These should be ignored
93+
assert not (
94+
agent_src_path / "ignored_by_git.txt"
95+
).exists(), "Should respect .gitignore"
96+
assert not (
97+
agent_src_path / "ignored_by_gcloud.txt"
98+
).exists(), "Should respect .gcloudignore"
99+
assert not (
100+
agent_src_path / "ignored_rooted.txt"
101+
).exists(), "Should respect root-anchored (leading slash) patterns"
102+
103+
104+
def test_to_agent_engine_respects_multiple_ignore_files(
105+
monkeypatch: pytest.MonkeyPatch,
106+
tmp_path: Path,
107+
) -> None:
108+
"""Test that to_agent_engine respects .gitignore, .gcloudignore and .ae_ignore."""
109+
# We need to be in the project dir for to_agent_engine
110+
project_dir = tmp_path / "project"
111+
project_dir.mkdir()
112+
monkeypatch.chdir(project_dir)
113+
114+
agent_dir = project_dir / "my_agent"
115+
agent_dir.mkdir()
116+
(agent_dir / "agent.py").write_text("root_agent = None")
117+
(agent_dir / "__init__.py").write_text("from . import agent")
118+
(agent_dir / "ignored_by_git.txt").write_text("ignored")
119+
(agent_dir / "ignored_by_ae.txt").write_text("ignored")
120+
121+
(agent_dir / ".gitignore").write_text("ignored_by_git.txt\n")
122+
(agent_dir / ".ae_ignore").write_text("ignored_by_ae.txt\n")
123+
124+
# Mock vertexai.Client and other things to avoid network/complex setup
125+
monkeypatch.setattr("vertexai.Client", mock.Mock())
126+
# Mock shutil.rmtree to keep the temp folder for verification
127+
original_rmtree = shutil.rmtree
128+
129+
def mock_rmtree(path, **kwargs):
130+
if "_tmp" in str(path):
131+
return None
132+
return original_rmtree(path, **kwargs)
133+
134+
monkeypatch.setattr(shutil, "rmtree", mock_rmtree)
135+
136+
cli_deploy.to_agent_engine(
137+
agent_folder=str(agent_dir),
138+
staging_bucket="gs://test",
139+
adk_app="adk_app",
140+
)
141+
142+
# Find the temp folder created by to_agent_engine
143+
temp_folders = [
144+
d for d in project_dir.iterdir() if d.is_dir() and "_tmp" in d.name
145+
]
146+
assert len(temp_folders) == 1
147+
agent_src_path = temp_folders[0]
148+
149+
copied_agent_dir = agent_src_path / "agents" / "my_agent"
150+
assert (copied_agent_dir / "agent.py").exists()
151+
assert not (
152+
copied_agent_dir / "ignored_by_git.txt"
153+
).exists(), "Should respect .gitignore"
154+
assert not (
155+
copied_agent_dir / "ignored_by_ae.txt"
156+
).exists(), "Should respect .ae_ignore"
157+
158+
159+
def test_to_gke_respects_ignore_files(
160+
monkeypatch: pytest.MonkeyPatch,
161+
tmp_path: Path,
162+
) -> None:
163+
"""Test that to_gke respects ignore files."""
164+
agent_dir = tmp_path / "agent"
165+
agent_dir.mkdir()
166+
(agent_dir / "agent.py").write_text("# agent")
167+
(agent_dir / "__init__.py").write_text("")
168+
(agent_dir / "ignored.txt").write_text("ignored")
169+
(agent_dir / ".gitignore").write_text("ignored.txt\n")
170+
171+
temp_deploy_dir = tmp_path / "temp_deploy"
172+
173+
# Mock subprocess.run to avoid actual gcloud call
174+
mock_run = mock.Mock()
175+
mock_run.return_value.stdout = "deployment created"
176+
monkeypatch.setattr(subprocess, "run", mock_run)
177+
# Mock shutil.rmtree to keep the temp folder for verification
178+
monkeypatch.setattr(
179+
shutil,
180+
"rmtree",
181+
lambda path, **kwargs: None
182+
if "temp_deploy" in str(path)
183+
else shutil.rmtree(path, **kwargs),
184+
)
185+
186+
cli_deploy.to_gke(
187+
agent_folder=str(agent_dir),
188+
project="proj",
189+
region="us-central1",
190+
cluster_name="cluster",
191+
service_name="svc",
192+
app_name="app",
193+
temp_folder=str(temp_deploy_dir),
194+
port=8080,
195+
trace_to_cloud=False,
196+
otel_to_cloud=False,
197+
with_ui=False,
198+
log_level="info",
199+
adk_version="1.0.0",
200+
)
201+
202+
agent_src_path = temp_deploy_dir / "agents" / "app"
203+
204+
assert (agent_src_path / "agent.py").exists()
205+
assert not (
206+
agent_src_path / "ignored.txt"
207+
).exists(), "Should respect .gitignore"

0 commit comments

Comments
 (0)