mirror of https://github.com/portainer/portainer
fix(libstack): fix environment variable handling in compose BE- (#165)
parent
ee0dbf2d22
commit
13143bc7ea
|
@ -71,27 +71,20 @@ func withComposeService(
|
|||
composeService := compose.NewComposeService(cli)
|
||||
|
||||
configDetails := types.ConfigDetails{
|
||||
WorkingDir: options.WorkingDir,
|
||||
Environment: make(map[string]string),
|
||||
WorkingDir: options.WorkingDir,
|
||||
}
|
||||
|
||||
for _, p := range filePaths {
|
||||
configDetails.ConfigFiles = append(configDetails.ConfigFiles, types.ConfigFile{Filename: p})
|
||||
}
|
||||
|
||||
envFile := make(map[string]string)
|
||||
|
||||
if options.EnvFilePath != "" {
|
||||
env, err := dotenv.GetEnvFromFile(make(map[string]string), []string{options.EnvFilePath})
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to get the environment from the env file: %w", err)
|
||||
}
|
||||
|
||||
maps.Copy(envFile, env)
|
||||
|
||||
configDetails.Environment = env
|
||||
env, err := parseEnvironment(options)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
configDetails.Environment = env
|
||||
|
||||
if len(configDetails.ConfigFiles) == 0 {
|
||||
return composeFn(composeService, nil)
|
||||
}
|
||||
|
@ -263,3 +256,29 @@ func addServiceLabels(project *types.Project, oneOff bool) {
|
|||
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
|
||||
}
|
||||
|
|
|
@ -142,6 +142,7 @@ func Test_Config(t *testing.T) {
|
|||
composeFileContent string
|
||||
expectFileContent string
|
||||
envFileContent string
|
||||
env []string
|
||||
}{
|
||||
{
|
||||
name: "compose file with relative path",
|
||||
|
@ -173,7 +174,6 @@ networks:
|
|||
default:
|
||||
name: configtest_default
|
||||
`,
|
||||
envFileContent: "",
|
||||
},
|
||||
{
|
||||
name: "compose file with absolute path",
|
||||
|
@ -205,7 +205,6 @@ networks:
|
|||
default:
|
||||
name: configtest_default
|
||||
`,
|
||||
envFileContent: "",
|
||||
},
|
||||
{
|
||||
name: "compose file with declared volume",
|
||||
|
@ -243,7 +242,6 @@ volumes:
|
|||
name: configtest_nginx-data
|
||||
driver: local
|
||||
`,
|
||||
envFileContent: "",
|
||||
},
|
||||
{
|
||||
name: "compose file with relative path environment variable placeholder",
|
||||
|
@ -254,6 +252,7 @@ volumes:
|
|||
- 8019:80
|
||||
volumes:
|
||||
- ${WEB_HOME}:/usr/share/nginx/html/
|
||||
- ./config/${CONFIG_DIR}:/tmp/config
|
||||
env_file:
|
||||
- stack.env
|
||||
`,
|
||||
|
@ -276,11 +275,17 @@ services:
|
|||
target: /usr/share/nginx/html
|
||||
bind:
|
||||
create_host_path: true
|
||||
- type: bind
|
||||
source: ./config/something
|
||||
target: /tmp/config
|
||||
bind:
|
||||
create_host_path: true
|
||||
networks:
|
||||
default:
|
||||
name: configtest_default
|
||||
`,
|
||||
envFileContent: `WEB_HOME=./html`,
|
||||
env: []string{"CONFIG_DIR=something"},
|
||||
},
|
||||
{
|
||||
name: "compose file with absolute path environment variable placeholder",
|
||||
|
@ -294,7 +299,6 @@ networks:
|
|||
env_file:
|
||||
- stack.env
|
||||
`,
|
||||
|
||||
expectFileContent: `name: configtest
|
||||
services:
|
||||
nginx:
|
||||
|
@ -336,6 +340,7 @@ networks:
|
|||
WorkingDir: dir,
|
||||
ProjectName: projectName,
|
||||
EnvFilePath: envFilePath,
|
||||
Env: tc.env,
|
||||
ConfigOptions: []string{"--no-path-resolution"},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
|
Loading…
Reference in New Issue