Skip to content

Commit e114dd0

Browse files
committed
bake: support compose service as build context
Signed-off-by: CrazyMax <[email protected]>
1 parent e0c67bf commit e114dd0

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

bake/compose.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package bake
33
import (
44
"context"
55
"fmt"
6-
"maps"
76
"os"
87
"path/filepath"
98
"slices"
@@ -92,7 +91,12 @@ func ParseCompose(cfgs []composetypes.ConfigFile, envs map[string]string) (*Conf
9291
var additionalContexts map[string]string
9392
if s.Build.AdditionalContexts != nil {
9493
additionalContexts = map[string]string{}
95-
maps.Copy(additionalContexts, s.Build.AdditionalContexts)
94+
for k, v := range s.Build.AdditionalContexts {
95+
if strings.HasPrefix(v, "service:") {
96+
v = strings.Replace(v, "service:", "target:", 1)
97+
}
98+
additionalContexts[k] = v
99+
}
96100
}
97101

98102
var shmSize *string

bake/compose_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,37 @@ services:
798798
})
799799
}
800800

801+
func TestServiceContext(t *testing.T) {
802+
dt := []byte(`
803+
services:
804+
base:
805+
build:
806+
dockerfile: baseapp.Dockerfile
807+
command: ./entrypoint.sh
808+
webapp:
809+
build:
810+
context: ./dir
811+
additional_contexts:
812+
base: service:base
813+
`)
814+
815+
c, err := ParseCompose([]composetypes.ConfigFile{{Content: dt}}, nil)
816+
require.NoError(t, err)
817+
818+
require.Equal(t, 1, len(c.Groups))
819+
require.Equal(t, "default", c.Groups[0].Name)
820+
sort.Strings(c.Groups[0].Targets)
821+
require.Equal(t, []string{"base", "webapp"}, c.Groups[0].Targets)
822+
823+
require.Equal(t, 2, len(c.Targets))
824+
sort.Slice(c.Targets, func(i, j int) bool {
825+
return c.Targets[i].Name < c.Targets[j].Name
826+
})
827+
828+
require.Equal(t, "webapp", c.Targets[1].Name)
829+
require.Equal(t, map[string]string{"base": "target:base"}, c.Targets[1].Contexts)
830+
}
831+
801832
// chdir changes the current working directory to the named directory,
802833
// and then restore the original working directory at the end of the test.
803834
func chdir(t *testing.T, dir string) {

0 commit comments

Comments
 (0)