-
Notifications
You must be signed in to change notification settings - Fork 686
build: apply global .dockerignore during context sync #4114
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
Open
crazy-max
wants to merge
2
commits into
docker:master
Choose a base branch
from
crazy-max:global-dockerignore
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+270
−44
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -50,6 +50,7 @@ import ( | |
| "github.com/moby/buildkit/util/archiveutil" | ||
| "github.com/moby/buildkit/util/entitlements" | ||
| "github.com/moby/buildkit/util/gitutil" | ||
| "github.com/moby/patternmatcher/ignorefile" | ||
| "github.com/opencontainers/go-digest" | ||
| ocispecs "github.com/opencontainers/image-spec/specs-go/v1" | ||
| "github.com/pkg/errors" | ||
|
|
@@ -577,7 +578,7 @@ func toSolveOpt(ctx context.Context, np *noderesolver.ResolvedNode, multiDriver | |
| so.FrontendAttrs["label:"+k] = v | ||
| } | ||
|
|
||
| releaseLoad, err := loadInputs(ctx, nodeDriver, &opt.Inputs, pw, &so) | ||
| releaseLoad, err := loadInputs(ctx, nodeDriver, &opt.Inputs, cfg, pw, &so) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
|
|
@@ -897,7 +898,7 @@ func policyEnvFilename(inp Inputs) string { | |
| return "Dockerfile" | ||
| } | ||
|
|
||
| func loadInputs(ctx context.Context, d *driver.DriverHandle, inp *Inputs, pw progress.Writer, target *client.SolveOpt) (func(), error) { | ||
| func loadInputs(ctx context.Context, d *driver.DriverHandle, inp *Inputs, cfg *confutil.Config, pw progress.Writer, target *client.SolveOpt) (func(), error) { | ||
| if inp.ContextPath == "" { | ||
| return nil, errors.New("please specify build context (e.g. \".\" for the current directory)") | ||
| } | ||
|
|
@@ -959,7 +960,7 @@ func loadInputs(ctx context.Context, d *driver.DriverHandle, inp *Inputs, pw pro | |
| } | ||
| } | ||
| case osutil.IsLocalDir(inp.ContextPath): | ||
| if err := setLocalMount("context", inp.ContextPath, target); err != nil { | ||
| if err := setLocalContextMount("context", inp.ContextPath, cfg, target); err != nil { | ||
| return nil, err | ||
| } | ||
| contextDir = inp.ContextPath | ||
|
|
@@ -1139,7 +1140,7 @@ func loadInputs(ctx context.Context, d *driver.DriverHandle, inp *Inputs, pw pro | |
| if k == "context" || k == "dockerfile" { | ||
| localName = "_" + k // underscore to avoid collisions | ||
| } | ||
| if err := setLocalMount(localName, v.Path, target); err != nil { | ||
| if err := setLocalContextMount(localName, v.Path, cfg, target); err != nil { | ||
| return nil, err | ||
| } | ||
| target.FrontendAttrs["context:"+k] = "local:" + localName | ||
|
|
@@ -1231,6 +1232,42 @@ func setLocalMount(name, dir string, so *client.SolveOpt) error { | |
| return nil | ||
| } | ||
|
|
||
| func setLocalContextMount(name, dir string, cfg *confutil.Config, so *client.SolveOpt) error { | ||
| if err := setLocalMount(name, dir, so); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| filename := filepath.Join(cfg.Dir(), ".dockerignore") | ||
| f, err := os.Open(filename) | ||
| if errors.Is(err, os.ErrNotExist) { | ||
| return nil | ||
| } | ||
| if err != nil { | ||
| return errors.Wrapf(err, "failed to open global ignore file %s", filename) | ||
| } | ||
| defer f.Close() | ||
|
|
||
| patterns, err := ignorefile.ReadAll(f) | ||
| if err != nil { | ||
| return errors.Wrapf(err, "failed to read global ignore file %s", filename) | ||
| } | ||
| if len(patterns) == 0 { | ||
| return nil | ||
| } | ||
|
Comment on lines
+1250
to
+1256
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can take advantage of that here by doing |
||
| // BuildKit reads the context's ignore file through this mount before | ||
| // applying its repository-specific rules. | ||
| mount := so.LocalMounts[name].(*fsMount) | ||
| filtered, err := fsutil.NewFilterFS(mount.FS, &fsutil.FilterOpt{ | ||
| ExcludePatterns: append(slices.Clone(patterns), "!.dockerignore"), | ||
| }) | ||
| if err != nil { | ||
| return errors.Wrapf(err, "failed to parse global ignore file %s", filename) | ||
| } | ||
| mount.FS = filtered | ||
| mount.patterns = patterns | ||
| return nil | ||
| } | ||
|
|
||
| func createTempDockerfile(r io.Reader, multiReader *SyncMultiReader) (string, error) { | ||
| dir, err := os.MkdirTemp("", "dockerfile") | ||
| if err != nil { | ||
|
|
@@ -1341,7 +1378,8 @@ func handleLowercaseDockerfile(dir, p string) string { | |
|
|
||
| type fsMount struct { | ||
| fsutil.FS | ||
| dir string | ||
| dir string | ||
| patterns []string | ||
| } | ||
|
|
||
| var _ fsutil.FS = &fsMount{} | ||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could also just set
err = niland callerrors.Wrapfeither way since that function will returnnilif it is passed anilerror.