Skip to content

Commit

Permalink
Merge pull request #385 from ndeloof/additional_context_path
Browse files Browse the repository at this point in the history
  • Loading branch information
ndeloof authored Apr 17, 2023
2 parents 5390f0a + 1592cda commit e4d5895
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 8 deletions.
2 changes: 1 addition & 1 deletion loader/full-struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func services(workingDir, homeDir string) []types.ServiceConfig {
Target: "foo",
Network: "foo",
CacheFrom: []string{"foo", "bar"},
AdditionalContexts: map[string]*string{"foo": strPtr("/bar")},
AdditionalContexts: types.Mapping{"foo": "/bar"},
Labels: map[string]string{"FOO": "BAR"},
Secrets: []types.ServiceSecretConfig{
{
Expand Down
21 changes: 15 additions & 6 deletions loader/normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,23 @@ func Normalize(project *types.Project, resolvePaths bool) error {
if s.Build.Dockerfile == "" && s.Build.DockerfileInline == "" {
s.Build.Dockerfile = "Dockerfile"
}
localContext := absPath(project.WorkingDir, s.Build.Context)
if _, err := os.Stat(localContext); err == nil {
if resolvePaths {
if resolvePaths {
// Build context might be a remote http/git context. Unfortunately supported "remote"
// syntax is highly ambiguous in moby/moby and not defined by compose-spec,
// so let's assume runtime will check
localContext := absPath(project.WorkingDir, s.Build.Context)
if _, err := os.Stat(localContext); err == nil {
s.Build.Context = localContext
}
// } else {
// might be a remote http/git context. Unfortunately supported "remote" syntax is highly ambiguous
// in moby/moby and not defined by compose-spec, so let's assume runtime will check
for name, path := range s.Build.AdditionalContexts {
if strings.Contains(path, "://") { // `docker-image://` or any builder specific context type
continue
}
path = absPath(project.WorkingDir, path)
if _, err := os.Stat(path); err == nil {
s.Build.AdditionalContexts[name] = path
}
}
}
s.Build.Args = s.Build.Args.Resolve(fn)
}
Expand Down
52 changes: 52 additions & 0 deletions loader/normalize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,55 @@ networks:
assert.NilError(t, err)
assert.Equal(t, expected, string(marshal))
}

func TestNormalizeAdditionalContexts(t *testing.T) {
project := types.Project{
Name: "myProject",
Services: types.Services{
types.ServiceConfig{
Name: "test",
Build: &types.BuildConfig{
Context: ".",
Dockerfile: "Dockerfile",
AdditionalContexts: map[string]string{
"image": "docker-image://foo",
"oci": "oci-layout://foo",
"abs_path": "/tmp",
"rel_path": "./testdata",
},
},
},
},
}

absCwd, _ := filepath.Abs(".")
expected := types.Project{
Name: "myProject",
Services: types.Services{
types.ServiceConfig{
Name: "test",
Build: &types.BuildConfig{
Context: absCwd,
Dockerfile: "Dockerfile",
AdditionalContexts: map[string]string{
"image": "docker-image://foo",
"oci": "oci-layout://foo",
"abs_path": "/tmp",
"rel_path": filepath.Join(absCwd, "testdata"),
},
},
Networks: map[string]*types.ServiceNetworkConfig{
"default": nil,
},
},
},
Networks: types.Networks{"default": types.NetworkConfig{
Name: "myProject_default",
}},
WorkingDir: absCwd,
ComposeFiles: []string{},
}
err := Normalize(&project, true)
assert.NilError(t, err)
assert.DeepEqual(t, expected, project)
}
2 changes: 1 addition & 1 deletion types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ type BuildConfig struct {
CacheFrom StringList `mapstructure:"cache_from" yaml:"cache_from,omitempty" json:"cache_from,omitempty"`
CacheTo StringList `mapstructure:"cache_to" yaml:"cache_to,omitempty" json:"cache_to,omitempty"`
NoCache bool `mapstructure:"no_cache" yaml:"no_cache,omitempty" json:"no_cache,omitempty"`
AdditionalContexts MappingWithEquals `mapstructure:"additional_contexts" yaml:"additional_contexts,omitempty" json:"additional_contexts,omitempty"`
AdditionalContexts Mapping `mapstructure:"additional_contexts" yaml:"additional_contexts,omitempty" json:"additional_contexts,omitempty"`
Pull bool `mapstructure:"pull" yaml:"pull,omitempty" json:"pull,omitempty"`
ExtraHosts HostsList `mapstructure:"extra_hosts" yaml:"extra_hosts,omitempty" json:"extra_hosts,omitempty"`
Isolation string `yaml:",omitempty" json:"isolation,omitempty"`
Expand Down

0 comments on commit e4d5895

Please sign in to comment.