Skip to content

Commit

Permalink
Support globs in custom/buildpacks builder deps (#3878)
Browse files Browse the repository at this point in the history
Fixes #3826

Signed-off-by: David Gageot <[email protected]>
  • Loading branch information
dgageot authored Mar 30, 2020
1 parent ee9091b commit 0fb5a24
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 34 deletions.
3 changes: 2 additions & 1 deletion examples/custom/skaffold.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ build:
buildCommand: ./build.sh
dependencies:
paths:
- .
- "go.mod"
- "**.go"
3 changes: 2 additions & 1 deletion integration/examples/custom/skaffold.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ build:
buildCommand: ./build.sh
dependencies:
paths:
- .
- "go.mod"
- "**.go"
17 changes: 2 additions & 15 deletions pkg/skaffold/build/buildpacks/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,12 @@ package buildpacks

import (
"context"
"fmt"
"sort"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/list"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
)

// GetDependencies returns dependencies listed for a buildpack artifact
func GetDependencies(ctx context.Context, workspace string, a *latest.BuildpackArtifact) ([]string, error) {
files, err := docker.WalkWorkspace(workspace, a.Dependencies.Ignore, a.Dependencies.Paths)
if err != nil {
return nil, fmt.Errorf("walking workspace %q: %w", workspace, err)
}

var dependencies []string
for file := range files {
dependencies = append(dependencies, file)
}

sort.Strings(dependencies)
return dependencies, nil
return list.Files(workspace, a.Dependencies.Paths, a.Dependencies.Ignore)
}
14 changes: 11 additions & 3 deletions pkg/skaffold/build/buildpacks/dependencies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,22 @@ func TestGetDependencies(t *testing.T) {
description: "watch everything",
paths: []string{"."},
expected: []string{"bar", filepath.FromSlash("baz/file"), "foo"},
}, {
},
{
description: "watch nothing",
}, {
},
{
description: "ignore some paths",
paths: []string{"."},
ignore: []string{"b*"},
expected: []string{"foo"},
}, {
},
{
description: "glob",
paths: []string{"**"},
expected: []string{"bar", filepath.FromSlash("baz/file"), "foo"},
},
{
description: "error",
paths: []string{"unknown"},
shouldErr: true,
Expand Down
13 changes: 2 additions & 11 deletions pkg/skaffold/build/custom/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import (
"encoding/json"
"fmt"
"os/exec"
"sort"
"strings"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/list"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
Expand All @@ -50,15 +50,6 @@ func GetDependencies(ctx context.Context, workspace string, a *latest.CustomArti
return deps, nil

default:
files, err := docker.WalkWorkspace(workspace, a.Dependencies.Ignore, a.Dependencies.Paths)
if err != nil {
return nil, fmt.Errorf("walking workspace %q: %w", workspace, err)
}
var dependencies []string
for file := range files {
dependencies = append(dependencies, file)
}
sort.Strings(dependencies)
return dependencies, nil
return list.Files(workspace, a.Dependencies.Paths, a.Dependencies.Ignore)
}
}
14 changes: 11 additions & 3 deletions pkg/skaffold/build/custom/dependencies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,22 @@ func TestGetDependenciesPaths(t *testing.T) {
description: "watch everything",
paths: []string{"."},
expected: []string{"bar", filepath.FromSlash("baz/file"), "foo"},
}, {
},
{
description: "watch nothing",
}, {
},
{
description: "ignore some paths",
paths: []string{"."},
ignore: []string{"b*"},
expected: []string{"foo"},
}, {
},
{
description: "glob",
paths: []string{"**"},
expected: []string{"bar", filepath.FromSlash("baz/file"), "foo"},
},
{
description: "error",
paths: []string{"unknown"},
shouldErr: true,
Expand Down
64 changes: 64 additions & 0 deletions pkg/skaffold/build/list/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Copyright 2020 The Skaffold Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package list

import (
"fmt"
"path/filepath"
"sort"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
)

// Files list files in a workspace, given a list of patterns and exclusions.
// A pattern that doesn't correspond to any file is an error.
func Files(workspace string, patterns, excludes []string) ([]string, error) {
var paths []string

for _, pattern := range patterns {
expanded, err := filepath.Glob(filepath.Join(workspace, pattern))
if err != nil {
return nil, err
}

if len(expanded) == 0 {
return nil, fmt.Errorf("pattern %q did not match any file", pattern)
}

for _, e := range expanded {
rel, err := filepath.Rel(workspace, e)
if err != nil {
return nil, err
}

paths = append(paths, rel)
}
}

files, err := docker.WalkWorkspace(workspace, excludes, paths)
if err != nil {
return nil, fmt.Errorf("walking workspace %q: %w", workspace, err)
}

var dependencies []string
for file := range files {
dependencies = append(dependencies, file)
}

sort.Strings(dependencies)
return dependencies, nil
}

0 comments on commit 0fb5a24

Please sign in to comment.