Skip to content

Commit b9b2435

Browse files
authored
Merge pull request #3595 from tonistiigi/bake-var-disable
bake: allow disabling env lookup for bake
2 parents efaf9da + d27c7f8 commit b9b2435

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

bake/bake.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"sort"
1616
"strconv"
1717
"strings"
18+
"sync"
1819
"time"
1920

2021
composecli "github.com/compose-spec/compose-go/v2/cli"
@@ -375,8 +376,12 @@ func ParseFiles(files []File, defaults map[string]string) (_ *Config, _ *hclpars
375376

376377
var pm hclparser.ParseMeta
377378
if len(hclFiles) > 0 {
379+
lookup := func(string) (string, bool) { return "", false }
380+
if envLookupAllowed() {
381+
lookup = os.LookupEnv
382+
}
378383
res, err := hclparser.Parse(hclparser.MergeFiles(hclFiles), hclparser.Opt{
379-
LookupVar: os.LookupEnv,
384+
LookupVar: lookup,
380385
Vars: defaults,
381386
ValidateLabel: validateTargetName,
382387
}, &c)
@@ -597,7 +602,7 @@ func (c Config) newOverrides(v []string) (map[string]map[string]Override, error)
597602
if len(keys) != 3 {
598603
return nil, errors.Errorf("invalid key %s, args requires name", parts[0])
599604
}
600-
if len(parts) < 2 {
605+
if len(parts) < 2 && envLookupAllowed() {
601606
v, ok := os.LookupEnv(keys[2])
602607
if !ok {
603608
continue
@@ -1728,3 +1733,13 @@ func parseArrValue[T any, PT arrValue[T]](s []string) ([]*T, error) {
17281733
}
17291734
return outputs, nil
17301735
}
1736+
1737+
var envLookupAllowed = sync.OnceValue(func() bool {
1738+
if v, ok := os.LookupEnv("BUILDX_BAKE_DISABLE_VARS_ENV_LOOKUP"); ok {
1739+
disable, err := strconv.ParseBool(v)
1740+
if err == nil && disable {
1741+
return false
1742+
}
1743+
}
1744+
return true
1745+
})

bake/compose.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,11 @@ func validateCompose(dt []byte, envs map[string]string) error {
304304
}
305305

306306
func composeEnv() (map[string]string, error) {
307-
envs := sliceToMap(os.Environ())
307+
var env []string
308+
if envLookupAllowed() {
309+
env = os.Environ()
310+
}
311+
envs := sliceToMap(env)
308312
if wd, err := os.Getwd(); err == nil {
309313
envs, err = loadDotEnv(envs, wd)
310314
if err != nil {

0 commit comments

Comments
 (0)