Skip to content

Commit

Permalink
Merge pull request #346 from ezradiniz/bugfix/load-service-with-exten…
Browse files Browse the repository at this point in the history
…ds-build-context

fix build context path to allow url to a git repository
  • Loading branch information
glours authored Jan 20, 2023
2 parents ddc8c41 + 2df3c92 commit cb8843f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
17 changes: 14 additions & 3 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,9 +574,7 @@ func loadServiceWithExtends(filename, name string, servicesDict map[string]inter
// absolute path.
baseFileParent := filepath.Dir(*file)
if baseService.Build != nil {
// Note that the Dockerfile is always defined relative to the
// build context, so there's no need to update the Dockerfile field.
baseService.Build.Context = absPath(baseFileParent, baseService.Build.Context)
baseService.Build.Context = resolveBuildContextPath(baseFileParent, baseService.Build.Context)
}

for i, vol := range baseService.Volumes {
Expand All @@ -596,6 +594,19 @@ func loadServiceWithExtends(filename, name string, servicesDict map[string]inter
return serviceConfig, nil
}

func resolveBuildContextPath(baseFileParent string, context string) string {
// Checks if the context is an HTTP(S) URL or a remote git repository URL
for _, prefix := range []string{"https://", "http://", "git://", "github.com/", "git@"} {
if strings.HasPrefix(context, prefix) {
return context
}
}

// Note that the Dockerfile is always defined relative to the
// build context, so there's no need to update the Dockerfile field.
return absPath(baseFileParent, context)
}

// LoadService produces a single ServiceConfig from a compose file Dict
// the serviceDict is not validated if directly used. Use Load() to enable validation
func LoadService(name string, serviceDict map[string]interface{}, workingDir string, lookupEnv template.Mapping, resolvePaths bool, convertPaths bool) (*types.ServiceConfig, error) {
Expand Down
33 changes: 33 additions & 0 deletions loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1875,6 +1875,39 @@ func TestLoadWithExtends(t *testing.T) {
assert.Check(t, is.DeepEqual(expServices, actual.Services))
}

func TestLoadWithExtendsWithContextUrl(t *testing.T) {
b, err := os.ReadFile("testdata/compose-test-extends-with-context-url.yaml")
assert.NilError(t, err)

configDetails := types.ConfigDetails{
WorkingDir: "testdata",
ConfigFiles: []types.ConfigFile{
{Filename: "testdata/compose-test-extends-with-context-url.yaml", Content: b},
},
}

actual, err := Load(configDetails)
assert.NilError(t, err)

expServices := types.Services{
{
Name: "importer-with-https-url",
Build: &types.BuildConfig{
Context: "https://github.com/docker/compose.git",
Dockerfile: "Dockerfile",
},
Extends: types.ExtendsConfig{
"file": strPtr("compose-test-extends-with-context-url-imported.yaml"),
"service": strPtr("imported-with-https-url"),
},
Environment: types.MappingWithEquals{},
Networks: map[string]*types.ServiceNetworkConfig{"default": nil},
Scale: 1,
},
}
assert.Check(t, is.DeepEqual(expServices, actual.Services))
}

func TestServiceDeviceRequestCount(t *testing.T) {
_, err := loadYAML(`
services:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
services:
imported-with-https-url:
build:
context: https://github.com/docker/compose.git
5 changes: 5 additions & 0 deletions loader/testdata/compose-test-extends-with-context-url.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
importer-with-https-url:
extends:
file: compose-test-extends-with-context-url-imported.yaml
service: imported-with-https-url

0 comments on commit cb8843f

Please sign in to comment.