Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions pkg/compose/build_bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,22 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
privileged bool
read []string
expectedImages = make(map[string]string, len(serviceToBeBuild)) // service name -> expected image
targets = make(map[string]string, len(serviceToBeBuild)) // service name -> build target
)

for serviceName, service := range serviceToBeBuild {
// produce a unique ID for service used as bake target
for serviceName := range project.Services {
t := strings.ReplaceAll(serviceName, ".", "_")
for {
if _, ok := targets[serviceName]; !ok {
targets[serviceName] = t
break
}
t += "_"
}
}

for serviceName, service := range project.Services {
if service.Build == nil {
continue
}
Expand Down Expand Up @@ -192,9 +205,10 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
}
}

cfg.Targets[serviceName] = bakeTarget{
target := targets[serviceName]
cfg.Targets[target] = bakeTarget{
Context: build.Context,
Contexts: additionalContexts(build.AdditionalContexts),
Contexts: additionalContexts(build.AdditionalContexts, targets),
Dockerfile: dockerFilePath(build.Context, build.Dockerfile),
DockerfileInline: strings.ReplaceAll(build.DockerfileInline, "${", "$${"),
Args: args,
Expand All @@ -216,7 +230,14 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
Outputs: outputs,
Call: call,
}
group.Targets = append(group.Targets, serviceName)
}

// create a bake group with targets for services to build
for serviceName, service := range serviceToBeBuild {
if service.Build == nil {
continue
}
group.Targets = append(group.Targets, targets[serviceName])
}

cfg.Groups["default"] = group
Expand Down Expand Up @@ -340,7 +361,7 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
cw := progress.ContextWriter(ctx)
results := map[string]string{}
for service, name := range expectedImages {
built, ok := md[service] // bake target == service name
built, ok := md[targets[service]]
if !ok {
return nil, fmt.Errorf("build result not found in Bake metadata for service %s", service)
}
Expand All @@ -350,11 +371,11 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
return results, nil
}

func additionalContexts(contexts types.Mapping) map[string]string {
func additionalContexts(contexts types.Mapping, targets map[string]string) map[string]string {
ac := map[string]string{}
for k, v := range contexts {
if target, found := strings.CutPrefix(v, types.ServicePrefix); found {
v = "target:" + target
v = "target:" + targets[target]
}
ac[k] = v
}
Expand Down