collect built image IDs

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2023-03-14 13:33:33 +01:00 committed by Nicolas De loof
parent bbe1b77a67
commit e492330dd5
1 changed files with 54 additions and 38 deletions

View File

@ -52,57 +52,73 @@ func (s *composeService) Build(ctx context.Context, project *types.Project, opti
func (s *composeService) build(ctx context.Context, project *types.Project, options api.BuildOptions) (map[string]string, error) { func (s *composeService) build(ctx context.Context, project *types.Project, options api.BuildOptions) (map[string]string, error) {
args := flatten(options.Args.Resolve(envResolver(project.Environment))) args := flatten(options.Args.Resolve(envResolver(project.Environment)))
var imageIDs map[string]string builtIDs := make([]string, len(project.Services))
err := InDependencyOrder(ctx, project, func(ctx context.Context, name string) error { err := InDependencyOrder(ctx, project, func(ctx context.Context, name string) error {
if len(options.Services) > 0 && !utils.Contains(options.Services, name) { if len(options.Services) > 0 && !utils.Contains(options.Services, name) {
return nil return nil
} }
service, err := project.GetService(name) for i, service := range project.Services {
if err != nil { if service.Name != name {
return err continue
} }
if service.Build == nil { service, err := project.GetService(name)
return nil if err != nil {
} return err
imageName := api.GetImageNameOrDefault(service, project.Name) }
buildOptions, err := s.toBuildOptions(project, service, imageName, options.SSHs) if service.Build == nil {
if err != nil { return nil
return err }
} imageName := api.GetImageNameOrDefault(service, project.Name)
buildOptions.Pull = options.Pull buildOptions, err := s.toBuildOptions(project, service, imageName, options.SSHs)
buildOptions.BuildArgs = mergeArgs(buildOptions.BuildArgs, args) if err != nil {
buildOptions.NoCache = options.NoCache return err
buildOptions.CacheFrom, err = buildflags.ParseCacheEntry(service.Build.CacheFrom) }
if err != nil { buildOptions.Pull = options.Pull
return err buildOptions.BuildArgs = mergeArgs(buildOptions.BuildArgs, args)
} buildOptions.NoCache = options.NoCache
for _, image := range service.Build.CacheFrom { buildOptions.CacheFrom, err = buildflags.ParseCacheEntry(service.Build.CacheFrom)
buildOptions.CacheFrom = append(buildOptions.CacheFrom, bclient.CacheOptionsEntry{ if err != nil {
Type: "registry", return err
Attrs: map[string]string{"ref": image}, }
}) for _, image := range service.Build.CacheFrom {
} buildOptions.CacheFrom = append(buildOptions.CacheFrom, bclient.CacheOptionsEntry{
buildOptions.Exports = []bclient.ExportEntry{{ Type: "registry",
Type: "docker", Attrs: map[string]string{"ref": image},
Attrs: map[string]string{ })
"load": "true", }
"push": fmt.Sprint(options.Push),
},
}}
if len(buildOptions.Platforms) > 1 {
buildOptions.Exports = []bclient.ExportEntry{{ buildOptions.Exports = []bclient.ExportEntry{{
Type: "image", Type: "docker",
Attrs: map[string]string{ Attrs: map[string]string{
"load": "true",
"push": fmt.Sprint(options.Push), "push": fmt.Sprint(options.Push),
}, },
}} }}
if len(buildOptions.Platforms) > 1 {
buildOptions.Exports = []bclient.ExportEntry{{
Type: "image",
Attrs: map[string]string{
"push": fmt.Sprint(options.Push),
},
}}
}
opts := map[string]build.Options{imageName: buildOptions}
ids, err := s.doBuild(ctx, project, opts, options.Progress)
if err != nil {
return err
}
builtIDs[i] = ids[imageName]
} }
opts := map[string]build.Options{imageName: buildOptions} return nil
imageIDs, err = s.doBuild(ctx, project, opts, options.Progress)
return err
}, func(traversal *graphTraversal) { }, func(traversal *graphTraversal) {
traversal.maxConcurrency = s.maxConcurrency traversal.maxConcurrency = s.maxConcurrency
}) })
imageIDs := map[string]string{}
for i, d := range builtIDs {
if d != "" {
imageIDs[project.Services[i].Image] = d
}
}
return imageIDs, err return imageIDs, err
} }