diff --git a/pkg/agent/tunnelserver/tunnelserver.go b/pkg/agent/tunnelserver/tunnelserver.go index b4dafd9ee..be6f69b0f 100644 --- a/pkg/agent/tunnelserver/tunnelserver.go +++ b/pkg/agent/tunnelserver/tunnelserver.go @@ -8,6 +8,7 @@ import ( "fmt" "io" "os" + "path/filepath" "strings" "github.com/loft-sh/devpod/pkg/agent/tunnel" @@ -23,6 +24,7 @@ import ( provider2 "github.com/loft-sh/devpod/pkg/provider" "github.com/loft-sh/devpod/pkg/stdio" "github.com/loft-sh/log" + "github.com/moby/buildkit/frontend/dockerfile/dockerignore" perrors "github.com/pkg/errors" "google.golang.org/grpc" "google.golang.org/grpc/reflection" @@ -383,8 +385,18 @@ func (t *tunnelServer) StreamWorkspace(message *tunnel.Empty, stream tunnel.Tunn return fmt.Errorf("workspace is nil") } + // Get .devpodignore files to exclude + excludes := []string{} + f, err := os.Open(filepath.Join(t.workspace.Source.LocalFolder, ".devpodignore")) + if err == nil { + excludes, err = dockerignore.ReadAll(f) + if err != nil { + t.log.Warnf("Error reading .devpodignore file: %v", err) + } + } + buf := bufio.NewWriterSize(NewStreamWriter(stream, t.log), 10*1024) - err := extract.WriteTar(buf, t.workspace.Source.LocalFolder, false) + err = extract.WriteTarExclude(buf, t.workspace.Source.LocalFolder, false, excludes) if err != nil { return err } @@ -405,8 +417,18 @@ func (t *tunnelServer) StreamMount(message *tunnel.StreamMountRequest, stream tu return fmt.Errorf("mount %s is not allowed to download", message.Mount) } + // Get .devpodignore files to exclude + excludes := []string{} + f, err := os.Open(filepath.Join(t.workspace.Source.LocalFolder, ".devpodignore")) + if err == nil { + excludes, err = dockerignore.ReadAll(f) + if err != nil { + t.log.Warnf("Error reading .devpodignore file: %v", err) + } + } + buf := bufio.NewWriterSize(NewStreamWriter(stream, t.log), 10*1024) - err := extract.WriteTar(buf, mount.Source, false) + err = extract.WriteTarExclude(buf, mount.Source, false, excludes) if err != nil { return err } diff --git a/pkg/agent/workspace.go b/pkg/agent/workspace.go index 2f388bceb..95fffa75d 100644 --- a/pkg/agent/workspace.go +++ b/pkg/agent/workspace.go @@ -17,6 +17,7 @@ import ( provider2 "github.com/loft-sh/devpod/pkg/provider" "github.com/loft-sh/log" "github.com/mitchellh/go-homedir" + "github.com/moby/buildkit/frontend/dockerfile/dockerignore" ) var extraSearchLocations = []string{"/home/devpod/.devpod/agent", "/opt/devpod/agent", "/var/lib/devpod/agent", "/var/devpod/agent"} @@ -303,6 +304,22 @@ func CloneRepositoryForWorkspace( log.Done("Successfully cloned repository") + // Get .devpodignore files to exclude + f, err := os.Open(filepath.Join(workspaceDir, ".devpodignore")) + if err != nil { + return nil + } + excludes, err := dockerignore.ReadAll(f) + if err != nil { + log.Warn(".devpodignore file is invalid : ", err) + return nil + } + // Remove files from workspace content folder + for _, exclude := range excludes { + os.RemoveAll(filepath.Join(workspaceDir, exclude)) + } + log.Debug("Ignore files from .devpodignore ", excludes) + return nil }