@@ -45,24 +45,21 @@ func (s *composeService) Build(ctx context.Context, project *types.Project, opti
4545 opts := map [string ]build.Options {}
4646 imagesToBuild := []string {}
4747
48- args := map [string ]string {}
49- for k , v := range options .Args .Resolve (func (s string ) (string , bool ) {
48+ args := flatten (options .Args .Resolve (func (s string ) (string , bool ) {
5049 s , ok := project .Environment [s ]
5150 return s , ok
52- }).RemoveEmpty () {
53- args [k ] = * v
54- }
51+ }))
5552
5653 for _ , service := range project .Services {
5754 if service .Build != nil {
5855 imageName := getImageName (service , project .Name )
5956 imagesToBuild = append (imagesToBuild , imageName )
60- buildOptions , err := s .toBuildOptions (service , imageName )
57+ buildOptions , err := s .toBuildOptions (project , service , imageName )
6158 if err != nil {
6259 return err
6360 }
6461 buildOptions .Pull = options .Pull
65- buildOptions .BuildArgs = args
62+ buildOptions .BuildArgs = mergeArgs ( buildOptions . BuildArgs , args )
6663 buildOptions .NoCache = options .NoCache
6764 opts [imageName ] = buildOptions
6865 buildOptions .CacheFrom , err = buildflags .ParseCacheEntry (service .Build .CacheFrom )
@@ -142,7 +139,7 @@ func (s *composeService) getBuildOptions(project *types.Project, images map[stri
142139 continue
143140 }
144141 imagesToBuild = append (imagesToBuild , imageName )
145- opt , err := s .toBuildOptions (service , imageName )
142+ opt , err := s .toBuildOptions (project , service , imageName )
146143 if err != nil {
147144 return nil , nil , err
148145 }
@@ -263,11 +260,14 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opts
263260 return imagesBuilt , err
264261}
265262
266- func (s * composeService ) toBuildOptions (service types.ServiceConfig , imageTag string ) (build.Options , error ) {
263+ func (s * composeService ) toBuildOptions (project * types. Project , service types.ServiceConfig , imageTag string ) (build.Options , error ) {
267264 var tags []string
268265 tags = append (tags , imageTag )
269266
270- var buildArgs map [string ]string
267+ buildArgs := flatten (service .Build .Args .Resolve (func (s string ) (string , bool ) {
268+ s , ok := project .Environment [s ]
269+ return s , ok
270+ }))
271271
272272 var plats []specs.Platform
273273 if service .Platform != "" {
@@ -283,7 +283,7 @@ func (s *composeService) toBuildOptions(service types.ServiceConfig, imageTag st
283283 ContextPath : service .Build .Context ,
284284 DockerfilePath : service .Build .Dockerfile ,
285285 },
286- BuildArgs : flatten ( mergeArgs ( service . Build . Args , buildArgs )) ,
286+ BuildArgs : buildArgs ,
287287 Tags : tags ,
288288 Target : service .Build .Target ,
289289 Exports : []bclient.ExportEntry {{Type : "image" , Attrs : map [string ]string {}}},
@@ -292,11 +292,11 @@ func (s *composeService) toBuildOptions(service types.ServiceConfig, imageTag st
292292 }, nil
293293}
294294
295- func flatten (in types.MappingWithEquals ) map [ string ] string {
295+ func flatten (in types.MappingWithEquals ) types. Mapping {
296296 if len (in ) == 0 {
297297 return nil
298298 }
299- out := make ( map [ string ] string )
299+ out := types. Mapping {}
300300 for k , v := range in {
301301 if v == nil {
302302 continue
@@ -306,15 +306,12 @@ func flatten(in types.MappingWithEquals) map[string]string {
306306 return out
307307}
308308
309- func mergeArgs (src types.MappingWithEquals , values map [string ]string ) types.MappingWithEquals {
310- for key := range src {
311- if val , ok := values [key ]; ok {
312- if val == "" {
313- src [key ] = nil
314- } else {
315- src [key ] = & val
316- }
309+ func mergeArgs (m ... types.Mapping ) types.Mapping {
310+ merged := types.Mapping {}
311+ for _ , mapping := range m {
312+ for key , val := range mapping {
313+ merged [key ] = val
317314 }
318315 }
319- return src
316+ return merged
320317}
0 commit comments