diff --git a/integration/dockerfiles/Dockerfile_test_dockerignore_relative b/integration/dockerfiles/Dockerfile_test_dockerignore_relative new file mode 100644 index 000000000..bc9426805 --- /dev/null +++ b/integration/dockerfiles/Dockerfile_test_dockerignore_relative @@ -0,0 +1,5 @@ +# This dockerfile makes sure Dockerfile.dockerignore is working +# If so then ignore_relative/foo should copy to /foo +# If not, then this image won't build because it will attempt to copy three files to /foo, which is a file not a directory +FROM scratch +COPY ignore_relative/* /foo diff --git a/integration/dockerfiles/Dockerfile_test_dockerignore_relative.dockerignore b/integration/dockerfiles/Dockerfile_test_dockerignore_relative.dockerignore new file mode 100644 index 000000000..7f5371bdd --- /dev/null +++ b/integration/dockerfiles/Dockerfile_test_dockerignore_relative.dockerignore @@ -0,0 +1,3 @@ +# A .dockerignore file to make sure dockerignore support works +ignore_relative/** +!ignore_relative/foo diff --git a/integration/ignore_relative/bar b/integration/ignore_relative/bar new file mode 100644 index 000000000..e69de29bb diff --git a/integration/ignore_relative/baz b/integration/ignore_relative/baz new file mode 100644 index 000000000..e69de29bb diff --git a/integration/ignore_relative/foo b/integration/ignore_relative/foo new file mode 100644 index 000000000..e69de29bb diff --git a/pkg/executor/build.go b/pkg/executor/build.go index e134e52b5..45f23cca3 100644 --- a/pkg/executor/build.go +++ b/pkg/executor/build.go @@ -427,7 +427,7 @@ func DoBuild(opts *config.KanikoOptions) (v1.Image, error) { if err != nil { return nil, err } - if err := util.GetExcludedFiles(opts.SrcContext); err != nil { + if err := util.GetExcludedFiles(opts.DockerfilePath, opts.SrcContext); err != nil { return nil, err } // Some stages may refer to other random images, not previous stages diff --git a/pkg/util/command_util_test.go b/pkg/util/command_util_test.go index 3e2342595..cd1a324a2 100644 --- a/pkg/util/command_util_test.go +++ b/pkg/util/command_util_test.go @@ -390,7 +390,7 @@ var isSrcValidTests = []struct { func Test_IsSrcsValid(t *testing.T) { for _, test := range isSrcValidTests { t.Run(test.name, func(t *testing.T) { - if err := GetExcludedFiles(buildContextPath); err != nil { + if err := GetExcludedFiles("", buildContextPath); err != nil { t.Fatalf("error getting excluded files: %v", err) } err := IsSrcsValid(test.srcsAndDest, test.resolvedSources, buildContextPath) diff --git a/pkg/util/fs_util.go b/pkg/util/fs_util.go index 338caa02a..cc9e4a4bd 100644 --- a/pkg/util/fs_util.go +++ b/pkg/util/fs_util.go @@ -554,11 +554,15 @@ func CopyFile(src, dest, buildcontext string) (bool, error) { } // GetExcludedFiles gets a list of files to exclude from the .dockerignore -func GetExcludedFiles(buildcontext string) error { - path := filepath.Join(buildcontext, ".dockerignore") +func GetExcludedFiles(dockerfilepath string, buildcontext string) error { + path := dockerfilepath + ".dockerignore" + if !FilepathExists(path) { + path = filepath.Join(buildcontext, ".dockerignore") + } if !FilepathExists(path) { return nil } + logrus.Infof("Using dockerignore file: %v", path) contents, err := ioutil.ReadFile(path) if err != nil { return errors.Wrap(err, "parsing .dockerignore") diff --git a/pkg/util/fs_util_test.go b/pkg/util/fs_util_test.go index c44908056..37cb7b034 100644 --- a/pkg/util/fs_util_test.go +++ b/pkg/util/fs_util_test.go @@ -694,3 +694,49 @@ func Test_childDirInWhitelist(t *testing.T) { }) } } + +func Test_correctDockerignoreFileIsUsed(t *testing.T) { + type args struct { + dockerfilepath string + buildcontext string + excluded string + notExcluded string + } + tests := []struct { + name string + args args + }{ + { + name: "relative dockerfile used", + args: args{ + dockerfilepath: "../../integration/dockerfiles/Dockerfile_test_dockerignore_relative", + buildcontext: "../../integration/", + excluded: "ignore_relative/bar", + notExcluded: "ignore_relative/foo", + }, + }, + { + name: "context dockerfile is used", + args: args{ + dockerfilepath: "../../integration/dockerfiles/Dockerfile_test_dockerignore", + buildcontext: "../../integration/", + excluded: "ignore/bar", + notExcluded: "ignore/foo", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + excluded = nil + if err := GetExcludedFiles(tt.args.dockerfilepath, tt.args.buildcontext); err != nil { + t.Fatal(err) + } + if excl := excludeFile(tt.args.excluded, tt.args.buildcontext); !excl { + t.Errorf("'%v' not excluded as expected", tt.args.excluded) + } + if excl := excludeFile(tt.args.notExcluded, tt.args.buildcontext); excl { + t.Errorf("'%v' excluded against expectation", tt.args.notExcluded) + } + }) + } +}