@@ -61,7 +61,16 @@ const (
61
61
// GetBase takes an importpath and returns a base image reference and base image (or index).
62
62
type GetBase func (context.Context , string ) (name.Reference , Result , error )
63
63
64
- type builder func (context.Context , string , string , v1.Platform , Config ) (string , error )
64
+ // buildContext provides parameters for a builder function.
65
+ type buildContext struct {
66
+ ip string
67
+ dir string
68
+ env []string
69
+ platform v1.Platform
70
+ config Config
71
+ }
72
+
73
+ type builder func (context.Context , buildContext ) (string , error )
65
74
66
75
type sbomber func (context.Context , string , string , string , oci.SignedEntity , string ) ([]byte , types.MediaType , error )
67
76
@@ -81,6 +90,7 @@ type gobuild struct {
81
90
disableOptimizations bool
82
91
trimpath bool
83
92
buildConfigs map [string ]Config
93
+ env []string
84
94
platformMatcher * platformMatcher
85
95
dir string
86
96
labels map [string ]string
@@ -103,6 +113,7 @@ type gobuildOpener struct {
103
113
disableOptimizations bool
104
114
trimpath bool
105
115
buildConfigs map [string ]Config
116
+ env []string
106
117
platforms []string
107
118
labels map [string ]string
108
119
dir string
@@ -131,6 +142,7 @@ func (gbo *gobuildOpener) Open() (Interface, error) {
131
142
disableOptimizations : gbo .disableOptimizations ,
132
143
trimpath : gbo .trimpath ,
133
144
buildConfigs : gbo .buildConfigs ,
145
+ env : gbo .env ,
134
146
labels : gbo .labels ,
135
147
dir : gbo .dir ,
136
148
platformMatcher : matcher ,
@@ -251,8 +263,8 @@ func getGoBinary() string {
251
263
return defaultGoBin
252
264
}
253
265
254
- func build (ctx context.Context , ip string , dir string , platform v1. Platform , config Config ) (string , error ) {
255
- buildArgs , err := createBuildArgs (config )
266
+ func build (ctx context.Context , buildCtx buildContext ) (string , error ) {
267
+ buildArgs , err := createBuildArgs (buildCtx . config )
256
268
if err != nil {
257
269
return "" , err
258
270
}
@@ -261,9 +273,9 @@ func build(ctx context.Context, ip string, dir string, platform v1.Platform, con
261
273
args = append (args , "build" )
262
274
args = append (args , buildArgs ... )
263
275
264
- env , err := buildEnv (platform , os .Environ (), config .Env )
276
+ env , err := buildEnv (buildCtx . platform , os .Environ (), buildCtx . env , buildCtx . config .Env )
265
277
if err != nil {
266
- return "" , fmt .Errorf ("could not create env for %s: %w" , ip , err )
278
+ return "" , fmt .Errorf ("could not create env for %s: %w" , buildCtx . ip , err )
267
279
}
268
280
269
281
tmpDir := ""
@@ -282,7 +294,7 @@ func build(ctx context.Context, ip string, dir string, platform v1.Platform, con
282
294
}
283
295
284
296
// TODO(#264): if KOCACHE is unset, default to filepath.Join(os.TempDir(), "ko").
285
- tmpDir = filepath .Join (dir , "bin" , ip , platform .String ())
297
+ tmpDir = filepath .Join (dir , "bin" , buildCtx . ip , buildCtx . platform .String ())
286
298
if err := os .MkdirAll (tmpDir , os .ModePerm ); err != nil {
287
299
return "" , fmt .Errorf ("creating KOCACHE bin dir: %w" , err )
288
300
}
@@ -296,18 +308,18 @@ func build(ctx context.Context, ip string, dir string, platform v1.Platform, con
296
308
file := filepath .Join (tmpDir , "out" )
297
309
298
310
args = append (args , "-o" , file )
299
- args = append (args , ip )
311
+ args = append (args , buildCtx . ip )
300
312
301
313
gobin := getGoBinary ()
302
314
cmd := exec .CommandContext (ctx , gobin , args ... )
303
- cmd .Dir = dir
315
+ cmd .Dir = buildCtx . dir
304
316
cmd .Env = env
305
317
306
318
var output bytes.Buffer
307
319
cmd .Stderr = & output
308
320
cmd .Stdout = & output
309
321
310
- log .Printf ("Building %s for %s" , ip , platform )
322
+ log .Printf ("Building %s for %s" , buildCtx . ip , buildCtx . platform )
311
323
if err := cmd .Run (); err != nil {
312
324
if os .Getenv ("KOCACHE" ) == "" {
313
325
os .RemoveAll (tmpDir )
@@ -440,7 +452,7 @@ func cycloneDX() sbomber {
440
452
// buildEnv creates the environment variables used by the `go build` command.
441
453
// From `os/exec.Cmd`: If Env contains duplicate environment keys, only the last
442
454
// value in the slice for each duplicate key is used.
443
- func buildEnv (platform v1.Platform , userEnv , configEnv []string ) ([]string , error ) {
455
+ func buildEnv (platform v1.Platform , osEnv , globalEnv , configEnv []string ) ([]string , error ) {
444
456
// Default env
445
457
env := []string {
446
458
"CGO_ENABLED=0" ,
@@ -464,7 +476,8 @@ func buildEnv(platform v1.Platform, userEnv, configEnv []string) ([]string, erro
464
476
}
465
477
}
466
478
467
- env = append (env , userEnv ... )
479
+ env = append (env , osEnv ... )
480
+ env = append (env , globalEnv ... )
468
481
env = append (env , configEnv ... )
469
482
return env , nil
470
483
}
@@ -836,7 +849,13 @@ func (g *gobuild) buildOne(ctx context.Context, refStr string, base v1.Image, pl
836
849
}
837
850
// Do the build into a temporary file.
838
851
config := g .configForImportPath (ref .Path ())
839
- file , err := g .build (ctx , ref .Path (), g .dir , * platform , config )
852
+ file , err := g .build (ctx , buildContext {
853
+ ip : ref .Path (),
854
+ dir : g .dir ,
855
+ env : g .env ,
856
+ platform : * platform ,
857
+ config : config ,
858
+ })
840
859
if err != nil {
841
860
return nil , fmt .Errorf ("build: %w" , err )
842
861
}
0 commit comments