1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-19 13:29:41 +02:00

fix(libstack): fix environment variable handling in compose BE- (#165)

This commit is contained in:
andres-portainer 2024-11-26 17:37:22 -03:00 committed by GitHub
parent ee0dbf2d22
commit 13143bc7ea
2 changed files with 41 additions and 17 deletions

View file

@ -72,25 +72,18 @@ func withComposeService(
configDetails := types.ConfigDetails{ configDetails := types.ConfigDetails{
WorkingDir: options.WorkingDir, WorkingDir: options.WorkingDir,
Environment: make(map[string]string),
} }
for _, p := range filePaths { for _, p := range filePaths {
configDetails.ConfigFiles = append(configDetails.ConfigFiles, types.ConfigFile{Filename: p}) configDetails.ConfigFiles = append(configDetails.ConfigFiles, types.ConfigFile{Filename: p})
} }
envFile := make(map[string]string) env, err := parseEnvironment(options)
if options.EnvFilePath != "" {
env, err := dotenv.GetEnvFromFile(make(map[string]string), []string{options.EnvFilePath})
if err != nil { if err != nil {
return fmt.Errorf("unable to get the environment from the env file: %w", err) return err
} }
maps.Copy(envFile, env)
configDetails.Environment = env configDetails.Environment = env
}
if len(configDetails.ConfigFiles) == 0 { if len(configDetails.ConfigFiles) == 0 {
return composeFn(composeService, nil) return composeFn(composeService, nil)
@ -263,3 +256,29 @@ func addServiceLabels(project *types.Project, oneOff bool) {
project.Services[i] = s project.Services[i] = s
} }
} }
func parseEnvironment(options libstack.Options) (map[string]string, error) {
env := make(map[string]string)
for _, envLine := range options.Env {
e, err := dotenv.UnmarshalWithLookup(envLine, nil)
if err != nil {
return nil, fmt.Errorf("unable to parse environment variables: %w", err)
}
maps.Copy(env, e)
}
if options.EnvFilePath == "" {
return env, nil
}
e, err := dotenv.GetEnvFromFile(make(map[string]string), []string{options.EnvFilePath})
if err != nil {
return nil, fmt.Errorf("unable to get the environment from the env file: %w", err)
}
maps.Copy(env, e)
return env, nil
}

View file

@ -142,6 +142,7 @@ func Test_Config(t *testing.T) {
composeFileContent string composeFileContent string
expectFileContent string expectFileContent string
envFileContent string envFileContent string
env []string
}{ }{
{ {
name: "compose file with relative path", name: "compose file with relative path",
@ -173,7 +174,6 @@ networks:
default: default:
name: configtest_default name: configtest_default
`, `,
envFileContent: "",
}, },
{ {
name: "compose file with absolute path", name: "compose file with absolute path",
@ -205,7 +205,6 @@ networks:
default: default:
name: configtest_default name: configtest_default
`, `,
envFileContent: "",
}, },
{ {
name: "compose file with declared volume", name: "compose file with declared volume",
@ -243,7 +242,6 @@ volumes:
name: configtest_nginx-data name: configtest_nginx-data
driver: local driver: local
`, `,
envFileContent: "",
}, },
{ {
name: "compose file with relative path environment variable placeholder", name: "compose file with relative path environment variable placeholder",
@ -254,6 +252,7 @@ volumes:
- 8019:80 - 8019:80
volumes: volumes:
- ${WEB_HOME}:/usr/share/nginx/html/ - ${WEB_HOME}:/usr/share/nginx/html/
- ./config/${CONFIG_DIR}:/tmp/config
env_file: env_file:
- stack.env - stack.env
`, `,
@ -276,11 +275,17 @@ services:
target: /usr/share/nginx/html target: /usr/share/nginx/html
bind: bind:
create_host_path: true create_host_path: true
- type: bind
source: ./config/something
target: /tmp/config
bind:
create_host_path: true
networks: networks:
default: default:
name: configtest_default name: configtest_default
`, `,
envFileContent: `WEB_HOME=./html`, envFileContent: `WEB_HOME=./html`,
env: []string{"CONFIG_DIR=something"},
}, },
{ {
name: "compose file with absolute path environment variable placeholder", name: "compose file with absolute path environment variable placeholder",
@ -294,7 +299,6 @@ networks:
env_file: env_file:
- stack.env - stack.env
`, `,
expectFileContent: `name: configtest expectFileContent: `name: configtest
services: services:
nginx: nginx:
@ -336,6 +340,7 @@ networks:
WorkingDir: dir, WorkingDir: dir,
ProjectName: projectName, ProjectName: projectName,
EnvFilePath: envFilePath, EnvFilePath: envFilePath,
Env: tc.env,
ConfigOptions: []string{"--no-path-resolution"}, ConfigOptions: []string{"--no-path-resolution"},
}) })
require.NoError(t, err) require.NoError(t, err)