Merge pull request #3276 from crazy-max/compose-dotenv-evaluate

compose: evaluate vars from current env in dotenv file
This commit is contained in:
CrazyMax
2025-07-01 10:13:54 +02:00
committed by GitHub
2 changed files with 30 additions and 1 deletions
+4 -1
View File
@@ -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
}
+26
View File
@@ -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) {