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

fix(libstack): miss to read default .env file [BE-11638] (#458)

This commit is contained in:
Oscar Zhou 2025-02-26 13:00:25 +13:00 committed by GitHub
parent cc73b7831f
commit dd98097897

View file

@ -5,6 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"maps" "maps"
"os"
"path/filepath" "path/filepath"
"slices" "slices"
"strconv" "strconv"
@ -87,7 +88,7 @@ func withComposeService(
return composeFn(composeService, nil) return composeFn(composeService, nil)
} }
env, err := parseEnvironment(options) env, err := parseEnvironment(options, filePaths)
if err != nil { if err != nil {
return err return err
} }
@ -326,7 +327,7 @@ func addServiceLabels(project *types.Project, oneOff bool, edgeStackID portainer
} }
} }
func parseEnvironment(options libstack.Options) (map[string]string, error) { func parseEnvironment(options libstack.Options, filePaths []string) (map[string]string, error) {
env := make(map[string]string) env := make(map[string]string)
for _, envLine := range options.Env { for _, envLine := range options.Env {
@ -339,9 +340,24 @@ func parseEnvironment(options libstack.Options) (map[string]string, error) {
} }
if options.EnvFilePath == "" { if options.EnvFilePath == "" {
if len(filePaths) == 0 {
return env, nil return env, nil
} }
defaultDotEnv := filepath.Join(filepath.Dir(filePaths[0]), ".env")
s, err := os.Stat(defaultDotEnv)
if os.IsNotExist(err) {
return env, nil
}
if err != nil {
return env, err
}
if s.IsDir() {
return env, nil
}
options.EnvFilePath = defaultDotEnv
}
e, err := dotenv.GetEnvFromFile(make(map[string]string), []string{options.EnvFilePath}) e, err := dotenv.GetEnvFromFile(make(map[string]string), []string{options.EnvFilePath})
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to get the environment from the env file: %w", err) return nil, fmt.Errorf("unable to get the environment from the env file: %w", err)