@@ -45,24 +45,21 @@ func (s *composeService) Build(ctx context.Context, project *types.Project, opti
45
45
opts := map [string ]build.Options {}
46
46
imagesToBuild := []string {}
47
47
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 ) {
50
49
s , ok := project .Environment [s ]
51
50
return s , ok
52
- }).RemoveEmpty () {
53
- args [k ] = * v
54
- }
51
+ }))
55
52
56
53
for _ , service := range project .Services {
57
54
if service .Build != nil {
58
55
imageName := getImageName (service , project .Name )
59
56
imagesToBuild = append (imagesToBuild , imageName )
60
- buildOptions , err := s .toBuildOptions (service , imageName )
57
+ buildOptions , err := s .toBuildOptions (project , service , imageName )
61
58
if err != nil {
62
59
return err
63
60
}
64
61
buildOptions .Pull = options .Pull
65
- buildOptions .BuildArgs = args
62
+ buildOptions .BuildArgs = mergeArgs ( buildOptions . BuildArgs , args )
66
63
buildOptions .NoCache = options .NoCache
67
64
opts [imageName ] = buildOptions
68
65
buildOptions .CacheFrom , err = buildflags .ParseCacheEntry (service .Build .CacheFrom )
@@ -142,7 +139,7 @@ func (s *composeService) getBuildOptions(project *types.Project, images map[stri
142
139
continue
143
140
}
144
141
imagesToBuild = append (imagesToBuild , imageName )
145
- opt , err := s .toBuildOptions (service , imageName )
142
+ opt , err := s .toBuildOptions (project , service , imageName )
146
143
if err != nil {
147
144
return nil , nil , err
148
145
}
@@ -263,11 +260,14 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opts
263
260
return imagesBuilt , err
264
261
}
265
262
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 ) {
267
264
var tags []string
268
265
tags = append (tags , imageTag )
269
266
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
+ }))
271
271
272
272
var plats []specs.Platform
273
273
if service .Platform != "" {
@@ -283,7 +283,7 @@ func (s *composeService) toBuildOptions(service types.ServiceConfig, imageTag st
283
283
ContextPath : service .Build .Context ,
284
284
DockerfilePath : service .Build .Dockerfile ,
285
285
},
286
- BuildArgs : flatten ( mergeArgs ( service . Build . Args , buildArgs )) ,
286
+ BuildArgs : buildArgs ,
287
287
Tags : tags ,
288
288
Target : service .Build .Target ,
289
289
Exports : []bclient.ExportEntry {{Type : "image" , Attrs : map [string ]string {}}},
@@ -292,11 +292,11 @@ func (s *composeService) toBuildOptions(service types.ServiceConfig, imageTag st
292
292
}, nil
293
293
}
294
294
295
- func flatten (in types.MappingWithEquals ) map [ string ] string {
295
+ func flatten (in types.MappingWithEquals ) types. Mapping {
296
296
if len (in ) == 0 {
297
297
return nil
298
298
}
299
- out := make ( map [ string ] string )
299
+ out := types. Mapping {}
300
300
for k , v := range in {
301
301
if v == nil {
302
302
continue
@@ -306,15 +306,12 @@ func flatten(in types.MappingWithEquals) map[string]string {
306
306
return out
307
307
}
308
308
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
317
314
}
318
315
}
319
- return src
316
+ return merged
320
317
}
0 commit comments