Skip to content

Commit

Permalink
add support of COMPOSE_ENV_FILES env variable to pass a list of env f…
Browse files Browse the repository at this point in the history
…iles

Signed-off-by: Guillaume Lours <[email protected]>
  • Loading branch information
glours authored and ndeloof committed Oct 5, 2023
1 parent 2c0b023 commit 9dd081b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cmd/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ const (
ComposeRemoveOrphans = "COMPOSE_REMOVE_ORPHANS"
// ComposeIgnoreOrphans ignore "orphaned" containers
ComposeIgnoreOrphans = "COMPOSE_IGNORE_ORPHANS"
// ComposeEnvFiles defines the env files to use if --env-file isn't used
ComposeEnvFiles = "COMPOSE_ENV_FILES"
)

// Command defines a compose CLI command as a func with args
Expand Down Expand Up @@ -517,6 +519,11 @@ func RootCommand(dockerCli command.Cli, backend api.Service) *cobra.Command { //
}

func setEnvWithDotEnv(prjOpts *ProjectOptions) error {
if len(prjOpts.EnvFiles) == 0 {
if envFiles := os.Getenv(ComposeEnvFiles); envFiles != "" {
prjOpts.EnvFiles = strings.Split(envFiles, ",")
}
}
options, err := prjOpts.toProjectOptions()
if err != nil {
return compose.WrapComposeError(err)
Expand Down
14 changes: 14 additions & 0 deletions pkg/e2e/compose_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ func TestEnvPriority(t *testing.T) {
assert.Equal(t, strings.TrimSpace(res.Stdout()), "EnvFileDefaultValue")
})

// No Compose file, all other options with env variable from OS environment
// 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected (From environment default value from file in COMPOSE_ENV_FILES)
// 2. Compose File (service::environment section)
// 3. Compose File (service::env_file section file)
// 4. Container Image ENV directive
// 5. Variable is not defined
t.Run("shell priority from COMPOSE_ENV_FILES variable", func(t *testing.T) {
cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose.yaml",
"run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
cmd.Env = append(cmd.Env, "COMPOSE_ENV_FILES=./fixtures/environment/env-priority/.env.override.with.default")
res := icmd.RunCmd(cmd)
assert.Equal(t, strings.TrimSpace(res.Stdout()), "EnvFileDefaultValue")
})

// No Compose file and env variable pass to the run command
// 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected
// 2. Compose File (service::environment section)
Expand Down

0 comments on commit 9dd081b

Please sign in to comment.