From 68b49403a35891f4ed8c6c48632eed910482f980 Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Mon, 30 Jun 2025 11:38:43 +0200 Subject: [PATCH] compose: evaluate vars from current env in dotenv file Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- bake/compose.go | 5 ++++- bake/compose_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/bake/compose.go b/bake/compose.go index 8151cc46f..923b661fd 100644 --- a/bake/compose.go +++ b/bake/compose.go @@ -277,7 +277,10 @@ func loadDotEnv(curenv map[string]string, workingDir string) (map[string]string, return nil, err } - envs, err := dotenv.UnmarshalBytesWithLookup(dt, nil) + envs, err := dotenv.UnmarshalBytesWithLookup(dt, func(k string) (string, bool) { + v, ok := curenv[k] + return v, ok + }) if err != nil { return nil, err } diff --git a/bake/compose_test.go b/bake/compose_test.go index 6c04b7f5c..7d7a2cc74 100644 --- a/bake/compose_test.go +++ b/bake/compose_test.go @@ -862,6 +862,32 @@ services: require.NoError(t, err) } +func TestDotEnvEvaluate(t *testing.T) { + tmpdir := t.TempDir() + + err := os.WriteFile(filepath.Join(tmpdir, ".env"), []byte(` +TEST_VALUE=${SYSTEM_VALUE:?system_value_not_set} +FOO_VALUE=${TEST_VALUE:?test_value_not_set} +`), 0644) + require.NoError(t, err) + + dt := []byte(` +services: + test: + build: + args: + TEST_VALUE: + FOO_VALUE: +`) + + t.Setenv("SYSTEM_VALUE", "abc") + + chdir(t, tmpdir) + c, err := ParseComposeFiles([]File{{Name: "compose.yml", Data: dt}}) + require.NoError(t, err) + require.Equal(t, map[string]*string{"TEST_VALUE": ptrstr("abc"), "FOO_VALUE": ptrstr("abc")}, c.Targets[0].Args) +} + // chdir changes the current working directory to the named directory, // and then restore the original working directory at the end of the test. func chdir(t *testing.T, dir string) {