Merge pull request #3595 from tonistiigi/bake-var-disable

bake: allow disabling env lookup for bake
This commit is contained in:
Tõnis Tiigi
2026-01-14 14:54:17 -08:00
committed by GitHub
2 changed files with 22 additions and 3 deletions
+17 -2
View File
@@ -15,6 +15,7 @@ import (
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
composecli "github.com/compose-spec/compose-go/v2/cli" composecli "github.com/compose-spec/compose-go/v2/cli"
@@ -375,8 +376,12 @@ func ParseFiles(files []File, defaults map[string]string) (_ *Config, _ *hclpars
var pm hclparser.ParseMeta var pm hclparser.ParseMeta
if len(hclFiles) > 0 { if len(hclFiles) > 0 {
lookup := func(string) (string, bool) { return "", false }
if envLookupAllowed() {
lookup = os.LookupEnv
}
res, err := hclparser.Parse(hclparser.MergeFiles(hclFiles), hclparser.Opt{ res, err := hclparser.Parse(hclparser.MergeFiles(hclFiles), hclparser.Opt{
LookupVar: os.LookupEnv, LookupVar: lookup,
Vars: defaults, Vars: defaults,
ValidateLabel: validateTargetName, ValidateLabel: validateTargetName,
}, &c) }, &c)
@@ -597,7 +602,7 @@ func (c Config) newOverrides(v []string) (map[string]map[string]Override, error)
if len(keys) != 3 { if len(keys) != 3 {
return nil, errors.Errorf("invalid key %s, args requires name", parts[0]) return nil, errors.Errorf("invalid key %s, args requires name", parts[0])
} }
if len(parts) < 2 { if len(parts) < 2 && envLookupAllowed() {
v, ok := os.LookupEnv(keys[2]) v, ok := os.LookupEnv(keys[2])
if !ok { if !ok {
continue continue
@@ -1728,3 +1733,13 @@ func parseArrValue[T any, PT arrValue[T]](s []string) ([]*T, error) {
} }
return outputs, nil return outputs, nil
} }
var envLookupAllowed = sync.OnceValue(func() bool {
if v, ok := os.LookupEnv("BUILDX_BAKE_DISABLE_VARS_ENV_LOOKUP"); ok {
disable, err := strconv.ParseBool(v)
if err == nil && disable {
return false
}
}
return true
})
+5 -1
View File
@@ -304,7 +304,11 @@ func validateCompose(dt []byte, envs map[string]string) error {
} }
func composeEnv() (map[string]string, error) { func composeEnv() (map[string]string, error) {
envs := sliceToMap(os.Environ()) var env []string
if envLookupAllowed() {
env = os.Environ()
}
envs := sliceToMap(env)
if wd, err := os.Getwd(); err == nil { if wd, err := os.Getwd(); err == nil {
envs, err = loadDotEnv(envs, wd) envs, err = loadDotEnv(envs, wd)
if err != nil { if err != nil {