-
Notifications
You must be signed in to change notification settings - Fork 307
feat: add .agentignore support for agent code deploy packaging #8223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5d57aae
feat: add .agentignore support for agent code deploy packaging (#8170)
486716a
fix: address CI issues - cspell, gosec, go fix modernization
4b652d0
refactor: clean up security exclusions list and add nested path tests
fbc4fc4
feat: show polling attempt progress during waitForAgentActive
2b8e62c
fix: always exclude metadata files (agent.yaml, azure.yaml) from code…
54ab931
refactor: address review comments on .agentignore implementation
aab405e
fix: apply go fix modernization (slices.Contains)
9250186
refactor: remove forced exclusions, use raw string for defaults
ff7839d
chore: shorten polling progress message for better terminal display
8f14db5
refactor: address reviewer feedback on .agentignore (ctx param, exter…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ words: | |
| - westeurope | ||
| # Project terms | ||
| - ABAC | ||
| - agentignore | ||
| - ADLS | ||
| - agentserver | ||
| - aiservices | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
cli/azd/extensions/azure.ai.agents/internal/project/agentignore.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package project | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| gitignore "github.com/denormal/go-gitignore" | ||
| ) | ||
|
|
||
| const ( | ||
| agentIgnoreFileName = ".agentignore" | ||
| agentIgnoreMaxSize = 1 << 20 // 1 MB | ||
| ) | ||
|
|
||
| // defaultExclusionsContent is used as the matcher when no .agentignore file exists. | ||
| // Generated from DefaultAgentIgnoreContent() to maintain a single source of truth. | ||
| var defaultExclusionsContent = DefaultAgentIgnoreContent() | ||
|
|
||
| // utf8BOM is the byte order mark that some Windows editors prepend to UTF-8 files. | ||
| var utf8BOM = []byte{0xEF, 0xBB, 0xBF} | ||
|
|
||
| // agentIgnoreMatcher provides path matching for agent code deploy packaging. | ||
| type agentIgnoreMatcher struct { | ||
| ignore gitignore.GitIgnore // from .agentignore file or defaults | ||
| hasUserIgnore bool | ||
| } | ||
|
|
||
| // newAgentIgnoreMatcher creates a matcher by reading .agentignore from srcDir. | ||
| // If no .agentignore exists, defaults are used. | ||
| func newAgentIgnoreMatcher(ctx context.Context, srcDir string) (*agentIgnoreMatcher, error) { | ||
| _ = ctx // reserved for future cancellation support | ||
| m := &agentIgnoreMatcher{} | ||
|
|
||
| // Try to load user's .agentignore | ||
| ig, err := loadAgentIgnore(ctx, srcDir) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if ig != nil { | ||
| m.ignore = ig | ||
| m.hasUserIgnore = true | ||
| } else { | ||
| // No .agentignore file — use defaults | ||
| m.ignore = gitignore.New( | ||
| strings.NewReader(defaultExclusionsContent), | ||
| srcDir, | ||
| nil, | ||
| ) | ||
| } | ||
|
|
||
| return m, nil | ||
| } | ||
|
|
||
| // ShouldExclude returns true if the given path should be excluded from the ZIP. | ||
| // relPath is the path relative to srcDir using forward slashes. | ||
| // isDir indicates whether the path is a directory. | ||
| func (m *agentIgnoreMatcher) ShouldExclude(relPath string, isDir bool) bool { | ||
| match := m.ignore.Relative(relPath, isDir) | ||
| if match != nil && match.Ignore() { | ||
| return true | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // loadAgentIgnore reads an .agentignore file from srcDir. | ||
| // Returns nil, nil if no file exists. | ||
| func loadAgentIgnore(ctx context.Context, srcDir string) (gitignore.GitIgnore, error) { | ||
| _ = ctx // reserved for future cancellation support | ||
| path := filepath.Join(srcDir, agentIgnoreFileName) | ||
| info, err := os.Lstat(path) | ||
| if errors.Is(err, os.ErrNotExist) { | ||
| return nil, nil | ||
| } | ||
| if err != nil { | ||
| return nil, fmt.Errorf("reading %s: %w", agentIgnoreFileName, err) | ||
| } | ||
| if !info.Mode().IsRegular() { | ||
| return nil, fmt.Errorf("%s must be a regular file", agentIgnoreFileName) | ||
| } | ||
| if info.Size() > agentIgnoreMaxSize { | ||
| return nil, fmt.Errorf("%s exceeds maximum size (%d bytes)", agentIgnoreFileName, agentIgnoreMaxSize) | ||
| } | ||
|
|
||
| f, err := os.Open(path) //nolint:gosec // path is constructed from a known directory + constant filename | ||
| if err != nil { | ||
| return nil, fmt.Errorf("reading %s: %w", agentIgnoreFileName, err) | ||
| } | ||
| defer f.Close() | ||
|
|
||
| data, err := io.ReadAll(io.LimitReader(f, agentIgnoreMaxSize+1)) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("reading %s: %w", agentIgnoreFileName, err) | ||
| } | ||
| if int64(len(data)) > agentIgnoreMaxSize { | ||
| return nil, fmt.Errorf("%s exceeds maximum size (%d bytes)", agentIgnoreFileName, agentIgnoreMaxSize) | ||
| } | ||
|
|
||
| // Strip UTF-8 BOM | ||
| data = bytes.TrimPrefix(data, utf8BOM) | ||
|
|
||
| return gitignore.New(bytes.NewReader(data), srcDir, nil), nil | ||
| } | ||
|
|
||
| // DefaultAgentIgnoreContent returns the default .agentignore file content | ||
| // that should be generated during `azd ai agent init`. | ||
| func DefaultAgentIgnoreContent() string { | ||
|
trangevi marked this conversation as resolved.
|
||
| return `# Files excluded from agent code deployment packaging. | ||
| # Uses .gitignore syntax. | ||
| # Note: only the root .agentignore is read; subdirectory files are not supported. | ||
| # | ||
| # To include a file that is excluded by default, use negation: !filename | ||
|
|
||
| # azd tooling files | ||
| agent.yaml | ||
| agent.manifest.yaml | ||
| azure.yaml | ||
| .agentignore | ||
|
|
||
| # Security / secrets | ||
| .env | ||
| .env.* | ||
| .azure/ | ||
| .git/ | ||
|
|
||
| # Python | ||
| __pycache__/ | ||
| .venv/ | ||
| venv/ | ||
| *.pyc | ||
| *.pyo | ||
| .mypy_cache/ | ||
| .pytest_cache/ | ||
|
|
||
| # .NET | ||
| bin/ | ||
| obj/ | ||
| *.user | ||
| *.suo | ||
| .vs/ | ||
|
|
||
| # Node | ||
| node_modules/ | ||
|
|
||
| # Docker (not used in code deploy) | ||
| Dockerfile | ||
| .dockerignore | ||
| ` | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.