Fix .dockerignore for build context copies in later stages (#1447)

* Extend .dockerignore integration test with copies in later stages

.dockerignore should continue to apply when copying from the build context in later stages, but it currently doesn't

* Replace excluded global with passed along FileContext struct

This new FileContext struct allows much cleaner handling of context specific file exclusions.
The global excluded file state is no longer needed.

Additionally this also fixes the issue where excluded files aren't being applied for build context copies in later build stages.
This commit is contained in:
Tinjo Schöni
2020-10-08 12:47:14 -07:00
committed by GitHub
parent 0ef3a6b525
commit 5f4e2f1366
14 changed files with 150 additions and 126 deletions
+8 -8
View File
@@ -85,7 +85,7 @@ func ResolveEnvironmentReplacement(value string, envs []string, isFilepath bool)
return fp, nil
}
func ResolveEnvAndWildcards(sd instructions.SourcesAndDest, buildcontext string, envs []string) ([]string, string, error) {
func ResolveEnvAndWildcards(sd instructions.SourcesAndDest, fileContext FileContext, envs []string) ([]string, string, error) {
// First, resolve any environment replacement
resolvedEnvs, err := ResolveEnvironmentReplacementList(sd, envs, true)
if err != nil {
@@ -96,11 +96,11 @@ func ResolveEnvAndWildcards(sd instructions.SourcesAndDest, buildcontext string,
}
dest := resolvedEnvs[len(resolvedEnvs)-1]
// Resolve wildcards and get a list of resolved sources
srcs, err := ResolveSources(resolvedEnvs[0:len(resolvedEnvs)-1], buildcontext)
srcs, err := ResolveSources(resolvedEnvs[0:len(resolvedEnvs)-1], fileContext.Root)
if err != nil {
return nil, "", errors.Wrap(err, "failed to resolve sources")
}
err = IsSrcsValid(sd, srcs, buildcontext)
err = IsSrcsValid(sd, srcs, fileContext)
return srcs, dest, err
}
@@ -220,14 +220,14 @@ func URLDestinationFilepath(rawurl, dest, cwd string, envs []string) (string, er
return destPath, nil
}
func IsSrcsValid(srcsAndDest instructions.SourcesAndDest, resolvedSources []string, root string) error {
func IsSrcsValid(srcsAndDest instructions.SourcesAndDest, resolvedSources []string, fileContext FileContext) error {
srcs := srcsAndDest[:len(srcsAndDest)-1]
dest := srcsAndDest[len(srcsAndDest)-1]
if !ContainsWildcards(srcs) {
totalSrcs := 0
for _, src := range srcs {
if ExcludeFile(src, root) {
if fileContext.ExcludesFile(src) {
continue
}
totalSrcs++
@@ -242,7 +242,7 @@ func IsSrcsValid(srcsAndDest instructions.SourcesAndDest, resolvedSources []stri
if IsSrcRemoteFileURL(resolvedSources[0]) {
return nil
}
path := filepath.Join(root, resolvedSources[0])
path := filepath.Join(fileContext.Root, resolvedSources[0])
fi, err := os.Lstat(path)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to get fileinfo for %v", path))
@@ -259,12 +259,12 @@ func IsSrcsValid(srcsAndDest instructions.SourcesAndDest, resolvedSources []stri
continue
}
src = filepath.Clean(src)
files, err := RelativeFiles(src, root)
files, err := RelativeFiles(src, fileContext.Root)
if err != nil {
return errors.Wrap(err, "failed to get relative files")
}
for _, file := range files {
if ExcludeFile(file, root) {
if fileContext.ExcludesFile(file) {
continue
}
totalFiles++