mirror of
https://github.com/docker/compose.git
synced 2025-07-20 12:14:41 +02:00
Merge pull request #1679 from ndeloof/build_args
resolve build args on compose up --build
This commit is contained in:
commit
dbd936b704
@ -45,24 +45,21 @@ func (s *composeService) Build(ctx context.Context, project *types.Project, opti
|
|||||||
opts := map[string]build.Options{}
|
opts := map[string]build.Options{}
|
||||||
imagesToBuild := []string{}
|
imagesToBuild := []string{}
|
||||||
|
|
||||||
args := map[string]string{}
|
args := flatten(options.Args.Resolve(func(s string) (string, bool) {
|
||||||
for k, v := range options.Args.Resolve(func(s string) (string, bool) {
|
|
||||||
s, ok := project.Environment[s]
|
s, ok := project.Environment[s]
|
||||||
return s, ok
|
return s, ok
|
||||||
}).RemoveEmpty() {
|
}))
|
||||||
args[k] = *v
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, service := range project.Services {
|
for _, service := range project.Services {
|
||||||
if service.Build != nil {
|
if service.Build != nil {
|
||||||
imageName := getImageName(service, project.Name)
|
imageName := getImageName(service, project.Name)
|
||||||
imagesToBuild = append(imagesToBuild, imageName)
|
imagesToBuild = append(imagesToBuild, imageName)
|
||||||
buildOptions, err := s.toBuildOptions(service, imageName)
|
buildOptions, err := s.toBuildOptions(project, service, imageName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
buildOptions.Pull = options.Pull
|
buildOptions.Pull = options.Pull
|
||||||
buildOptions.BuildArgs = args
|
buildOptions.BuildArgs = mergeArgs(buildOptions.BuildArgs, args)
|
||||||
buildOptions.NoCache = options.NoCache
|
buildOptions.NoCache = options.NoCache
|
||||||
opts[imageName] = buildOptions
|
opts[imageName] = buildOptions
|
||||||
buildOptions.CacheFrom, err = buildflags.ParseCacheEntry(service.Build.CacheFrom)
|
buildOptions.CacheFrom, err = buildflags.ParseCacheEntry(service.Build.CacheFrom)
|
||||||
@ -142,7 +139,7 @@ func (s *composeService) getBuildOptions(project *types.Project, images map[stri
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
imagesToBuild = append(imagesToBuild, imageName)
|
imagesToBuild = append(imagesToBuild, imageName)
|
||||||
opt, err := s.toBuildOptions(service, imageName)
|
opt, err := s.toBuildOptions(project, service, imageName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
@ -263,11 +260,14 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opts
|
|||||||
return imagesBuilt, err
|
return imagesBuilt, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *composeService) toBuildOptions(service types.ServiceConfig, imageTag string) (build.Options, error) {
|
func (s *composeService) toBuildOptions(project *types.Project, service types.ServiceConfig, imageTag string) (build.Options, error) {
|
||||||
var tags []string
|
var tags []string
|
||||||
tags = append(tags, imageTag)
|
tags = append(tags, imageTag)
|
||||||
|
|
||||||
var buildArgs map[string]string
|
buildArgs := flatten(service.Build.Args.Resolve(func(s string) (string, bool) {
|
||||||
|
s, ok := project.Environment[s]
|
||||||
|
return s, ok
|
||||||
|
}))
|
||||||
|
|
||||||
var plats []specs.Platform
|
var plats []specs.Platform
|
||||||
if service.Platform != "" {
|
if service.Platform != "" {
|
||||||
@ -283,7 +283,7 @@ func (s *composeService) toBuildOptions(service types.ServiceConfig, imageTag st
|
|||||||
ContextPath: service.Build.Context,
|
ContextPath: service.Build.Context,
|
||||||
DockerfilePath: service.Build.Dockerfile,
|
DockerfilePath: service.Build.Dockerfile,
|
||||||
},
|
},
|
||||||
BuildArgs: flatten(mergeArgs(service.Build.Args, buildArgs)),
|
BuildArgs: buildArgs,
|
||||||
Tags: tags,
|
Tags: tags,
|
||||||
Target: service.Build.Target,
|
Target: service.Build.Target,
|
||||||
Exports: []bclient.ExportEntry{{Type: "image", Attrs: map[string]string{}}},
|
Exports: []bclient.ExportEntry{{Type: "image", Attrs: map[string]string{}}},
|
||||||
@ -292,11 +292,11 @@ func (s *composeService) toBuildOptions(service types.ServiceConfig, imageTag st
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func flatten(in types.MappingWithEquals) map[string]string {
|
func flatten(in types.MappingWithEquals) types.Mapping {
|
||||||
if len(in) == 0 {
|
if len(in) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
out := make(map[string]string)
|
out := types.Mapping{}
|
||||||
for k, v := range in {
|
for k, v := range in {
|
||||||
if v == nil {
|
if v == nil {
|
||||||
continue
|
continue
|
||||||
@ -306,15 +306,12 @@ func flatten(in types.MappingWithEquals) map[string]string {
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
func mergeArgs(src types.MappingWithEquals, values map[string]string) types.MappingWithEquals {
|
func mergeArgs(m ...types.Mapping) types.Mapping {
|
||||||
for key := range src {
|
merged := types.Mapping{}
|
||||||
if val, ok := values[key]; ok {
|
for _, mapping := range m {
|
||||||
if val == "" {
|
for key, val := range mapping {
|
||||||
src[key] = nil
|
merged[key] = val
|
||||||
} else {
|
|
||||||
src[key] = &val
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return src
|
return merged
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user