Skip to content

Commit cb8843f

Browse files
authored
Merge pull request #346 from ezradiniz/bugfix/load-service-with-extends-build-context
fix build context path to allow url to a git repository
2 parents ddc8c41 + 2df3c92 commit cb8843f

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

loader/loader.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -574,9 +574,7 @@ func loadServiceWithExtends(filename, name string, servicesDict map[string]inter
574574
// absolute path.
575575
baseFileParent := filepath.Dir(*file)
576576
if baseService.Build != nil {
577-
// Note that the Dockerfile is always defined relative to the
578-
// build context, so there's no need to update the Dockerfile field.
579-
baseService.Build.Context = absPath(baseFileParent, baseService.Build.Context)
577+
baseService.Build.Context = resolveBuildContextPath(baseFileParent, baseService.Build.Context)
580578
}
581579

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

597+
func resolveBuildContextPath(baseFileParent string, context string) string {
598+
// Checks if the context is an HTTP(S) URL or a remote git repository URL
599+
for _, prefix := range []string{"https://", "http://", "git://", "github.com/", "git@"} {
600+
if strings.HasPrefix(context, prefix) {
601+
return context
602+
}
603+
}
604+
605+
// Note that the Dockerfile is always defined relative to the
606+
// build context, so there's no need to update the Dockerfile field.
607+
return absPath(baseFileParent, context)
608+
}
609+
599610
// LoadService produces a single ServiceConfig from a compose file Dict
600611
// the serviceDict is not validated if directly used. Use Load() to enable validation
601612
func LoadService(name string, serviceDict map[string]interface{}, workingDir string, lookupEnv template.Mapping, resolvePaths bool, convertPaths bool) (*types.ServiceConfig, error) {

loader/loader_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,6 +1875,39 @@ func TestLoadWithExtends(t *testing.T) {
18751875
assert.Check(t, is.DeepEqual(expServices, actual.Services))
18761876
}
18771877

1878+
func TestLoadWithExtendsWithContextUrl(t *testing.T) {
1879+
b, err := os.ReadFile("testdata/compose-test-extends-with-context-url.yaml")
1880+
assert.NilError(t, err)
1881+
1882+
configDetails := types.ConfigDetails{
1883+
WorkingDir: "testdata",
1884+
ConfigFiles: []types.ConfigFile{
1885+
{Filename: "testdata/compose-test-extends-with-context-url.yaml", Content: b},
1886+
},
1887+
}
1888+
1889+
actual, err := Load(configDetails)
1890+
assert.NilError(t, err)
1891+
1892+
expServices := types.Services{
1893+
{
1894+
Name: "importer-with-https-url",
1895+
Build: &types.BuildConfig{
1896+
Context: "https://github.com/docker/compose.git",
1897+
Dockerfile: "Dockerfile",
1898+
},
1899+
Extends: types.ExtendsConfig{
1900+
"file": strPtr("compose-test-extends-with-context-url-imported.yaml"),
1901+
"service": strPtr("imported-with-https-url"),
1902+
},
1903+
Environment: types.MappingWithEquals{},
1904+
Networks: map[string]*types.ServiceNetworkConfig{"default": nil},
1905+
Scale: 1,
1906+
},
1907+
}
1908+
assert.Check(t, is.DeepEqual(expServices, actual.Services))
1909+
}
1910+
18781911
func TestServiceDeviceRequestCount(t *testing.T) {
18791912
_, err := loadYAML(`
18801913
services:
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
services:
2+
imported-with-https-url:
3+
build:
4+
context: https://github.com/docker/compose.git
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
services:
2+
importer-with-https-url:
3+
extends:
4+
file: compose-test-extends-with-context-url-imported.yaml
5+
service: imported-with-https-url

0 commit comments

Comments
 (0)