Skip to content

Commit 2812edd

Browse files
fix: include env interpolation in compose includes
1 parent ddb94f1 commit 2812edd

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

loader/include.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,16 @@ func ApplyInclude(ctx context.Context, workingDir string, environment types.Mapp
141141
return err
142142
}
143143

144+
// Make env vars from the include's env_file available to the parent
145+
// environment so they are present later when service-level env_files
146+
// are parsed during project resolution.
147+
for k, v := range envFromFile {
148+
if environment == nil {
149+
environment = types.Mapping{}
150+
}
151+
environment[k] = v
152+
}
153+
144154
config := types.ConfigDetails{
145155
WorkingDir: relworkingdir,
146156
ConfigFiles: types.ToConfigFiles(r.Path),

loader/include_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,48 @@ services:
231231
assert.Equal(t, p.Services["included"].Image, "alpine")
232232
}
233233

234+
func TestIncludeEnvFileInterpolation(t *testing.T) {
235+
fileName := "compose.yml"
236+
tmpdir := t.TempDir()
237+
// top-level include with env_file
238+
yaml := `
239+
include:
240+
- path: ./subproj/subcompose.yml
241+
env_file:
242+
- ./values.env
243+
`
244+
createFile(t, tmpdir, yaml, fileName)
245+
246+
yaml = `
247+
services:
248+
app:
249+
env_file: subvalues.env
250+
image: helloworld
251+
`
252+
createFileSubDir(t, tmpdir, "subproj", yaml, "subcompose.yml")
253+
254+
// env file inside included project references VAR from top-level include env_file
255+
createFileSubDir(t, tmpdir, "subproj", "MYVAR=${VAR?}", "subvalues.env")
256+
createFile(t, tmpdir, "VAR=1", "values.env")
257+
258+
p, err := LoadWithContext(context.TODO(), types.ConfigDetails{
259+
WorkingDir: tmpdir,
260+
ConfigFiles: []types.ConfigFile{{
261+
Filename: filepath.Join(tmpdir, fileName),
262+
}},
263+
Environment: nil,
264+
}, func(options *Options) {
265+
options.SkipNormalization = true
266+
options.ResolvePaths = true
267+
options.SetProjectName("project", true)
268+
})
269+
assert.NilError(t, err)
270+
app := p.Services["app"]
271+
// After resolution, MYVAR should be present and equal to 1
272+
assert.Check(t, app.Environment["MYVAR"] != nil)
273+
assert.Equal(t, *app.Environment["MYVAR"], "1")
274+
}
275+
234276
func createFile(t *testing.T, rootDir, content, fileName string) string {
235277
path := filepath.Join(rootDir, fileName)
236278
assert.NilError(t, os.WriteFile(path, []byte(content), 0o600))

0 commit comments

Comments
 (0)