Skip to content

Commit

Permalink
Implemement .devpodignore (#1337)
Browse files Browse the repository at this point in the history
* Implemement .devpodignore

* Don't return error for rpc
  • Loading branch information
bkneis authored Oct 31, 2024
1 parent b1a8147 commit ba509e9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
26 changes: 24 additions & 2 deletions pkg/agent/tunnelserver/tunnelserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/loft-sh/devpod/pkg/agent/tunnel"
Expand All @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/agent/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit ba509e9

Please sign in to comment.