Skip to content

Commit 68b4940

Browse files
committed
compose: evaluate vars from current env in dotenv file
Signed-off-by: CrazyMax <[email protected]>
1 parent f6c730e commit 68b4940

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

bake/compose.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,10 @@ func loadDotEnv(curenv map[string]string, workingDir string) (map[string]string,
277277
return nil, err
278278
}
279279

280-
envs, err := dotenv.UnmarshalBytesWithLookup(dt, nil)
280+
envs, err := dotenv.UnmarshalBytesWithLookup(dt, func(k string) (string, bool) {
281+
v, ok := curenv[k]
282+
return v, ok
283+
})
281284
if err != nil {
282285
return nil, err
283286
}

bake/compose_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,32 @@ services:
862862
require.NoError(t, err)
863863
}
864864

865+
func TestDotEnvEvaluate(t *testing.T) {
866+
tmpdir := t.TempDir()
867+
868+
err := os.WriteFile(filepath.Join(tmpdir, ".env"), []byte(`
869+
TEST_VALUE=${SYSTEM_VALUE:?system_value_not_set}
870+
FOO_VALUE=${TEST_VALUE:?test_value_not_set}
871+
`), 0644)
872+
require.NoError(t, err)
873+
874+
dt := []byte(`
875+
services:
876+
test:
877+
build:
878+
args:
879+
TEST_VALUE:
880+
FOO_VALUE:
881+
`)
882+
883+
t.Setenv("SYSTEM_VALUE", "abc")
884+
885+
chdir(t, tmpdir)
886+
c, err := ParseComposeFiles([]File{{Name: "compose.yml", Data: dt}})
887+
require.NoError(t, err)
888+
require.Equal(t, map[string]*string{"TEST_VALUE": ptrstr("abc"), "FOO_VALUE": ptrstr("abc")}, c.Targets[0].Args)
889+
}
890+
865891
// chdir changes the current working directory to the named directory,
866892
// and then restore the original working directory at the end of the test.
867893
func chdir(t *testing.T, dir string) {

0 commit comments

Comments
 (0)