compose: evaluate vars from current env in dotenv file
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
+4
-1
@@ -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
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user