diff --git a/cmd/executor/cmd/root.go b/cmd/executor/cmd/root.go index 7f0339c5df..ea2634d1e6 100644 --- a/cmd/executor/cmd/root.go +++ b/cmd/executor/cmd/root.go @@ -395,7 +395,12 @@ func copyDockerfile() error { return errors.Wrap(err, "copying dockerfile") } dockerignorePath := opts.DockerfilePath + ".dockerignore" - if util.FilepathExists(dockerignorePath) { + containerignorePath := opts.DockerfilePath + ".containerignore" + if util.FilepathExists(containerignorePath) { + if _, err := util.CopyFile(containerignorePath, config.DockerfilePath+".containerignore", util.FileContext{}, util.DoNotChangeUID, util.DoNotChangeGID, fs.FileMode(0o600), true); err != nil { + return errors.Wrap(err, "copying Dockerfile.containerignore") + } + } else if util.FilepathExists(dockerignorePath) { if _, err := util.CopyFile(dockerignorePath, config.DockerfilePath+".dockerignore", util.FileContext{}, util.DoNotChangeUID, util.DoNotChangeGID, fs.FileMode(0o600), true); err != nil { return errors.Wrap(err, "copying Dockerfile.dockerignore") } diff --git a/pkg/util/fs_util.go b/pkg/util/fs_util.go index c8d5a613aa..6737dc2086 100644 --- a/pkg/util/fs_util.go +++ b/pkg/util/fs_util.go @@ -773,19 +773,32 @@ func NewFileContextFromDockerfile(dockerfilePath, buildcontext string) (FileCont return fileContext, nil } -// getExcludedFiles returns a list of files to exclude from the .dockerignore -func getExcludedFiles(dockerfilePath, buildcontext string) ([]string, error) { - path := dockerfilePath + ".dockerignore" - if !FilepathExists(path) { +func getIgnoreFilePath(dockerfilePath, buildcontext string) (string, error) { + var path string + + dockerignorePath := dockerfilePath + ".dockerignore" + containerignorePath := dockerfilePath + ".containerignore" + if FilepathExists(containerignorePath) { + path = containerignorePath + } else if FilepathExists(filepath.Join(buildcontext, ".containerignore")) { + path = filepath.Join(buildcontext, ".containerignore") + } else if FilepathExists(dockerignorePath) { + path = dockerignorePath + } else if FilepathExists(filepath.Join(buildcontext, ".dockerignore")) { path = filepath.Join(buildcontext, ".dockerignore") + } else { + return "", errors.New("Ignore file not found") } - if !FilepathExists(path) { - return nil, nil - } - logrus.Infof("Using dockerignore file: %v", path) + return path, nil +} + +// getExcludedFiles returns a list of files to exclude from the .containerignore or .dockerignore +func getExcludedFiles(dockerfilePath, buildcontext string) ([]string, error) { + path, _ := getIgnoreFilePath(dockerfilePath, buildcontext) + logrus.Infof("Using ignorefile: %v", path) contents, err := os.ReadFile(path) if err != nil { - return nil, errors.Wrap(err, "parsing .dockerignore") + return nil, nil } reader := bytes.NewBuffer(contents) return dockerignore.ReadAll(reader) diff --git a/pkg/util/fs_util_test.go b/pkg/util/fs_util_test.go index 9bd44c835c..6522bda169 100644 --- a/pkg/util/fs_util_test.go +++ b/pkg/util/fs_util_test.go @@ -22,6 +22,7 @@ import ( "fmt" "io" "io/fs" + "io/ioutil" "os" "path/filepath" "reflect" @@ -920,6 +921,84 @@ func Test_childDirInSkiplist(t *testing.T) { } } +func Test_getIgnoreFilePath(t *testing.T) { + dockerfilePath := filepath.Join(os.TempDir(), "Dockerfile") + buildcontext := filepath.Join(os.TempDir(), "buildcontext") + + err := os.MkdirAll(buildcontext, 0755) + if err != nil { + t.Errorf("'%v' folder cannot be created", buildcontext) + } + + cleanup := func() { + os.Remove(dockerfilePath + ".containerignore") + os.Remove(dockerfilePath + ".dockerignore") + os.Remove(filepath.Join(buildcontext, ".containerignore")) + os.Remove(filepath.Join(buildcontext, ".dockerignore")) + } + + t.Run(".containerignore exists next to Dockerfile", func(t *testing.T) { + err := ioutil.WriteFile(dockerfilePath+".containerignore", []byte("foo"), 0644) + if err != nil { + t.Errorf("cannot create '%v'.containerignore", dockerfilePath) + } + path, err := getIgnoreFilePath(dockerfilePath, buildcontext) + expectedPath := dockerfilePath + ".containerignore" + if err != nil || path != expectedPath { + t.Errorf("expected '%v', got '%v', error: '%v'", expectedPath, path, err) + } + + cleanup() + }) + + t.Run(".containerignore exists in the buildcontext", func(t *testing.T) { + err := ioutil.WriteFile(filepath.Join(buildcontext, ".containerignore"), []byte("foo"), 0644) + if err != nil { + t.Errorf("cannot create '%v'.containerignore", buildcontext) + } + path, err := getIgnoreFilePath(dockerfilePath, buildcontext) + expectedPath := filepath.Join(buildcontext, ".containerignore") + if err != nil || path != expectedPath { + t.Errorf("expected '%v', got '%v', error: '%v'", expectedPath, path, err) + } + + cleanup() + }) + + t.Run(".dockerignore exists next to Dockerfile", func(t *testing.T) { + err := ioutil.WriteFile(dockerfilePath+".dockerignore", []byte("foo"), 0644) + if err != nil { + t.Errorf("cannot create '%v'.dockerignore", dockerfilePath) + } + path, err := getIgnoreFilePath(dockerfilePath, buildcontext) + expectedPath := dockerfilePath + ".dockerignore" + if err != nil || path != expectedPath { + t.Errorf("expected '%v', got '%v', error: '%v'", expectedPath, path, err) + } + + cleanup() + }) + + t.Run(".dockerignore exists in the buildcontext", func(t *testing.T) { + err := ioutil.WriteFile(filepath.Join(buildcontext, ".dockerignore"), []byte("foo"), 0644) + if err != nil { + t.Errorf("cannot create '%v'.dockerignore", buildcontext) + } + path, err := getIgnoreFilePath(dockerfilePath, buildcontext) + expectedPath := filepath.Join(buildcontext, ".dockerignore") + if err != nil || path != expectedPath { + t.Errorf("expected '%v', got '%v', error: '%v'", expectedPath, path, err) + } + + cleanup() + }) + + err = os.RemoveAll(buildcontext) + if err != nil { + t.Errorf("cannot clean '%v' folder", buildcontext) + } +} + func Test_correctDockerignoreFileIsUsed(t *testing.T) { type args struct { dockerfilepath string