Skip to content

Commit c365f01

Browse files
committed
compose: sanitize value of named contexts for target type
Signed-off-by: CrazyMax <[email protected]>
1 parent 8f57074 commit c365f01

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

bake/compose.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,7 @@ func ParseCompose(cfgs []composetypes.ConfigFile, envs map[string]string) (*Conf
7676

7777
var additionalContexts map[string]string
7878
if s.Build.AdditionalContexts != nil {
79-
additionalContexts = map[string]string{}
80-
for k, v := range s.Build.AdditionalContexts {
81-
if strings.HasPrefix(v, "service:") {
82-
v = strings.Replace(v, "service:", "target:", 1)
83-
}
84-
additionalContexts[k] = v
85-
}
79+
additionalContexts = composeToBuildkitNamedContexts(s.Build.AdditionalContexts)
8680
}
8781

8882
var shmSize *string
@@ -471,7 +465,7 @@ func (t *Target) composeExtTarget(exts map[string]any) error {
471465
t.NoCacheFilter = dedupSlice(append(t.NoCacheFilter, xb.NoCacheFilter...))
472466
}
473467
if len(xb.Contexts) > 0 {
474-
t.Contexts = dedupMap(t.Contexts, xb.Contexts)
468+
t.Contexts = dedupMap(t.Contexts, composeToBuildkitNamedContexts(xb.Contexts))
475469
}
476470

477471
return nil
@@ -506,3 +500,16 @@ func composeToBuildkitSSH(sshKey composetypes.SSHKey) *buildflags.SSH {
506500
}
507501
return bkssh
508502
}
503+
504+
func composeToBuildkitNamedContexts(m map[string]string) map[string]string {
505+
out := make(map[string]string, len(m))
506+
for k, v := range m {
507+
if strings.HasPrefix(v, "service:") || strings.HasPrefix(v, "target:") {
508+
if parts := strings.SplitN(v, ":", 2); len(parts) == 2 {
509+
v = "target:" + sanitizeTargetName(parts[1])
510+
}
511+
}
512+
out[k] = v
513+
}
514+
return out
515+
}

bake/compose_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,44 @@ services:
893893
require.Equal(t, map[string]string{"base": "target:base"}, c.Targets[1].Contexts)
894894
}
895895

896+
func TestServiceContextDot(t *testing.T) {
897+
dt := []byte(`
898+
services:
899+
base.1:
900+
build:
901+
dockerfile: baseapp.Dockerfile
902+
command: ./entrypoint.sh
903+
foo.1:
904+
build:
905+
dockerfile: fooapp.Dockerfile
906+
command: ./entrypoint.sh
907+
webapp:
908+
build:
909+
context: ./dir
910+
additional_contexts:
911+
base: service:base.1
912+
x-bake:
913+
contexts:
914+
foo: target:foo.1
915+
`)
916+
917+
c, err := ParseCompose([]composetypes.ConfigFile{{Content: dt}}, nil)
918+
require.NoError(t, err)
919+
920+
require.Equal(t, 1, len(c.Groups))
921+
require.Equal(t, "default", c.Groups[0].Name)
922+
sort.Strings(c.Groups[0].Targets)
923+
require.Equal(t, []string{"base_1", "foo_1", "webapp"}, c.Groups[0].Targets)
924+
925+
require.Equal(t, 3, len(c.Targets))
926+
sort.Slice(c.Targets, func(i, j int) bool {
927+
return c.Targets[i].Name < c.Targets[j].Name
928+
})
929+
930+
require.Equal(t, "webapp", c.Targets[2].Name)
931+
require.Equal(t, map[string]string{"base": "target:base_1", "foo": "target:foo_1"}, c.Targets[2].Contexts)
932+
}
933+
896934
func TestDotEnvDir(t *testing.T) {
897935
tmpdir := t.TempDir()
898936
require.NoError(t, os.Mkdir(filepath.Join(tmpdir, ".env"), 0755))

0 commit comments

Comments
 (0)