Skip to content

Commit

Permalink
fix: ensure docker runtimes support dockerignore, empty build args an…
Browse files Browse the repository at this point in the history
…d ignore other entrypoint files (#703)
  • Loading branch information
davemooreuws authored Apr 2, 2024
1 parent 21de10a commit 5c589ac
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
10 changes: 6 additions & 4 deletions pkg/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ func fromProjectConfiguration(projectConfig *ProjectConfiguration, fs afero.Fs)

var buildContext *runtime.RuntimeBuildContext

otherEntryPointFiles := lo.Filter(files, func(file string, index int) bool {
return file != f
})

if serviceSpec.Runtime != "" {
// We have a custom runtime
customRuntime, ok := projectConfig.Runtimes[serviceSpec.Runtime]
Expand All @@ -320,8 +324,7 @@ func fromProjectConfiguration(projectConfig *ProjectConfiguration, fs afero.Fs)
relativeServiceEntrypointPath,
customRuntime.Dockerfile,
customRuntime.Args,
// TODO: Get other entrypoint files as ignores
[]string{},
otherEntryPointFiles,
fs,
)
if err != nil {
Expand All @@ -332,8 +335,7 @@ func fromProjectConfiguration(projectConfig *ProjectConfiguration, fs afero.Fs)
relativeServiceEntrypointPath,
"",
map[string]string{},
// TODO: Get other entrypoint files as ignores
[]string{},
otherEntryPointFiles,
fs,
)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion pkg/project/runtime/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/spf13/afero"
)

func TestGenerate(t *testing.T) {
Expand All @@ -30,6 +31,8 @@ func TestGenerate(t *testing.T) {
jsFile, _ := os.ReadFile("javascript.dockerfile")
jvmFile, _ := os.ReadFile("jvm.dockerfile")

fs := afero.NewOsFs()

tests := []struct {
name string
handler string
Expand Down Expand Up @@ -63,7 +66,7 @@ func TestGenerate(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rt, err := NewBuildContext(tt.handler, "", map[string]string{}, []string{}, nil)
rt, err := NewBuildContext(tt.handler, "", map[string]string{}, []string{}, fs)
if err != nil {
t.Error(err)
}
Expand Down
47 changes: 46 additions & 1 deletion pkg/project/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"path/filepath"
"strings"

"github.com/samber/lo"
"github.com/spf13/afero"
)

Expand All @@ -47,6 +48,31 @@ const (

var commonIgnore = []string{".nitric/", "!.nitric/*.yaml", ".git/", ".idea/", ".vscode/", ".github/", "*.dockerfile", "*.dockerignore"}

func getDockerIgnores(dockerIgnorePath string, fs afero.Fs) ([]string, error) {
// Check if the file exists
exists, err := afero.Exists(fs, dockerIgnorePath)
if err != nil {
return nil, err
}

if exists {
// Read the file
content, err := afero.ReadFile(fs, dockerIgnorePath)
if err != nil {
return nil, err
}

// Split the content into lines
lines := lo.Filter[string](strings.Split(string(content), "\n"), func(line string, index int) bool {
return strings.TrimSpace(line) != ""
})

return lines, nil
}

return []string{}, nil
}

func customBuildContext(entrypointFilePath string, dockerfilePath string, buildArgs map[string]string, additionalIgnores []string, fs afero.Fs) (*RuntimeBuildContext, error) {
// Get the dockerfile contents
// dockerfilePath
Expand All @@ -55,7 +81,10 @@ func customBuildContext(entrypointFilePath string, dockerfilePath string, buildA
return nil, err
}

// Get the ignore file contents
// ensure build args exists
if buildArgs == nil {
buildArgs = map[string]string{}
}

// Append handler to build args
buildArgs["HANDLER"] = filepath.ToSlash(entrypointFilePath)
Expand Down Expand Up @@ -179,11 +208,27 @@ func dartBuildContext(entrypointFilePath string, additionalIgnores []string) (*R
// if a dockerfile path is provided a custom runtime is assumed, otherwise the entrypoint file is used for automatic detection of language runtime.
func NewBuildContext(entrypointFilePath string, dockerfilePath string, buildArgs map[string]string, additionalIgnores []string, fs afero.Fs) (*RuntimeBuildContext, error) {
if dockerfilePath != "" {
dockerIgnorePath := fmt.Sprintf("%s.dockerignore", dockerfilePath)

dockerIgnores, err := getDockerIgnores(dockerIgnorePath, fs)
if err != nil {
return nil, err
}

additionalIgnores = append(additionalIgnores, dockerIgnores...)

return customBuildContext(entrypointFilePath, dockerfilePath, buildArgs, additionalIgnores, fs)
}

ext := filepath.Ext(entrypointFilePath)

dockerIgnores, err := getDockerIgnores(".dockerignore", fs)
if err != nil {
return nil, err
}

additionalIgnores = append(additionalIgnores, dockerIgnores...)

switch ext {
case ".csproj":
return csharpBuildContext(entrypointFilePath, additionalIgnores)
Expand Down

0 comments on commit 5c589ac

Please sign in to comment.