Skip to content

Commit

Permalink
Support wildcards in Dockerfiles
Browse files Browse the repository at this point in the history
Fixes 709

Signed-off-by: David Gageot <[email protected]>
  • Loading branch information
dgageot committed Jun 21, 2018
1 parent b6403b3 commit c9d8f39
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
29 changes: 28 additions & 1 deletion pkg/skaffold/docker/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,35 @@ func readDockerfile(workspace, dockerfilePath string) ([]string, error) {

dispatchInstructions(res)

expandedPaths := make(map[string]bool)
for p := range depMap {
path := filepath.Join(workspace, p)

if _, err := os.Stat(path); err == nil {
expandedPaths[p] = true
continue
}

files, err := filepath.Glob(path)
if err != nil {
return nil, errors.Wrap(err, "glob")
}
if files == nil {
return nil, fmt.Errorf("file pattern must match at least one file %s", path)
}

for _, f := range files {
rel, err := filepath.Rel(workspace, f)
if err != nil {
return nil, fmt.Errorf("getting relative path of %s", f)
}

expandedPaths[rel] = true
}
}

var deps []string
for dep := range depMap {
for dep := range expandedPaths {
deps = append(deps, dep)
}
logrus.Infof("Found dependencies for dockerfile %s", deps)
Expand Down
36 changes: 30 additions & 6 deletions pkg/skaffold/docker/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ COPY test.conf /etc/test2
CMD nginx
`

const wildcards = `
FROM nginx
ADD *.go /tmp
`

const wildcardsMatchesNone = `
FROM nginx
ADD *.none /tmp
`

const multiStageDockerfile = `
FROM golang:1.9.2
WORKDIR /go/src/github.com/r2d4/leeroy/
Expand Down Expand Up @@ -106,9 +116,11 @@ var ImageConfigs = map[string]*v1.ConfigFile{
},
},
},
"ubuntu:14.04": {Config: v1.Config{}},
"nginx": {Config: v1.Config{}},
"busybox": {Config: v1.Config{}},
"golang:1.9.2": {Config: v1.Config{}},
"gcr.io/distroless/base": {Config: v1.Config{}},
"ubuntu:14.04": {Config: v1.Config{}},
"nginx": {Config: v1.Config{}},
"busybox": {Config: v1.Config{}},
"oneport": {
Config: v1.Config{
ExposedPorts: map[string]struct{}{
Expand Down Expand Up @@ -154,6 +166,18 @@ func TestGetDependencies(t *testing.T) {
workspace: "docker",
expected: []string{"Dockerfile", "nginx.conf"},
},
{
description: "wildcards",
dockerfile: wildcards,
workspace: ".",
expected: []string{"Dockerfile", "server.go", "worker.go"},
},
{
description: "wildcards matches none",
dockerfile: wildcardsMatchesNone,
workspace: ".",
shouldErr: true,
},
{
description: "bad read",
badReader: true,
Expand Down Expand Up @@ -212,10 +236,10 @@ func TestGetDependencies(t *testing.T) {
},
{
description: "dockerignore with context in parent directory",
dockerfile: contextDockerfile,
dockerfile: copyDirectory,
workspace: "docker/..",
ignore: "bar\ndocker/*",
expected: []string{"Dockerfile", "file", "server.go", "test.conf", "worker.go"},
ignore: "bar\ndocker/*\n*.go",
expected: []string{"Dockerfile", "file", "test.conf"},
},
{
description: "onbuild test",
Expand Down

0 comments on commit c9d8f39

Please sign in to comment.