Skip to content

Modern Docker Management, Designed for Everyone

feat: global environment variables (#620)

Chase Baucomf216e7bb1ff5
diff --git a/backend/.air.toml b/backend/.air.toml
index bea86ec95..c9f853ee2 100644
--- a/backend/.air.toml
+++ b/backend/.air.toml
@@ -3,7 +3,7 @@ tmp_dir = ".bin"
[build]
bin = "./.bin/arcane"
- cmd = "CGO_ENABLED=0 go build -tags 'exclude_frontend' -ldflags='-buildid=' -o ./.bin/arcane ./cmd"
+ cmd = "CGO_ENABLED=1 go build -tags 'exclude_frontend' -ldflags='-buildid=' -o ./.bin/arcane ./cmd"
exclude_dir = [".bin", "data", "node_modules", ".git"]
stop_on_error = true
delay = 500
diff --git a/backend/internal/api/template_handler.go b/backend/internal/api/template_handler.go
index 4e4eb9765..2b63ed937 100644
--- a/backend/internal/api/template_handler.go
+++ b/backend/internal/api/template_handler.go
@@ -38,6 +38,8 @@ func NewTemplateHandler(group *gin.RouterGroup, templateService *services.Templa
apiGroup.POST("/registries", handler.CreateRegistry)
apiGroup.PUT("/registries/:id", handler.UpdateRegistry)
apiGroup.DELETE("/registries/:id", handler.DeleteRegistry)
+ apiGroup.GET("/variables", handler.GetGlobalVariables)
+ apiGroup.PUT("/variables", handler.UpdateGlobalVariables)
}
}
@@ -575,3 +577,45 @@ func (h *TemplateHandler) DownloadTemplate(c *gin.Context) {
"data": out,
})
}
+
+func (h *TemplateHandler) GetGlobalVariables(c *gin.Context) {
+ vars, err := h.templateService.GetGlobalVariables(c.Request.Context())
+ if err != nil {
+ c.JSON(http.StatusInternalServerError, gin.H{
+ "success": false,
+ "data": gin.H{"error": "Failed to retrieve global variables: " + err.Error()},
+ })
+ return
+ }
+
+ c.JSON(http.StatusOK, gin.H{
+ "success": true,
+ "data": vars,
+ })
+}
+
+func (h *TemplateHandler) UpdateGlobalVariables(c *gin.Context) {
+ var req dto.UpdateVariablesRequest
+ if err := c.ShouldBindJSON(&req); err != nil {
+ c.JSON(http.StatusBadRequest, gin.H{
+ "success": false,
+ "data": gin.H{"error": "Invalid request format: " + err.Error()},
+ })
+ return
+ }
+
+ if err := h.templateService.UpdateGlobalVariables(c.Request.Context(), req.Variables); err != nil {
+ c.JSON(http.StatusInternalServerError, gin.H{
+ "success": false,
+ "data": gin.H{"error": "Failed to update global variables: " + err.Error()},
+ })
+ return
+ }
+
+ c.JSON(http.StatusOK, gin.H{
+ "success": true,
+ "data": gin.H{
+ "message": "Global variables updated successfully",
+ },
+ })
+}
diff --git a/backend/internal/bootstrap/services_bootstrap.go b/backend/internal/bootstrap/services_bootstrap.go
index 5e417dc85..53a1d3715 100644
--- a/backend/internal/bootstrap/services_bootstrap.go
+++ b/backend/internal/bootstrap/services_bootstrap.go
@@ -50,7 +50,7 @@ func initializeServices(ctx context.Context, db *database.DB, cfg *config.Config
svcs.Container = services.NewContainerService(db, svcs.Event, svcs.Docker)
svcs.Volume = services.NewVolumeService(db, svcs.Docker, svcs.Event)
svcs.Network = services.NewNetworkService(db, svcs.Docker, svcs.Event)
- svcs.Template = services.NewTemplateService(ctx, db, httpClient)
+ svcs.Template = services.NewTemplateService(ctx, db, httpClient, svcs.Settings)
svcs.Auth = services.NewAuthService(svcs.User, svcs.Settings, svcs.Event, cfg.JWTSecret, cfg)
svcs.Oidc = services.NewOidcService(svcs.Auth, cfg, httpClient)
svcs.Updater = services.NewUpdaterService(db, svcs.Settings, svcs.Docker, svcs.Project, svcs.ImageUpdate, svcs.ContainerRegistry, svcs.Event, svcs.Image)
diff --git a/backend/internal/dto/variable_dto.go b/backend/internal/dto/variable_dto.go
new file mode 100644
index 000000000..f86e2db28
--- /dev/null
+++ b/backend/internal/dto/variable_dto.go
@@ -0,0 +1,14 @@
+package dto
+
+type VariableDto struct {
+ Key string `json:"key"`
+ Value string `json:"value"`
+}
+
+type GetVariablesResponse struct {
+ Variables []VariableDto `json:"variables"`
+}
+
+type UpdateVariablesRequest struct {
+ Variables []VariableDto `json:"variables"`
+}
diff --git a/backend/internal/services/project_service.go b/backend/internal/services/project_service.go
index 6f00e473d..7c898708c 100644
--- a/backend/internal/services/project_service.go
+++ b/backend/internal/services/project_service.go
@@ -120,7 +120,15 @@ func (s *ProjectService) GetProjectServices(ctx context.Context, projectID strin
return []ProjectServiceInfo{}, fmt.Errorf("no compose file found in project directory: %s", projectFromDb.Path)
}
- project, loadErr := projects.LoadComposeProject(ctx, composeFileFullPath, projectFromDb.Name)
+ // Get configured projects directory from settings
+ projectsDirSetting := s.settingsService.GetStringSetting(ctx, "projectsDirectory", "data/projects")
+ projectsDirectory, pdErr := fs.GetProjectsDirectory(ctx, strings.TrimSpace(projectsDirSetting))
+ if pdErr != nil {
+ slog.WarnContext(ctx, "unable to determine projects directory; using default", "error", pdErr)
+ projectsDirectory = "data/projects"
+ }
+
+ project, loadErr := projects.LoadComposeProject(ctx, composeFileFullPath, projectFromDb.Name, projectsDirectory)
if loadErr != nil {
return []ProjectServiceInfo{}, fmt.Errorf("failed to load compose project from %s: %w", projectFromDb.Path, loadErr)
}
@@ -425,7 +433,15 @@ func (s *ProjectService) DeployProject(ctx context.Context, projectID string, us
return fmt.Errorf("no compose file found in project directory: %s", projectFromDb.Path)
}
- project, loadErr := projects.LoadComposeProject(ctx, composeFileFullPath, projectFromDb.Name)
+ // Get configured projects directory from settings
+ projectsDirSetting := s.settingsService.GetStringSetting(ctx, "projectsDirectory", "data/projects")
+ projectsDirectory, pdErr := fs.GetProjectsDirectory(ctx, strings.TrimSpace(projectsDirSetting))
+ if pdErr != nil {
+ slog.WarnContext(ctx, "unable to determine projects directory; using default", "error", pdErr)
+ projectsDirectory = "data/projects"
+ }
+
+ project, loadErr := projects.LoadComposeProject(ctx, composeFileFullPath, projectFromDb.Name, projectsDirectory)
if loadErr != nil {
return fmt.Errorf("failed to load compose project from %s: %w", projectFromDb.Path, loadErr)
}
@@ -465,7 +481,15 @@ func (s *ProjectService) DownProject(ctx context.Context, projectID string, user
return err
}
- proj, _, lerr := projects.LoadComposeProjectFromDir(ctx, projectFromDb.Path, projectFromDb.Name)
+ // Get configured projects directory from settings
+ projectsDirSetting := s.settingsService.GetStringSetting(ctx, "projectsDirectory", "data/projects")
+ projectsDirectory, pdErr := fs.GetProjectsDirectory(ctx, strings.TrimSpace(projectsDirSetting))
+ if pdErr != nil {
+ slog.WarnContext(ctx, "unable to determine projects directory; using default", "error", pdErr)
+ projectsDirectory = "data/projects"
+ }
+
+ proj, _, lerr := projects.LoadComposeProjectFromDir(ctx, projectFromDb.Path, projectFromDb.Name, projectsDirectory)
if lerr != nil {
_ = s.updateProjectStatusInternal(ctx, projectID, models.ProjectStatusRunning)
return fmt.Errorf("failed to load compose project: %w", lerr)
@@ -543,7 +567,15 @@ func (s *ProjectService) DestroyProject(ctx context.Context, projectID string, r
}
if removeVolumes {
- if compProj, _, lerr := projects.LoadComposeProjectFromDir(ctx, proj.Path, proj.Name); lerr == nil {
+ // Get configured projects directory from settings
+ projectsDirSetting := s.settingsService.GetStringSetting(ctx, "projectsDirectory", "data/projects")
+ projectsDirectory, pdErr := fs.GetProjectsDirectory(ctx, strings.TrimSpace(projectsDirSetting))
+ if pdErr != nil {
+ slog.WarnContext(ctx, "unable to determine projects directory; using default", "error", pdErr)
+ projectsDirectory = "data/projects"
+ }
+
+ if compProj, _, lerr := projects.LoadComposeProjectFromDir(ctx, proj.Path, proj.Name, projectsDirectory); lerr == nil {
if derr := projects.ComposeDown(ctx, compProj, true); derr != nil {
slog.WarnContext(ctx, "failed to remove volumes", "error", derr)
}
@@ -598,7 +630,15 @@ func (s *ProjectService) PullProjectImages(ctx context.Context, projectID string
return err
}
- compProj, _, lerr := projects.LoadComposeProjectFromDir(ctx, proj.Path, proj.Name)
+ // Get configured projects directory from settings
+ projectsDirSetting := s.settingsService.GetStringSetting(ctx, "projectsDirectory", "data/projects")
+ projectsDirectory, pdErr := fs.GetProjectsDirectory(ctx, strings.TrimSpace(projectsDirSetting))
+ if pdErr != nil {
+ slog.WarnContext(ctx, "unable to determine projects directory; using default", "error", pdErr)
+ projectsDirectory = "data/projects"
+ }
+
+ compProj, _, lerr := projects.LoadComposeProjectFromDir(ctx, proj.Path, proj.Name, projectsDirectory)
if lerr != nil {
return fmt.Errorf("failed to load compose project: %w", lerr)
}
@@ -630,7 +670,15 @@ func (s *ProjectService) RestartProject(ctx context.Context, projectID string, u
return fmt.Errorf("failed to update project status to restarting: %w", err)
}
- compProj, _, lerr := projects.LoadComposeProjectFromDir(ctx, proj.Path, proj.Name)
+ // Get configured projects directory from settings
+ projectsDirSetting := s.settingsService.GetStringSetting(ctx, "projectsDirectory", "data/projects")
+ projectsDirectory, pdErr := fs.GetProjectsDirectory(ctx, strings.TrimSpace(projectsDirSetting))
+ if pdErr != nil {
+ slog.WarnContext(ctx, "unable to determine projects directory; using default", "error", pdErr)
+ projectsDirectory = "data/projects"
+ }
+
+ compProj, _, lerr := projects.LoadComposeProjectFromDir(ctx, proj.Path, proj.Name, projectsDirectory)
if lerr != nil {
_ = s.updateProjectStatusInternal(ctx, projectID, models.ProjectStatusRunning)
return fmt.Errorf("failed to load compose project: %w", lerr)
@@ -775,7 +823,13 @@ func (s *ProjectService) ListProjects(ctx context.Context, params pagination.Que
} else if displayServiceCount == 0 {
// Fallback: try to detect service count from compose file
if _, derr := projects.DetectComposeFile(project.Path); derr == nil {
- if proj, _, perr := projects.LoadComposeProjectFromDir(ctx, project.Path, project.Name); perr == nil {
+ // Get configured projects directory from settings
+ projectsDirSetting := s.settingsService.GetStringSetting(ctx, "projectsDirectory", "data/projects")
+ projectsDirectory, pdErr := fs.GetProjectsDirectory(ctx, strings.TrimSpace(projectsDirSetting))
+ if pdErr != nil {
+ projectsDirectory = "data/projects"
+ }
+ if proj, _, perr := projects.LoadComposeProjectFromDir(ctx, project.Path, project.Name, projectsDirectory); perr == nil {
displayServiceCount = len(proj.Services)
}
}
diff --git a/backend/internal/services/template_service.go b/backend/internal/services/template_service.go
index cddb9bb1e..910e1009c 100644
--- a/backend/internal/services/template_service.go
+++ b/backend/internal/services/template_service.go
@@ -1,6 +1,7 @@
package services
import (
+ "bufio"
"context"
"encoding/json"
"errors"
@@ -10,6 +11,7 @@ import (
"net/http"
"os"
"path/filepath"
+ "sort"
"strings"
"sync"
"time"
@@ -33,8 +35,9 @@ type registryFetchMeta struct {
}
type TemplateService struct {
- db *database.DB
- httpClient *http.Client
+ db *database.DB
+ httpClient *http.Client
+ settingsService *SettingsService
remoteMu sync.RWMutex
remoteCache remoteCache
@@ -51,7 +54,7 @@ func makeRemoteID(registryID, slug string) string {
return fmt.Sprintf("%s:%s:%s", remoteIDPrefix, registryID, slug)
}
-func NewTemplateService(ctx context.Context, db *database.DB, httpClient *http.Client) *TemplateService {
+func NewTemplateService(ctx context.Context, db *database.DB, httpClient *http.Client, settingsService *SettingsService) *TemplateService {
if httpClient == nil {
httpClient = http.DefaultClient
}
@@ -63,6 +66,7 @@ func NewTemplateService(ctx context.Context, db *database.DB, httpClient *http.C
return &TemplateService{
db: db,
httpClient: httpClient,
+ settingsService: settingsService,
remoteCache: remoteCache{},
registryFetchMeta: make(map[string]*registryFetchMeta),
}
@@ -724,3 +728,124 @@ func (s *TemplateService) syncFilesystemTemplatesInternal(ctx context.Context) e
}
return nil
}
+
+func (s *TemplateService) getGlobalVariablesPath(ctx context.Context) (string, error) {
+ projectsDirectory, err := appfs.GetProjectsDirectory(ctx, s.settingsService.GetStringSetting(ctx, "projectsDirectory", "data/projects"))
+ if err != nil {
+ return "", fmt.Errorf("failed to get projects directory: %w", err)
+ }
+
+ return filepath.Join(projectsDirectory, ".env.global"), nil
+}
+
+func (s *TemplateService) GetGlobalVariables(ctx context.Context) ([]dto.VariableDto, error) {
+ envPath, err := s.getGlobalVariablesPath(ctx)
+ if err != nil {
+ return nil, err
+ }
+
+ if _, err := os.Stat(envPath); os.IsNotExist(err) {
+ slog.DebugContext(ctx, "Global variables file does not exist yet", "path", envPath)
+ return []dto.VariableDto{}, nil
+ }
+
+ file, err := os.Open(envPath)
+ if err != nil {
+ return nil, fmt.Errorf("failed to open global variables file: %w", err)
+ }
+ defer file.Close()
+
+ vars := []dto.VariableDto{}
+ scanner := bufio.NewScanner(file)
+ lineNum := 0
+
+ for scanner.Scan() {
+ lineNum++
+ line := strings.TrimSpace(scanner.Text())
+
+ if line == "" || strings.HasPrefix(line, "#") {
+ continue
+ }
+
+ parts := strings.SplitN(line, "=", 2)
+ if len(parts) != 2 {
+ slog.WarnContext(ctx, "Skipping invalid line in global variables file",
+ "line", lineNum,
+ "content", line)
+ continue
+ }
+
+ key := strings.TrimSpace(parts[0])
+ value := strings.TrimSpace(parts[1])
+
+ if len(value) >= 2 {
+ if (strings.HasPrefix(value, `"`) && strings.HasSuffix(value, `"`)) ||
+ (strings.HasPrefix(value, `'`) && strings.HasSuffix(value, `'`)) {
+ value = value[1 : len(value)-1]
+ }
+ }
+
+ vars = append(vars, dto.VariableDto{
+ Key: key,
+ Value: value,
+ })
+ }
+
+ if err := scanner.Err(); err != nil {
+ return nil, fmt.Errorf("error reading global variables file: %w", err)
+ }
+
+ sort.Slice(vars, func(i, j int) bool {
+ return vars[i].Key < vars[j].Key
+ })
+
+ return vars, nil
+}
+
+func (s *TemplateService) UpdateGlobalVariables(ctx context.Context, vars []dto.VariableDto) error {
+ envPath, err := s.getGlobalVariablesPath(ctx)
+ if err != nil {
+ return err
+ }
+
+ projectsDirectory := filepath.Dir(envPath)
+ if err := os.MkdirAll(projectsDirectory, 0755); err != nil {
+ return fmt.Errorf("failed to create projects directory: %w", err)
+ }
+
+ var builder strings.Builder
+ builder.WriteString("# Global Environment Variables\n")
+ builder.WriteString("# These variables are available to all projects\n")
+ builder.WriteString("# Last updated: " + time.Now().Format(time.RFC3339) + "\n\n")
+
+ sortedVars := make([]dto.VariableDto, len(vars))
+ copy(sortedVars, vars)
+ sort.Slice(sortedVars, func(i, j int) bool {
+ return sortedVars[i].Key < sortedVars[j].Key
+ })
+
+ for _, v := range sortedVars {
+ if strings.TrimSpace(v.Key) == "" {
+ continue
+ }
+
+ key := strings.TrimSpace(v.Key)
+ value := strings.TrimSpace(v.Value)
+
+ if strings.ContainsAny(value, " \t\n\r#") {
+ value = fmt.Sprintf(`"%s"`, strings.ReplaceAll(value, `"`, `\"`))
+ }
+
+ builder.WriteString(fmt.Sprintf("%s=%s\n", key, value))
+ }
+
+ if err := os.WriteFile(envPath, []byte(builder.String()), 0600); err != nil {
+ return fmt.Errorf("failed to write global variables file: %w", err)
+ }
+
+ slog.InfoContext(ctx, "Updated global variables",
+ "path", envPath,
+ "count", len(sortedVars))
+
+ return nil
+}
diff --git a/backend/internal/utils/projects/env.go b/backend/internal/utils/projects/env.go
new file mode 100644
index 000000000..4789537a0
--- /dev/null
+++ b/backend/internal/utils/projects/env.go
@@ -0,0 +1,213 @@
+package projects
+
+import (
+ "bufio"
+ "context"
+ "fmt"
+ "log/slog"
+ "os"
+ "path/filepath"
+ "strings"
+ "time"
+)
+
+const (
+ globalEnvFileName = ".env.global"
+ projectEnvFileName = ".env"
+ globalEnvHeader = `# Global Environment Variables
+# These variables are available to all projects
+# Created: %s
+
+`
+)
+
+type EnvMap map[string]string
+
+type EnvLoader struct {
+ projectsDir string
+ workdir string
+}
+
+func NewEnvLoader(projectsDir, workdir string) *EnvLoader {
+ return &EnvLoader{
+ projectsDir: projectsDir,
+ workdir: workdir,
+ }
+}
+
+// LoadEnvironment loads and merges environment variables from all sources:
+// 1. Process environment
+// 2. Global .env.global file (from projects directory)
+// 3. Project-specific .env file (from workdir)
+func (l *EnvLoader) LoadEnvironment(ctx context.Context) (envMap EnvMap, injectionVars EnvMap, err error) {
+ envMap = l.loadProcessEnv()
+ injectionVars = make(EnvMap)
+
+ globalEnvPath := filepath.Join(l.projectsDir, globalEnvFileName)
+ if err := l.ensureGlobalEnvFile(ctx, globalEnvPath); err != nil {
+ slog.WarnContext(ctx, "Failed to ensure global env file", "path", globalEnvPath, "error", err)
+ }
+
+ if err := l.loadAndMergeGlobalEnv(ctx, globalEnvPath, envMap, injectionVars); err != nil {
+ slog.WarnContext(ctx, "Failed to load global env", "path", globalEnvPath, "error", err)
+ }
+
+ projectEnvPath := filepath.Join(l.workdir, projectEnvFileName)
+ if err := l.loadAndMergeProjectEnv(ctx, projectEnvPath, envMap, injectionVars); err != nil {
+ slog.WarnContext(ctx, "Failed to load project env", "path", projectEnvPath, "error", err)
+ }
+
+ return envMap, injectionVars, nil
+}
+
+func (l *EnvLoader) loadProcessEnv() EnvMap {
+ envMap := make(EnvMap)
+ for _, kv := range os.Environ() {
+ if k, v, ok := strings.Cut(kv, "="); ok {
+ envMap[k] = v
+ }
+ }
+ return envMap
+}
+
+func (l *EnvLoader) ensureGlobalEnvFile(ctx context.Context, path string) error {
+ if _, err := os.Stat(path); os.IsNotExist(err) {
+ header := fmt.Sprintf(globalEnvHeader, time.Now().Format(time.RFC3339))
+ if werr := os.WriteFile(path, []byte(header), 0600); werr != nil {
+ return fmt.Errorf("write file: %w", werr)
+ }
+ slog.InfoContext(ctx, "Created global env file", "path", path)
+ } else if err != nil {
+ slog.DebugContext(ctx, "Could not stat global env file", "path", path, "error", err)
+ }
+ return nil
+}
+
+func (l *EnvLoader) loadAndMergeGlobalEnv(ctx context.Context, path string, envMap, injectionVars EnvMap) error {
+ slog.DebugContext(ctx, "Checking for global env file", "path", path)
+
+ info, err := os.Stat(path)
+ if err != nil {
+ if os.IsNotExist(err) {
+ slog.DebugContext(ctx, "Global env file does not exist", "path", path)
+ } else {
+ slog.DebugContext(ctx, "Global env file not accessible", "path", path, "error", err)
+ }
+ return err
+ }
+
+ if info.IsDir() {
+ return fmt.Errorf("path is a directory: %s", path)
+ }
+
+ slog.DebugContext(ctx, "Found global env file", "path", path)
+
+ globalEnv, err := parseEnvFile(path)
+ if err != nil {
+ return fmt.Errorf("parse env file: %w", err)
+ }
+
+ slog.DebugContext(ctx, "Read global env file", "count", len(globalEnv))
+
+ for k, v := range globalEnv {
+ if _, exists := envMap[k]; !exists {
+ envMap[k] = v
+ }
+ injectionVars[k] = v
+ }
+
+ slog.DebugContext(ctx, "Merged global env into environment map", "total_env_count", len(envMap))
+ return nil
+}
+
+func (l *EnvLoader) loadAndMergeProjectEnv(ctx context.Context, path string, envMap, injectionVars EnvMap) error {
+ slog.DebugContext(ctx, "Checking for project .env file", "path", path)
+
+ info, err := os.Stat(path)
+ if err != nil {
+ if os.IsNotExist(err) {
+ slog.DebugContext(ctx, "Project .env file does not exist", "path", path)
+ } else {
+ slog.DebugContext(ctx, "Project .env file not accessible", "path", path, "error", err)
+ }
+ return err
+ }
+
+ if info.IsDir() {
+ return fmt.Errorf("path is a directory: %s", path)
+ }
+
+ slog.DebugContext(ctx, "Found project .env file", "path", path)
+
+ projectEnv, err := parseEnvFile(path)
+ if err != nil {
+ return fmt.Errorf("parse env file: %w", err)
+ }
+
+ slog.DebugContext(ctx, "Read project .env file", "count", len(projectEnv))
+
+ for k, v := range projectEnv {
+ envMap[k] = v
+ injectionVars[k] = v
+ }
+
+ slog.DebugContext(ctx, "Merged project .env into environment map", "total_env_count", len(envMap))
+ return nil
+}
+
+func parseEnvFile(path string) (EnvMap, error) {
+ f, err := os.Open(path)
+ if err != nil {
+ return nil, fmt.Errorf("open file: %w", err)
+ }
+ defer f.Close()
+
+ envMap := make(EnvMap)
+ scanner := bufio.NewScanner(f)
+
+ for scanner.Scan() {
+ line := strings.TrimSpace(scanner.Text())
+
+ // Skip empty lines and comments
+ if line == "" || strings.HasPrefix(line, "#") {
+ continue
+ }
+
+ // Split on first '=' to get key and value
+ parts := strings.SplitN(line, "=", 2)
+ if len(parts) != 2 {
+ continue
+ }
+
+ key := strings.TrimSpace(parts[0])
+ value := strings.TrimSpace(parts[1])
+
+ // Strip surrounding quotes and unescape
+ value = stripQuotes(value)
+
+ envMap[key] = value
+ }
+
+ if err := scanner.Err(); err != nil {
+ return nil, fmt.Errorf("scan file: %w", err)
+ }
+
+ return envMap, nil
+}
+
+func stripQuotes(value string) string {
+ if len(value) < 2 {
+ return value
+ }
+
+ // Check for surrounding quotes (either double or single)
+ if (strings.HasPrefix(value, `"`) && strings.HasSuffix(value, `"`)) ||
+ (strings.HasPrefix(value, `'`) && strings.HasSuffix(value, `'`)) {
+ // Remove surrounding quotes
+ value = value[1 : len(value)-1]
+ // Unescape inner double-quotes
+ value = strings.ReplaceAll(value, `\"`, `"`)
+ }
+
+ return value
+}
diff --git a/backend/internal/utils/projects/load.go b/backend/internal/utils/projects/load.go
index f50fa2fe8..c0b71a346 100644
--- a/backend/internal/utils/projects/load.go
+++ b/backend/internal/utils/projects/load.go
@@ -3,14 +3,13 @@ package projects
import (
"context"
"fmt"
+ "log/slog"
"os"
"path/filepath"
- "strings"
"github.com/compose-spec/compose-go/v2/loader"
composetypes "github.com/compose-spec/compose-go/v2/types"
"github.com/docker/compose/v2/pkg/api"
- "github.com/joho/godotenv"
)
var ComposeFileCandidates = []string{
@@ -20,9 +19,9 @@ var ComposeFileCandidates = []string{
"docker-compose.yml",
}
-func locateComposeFile(projectsDir string) string {
+func locateComposeFile(dir string) string {
for _, filename := range ComposeFileCandidates {
- fullPath := filepath.Join(projectsDir, filename)
+ fullPath := filepath.Join(dir, filename)
if info, err := os.Stat(fullPath); err == nil && !info.IsDir() {
return fullPath
}
@@ -35,28 +34,21 @@ func DetectComposeFile(dir string) (string, error) {
if compose == "" {
return "", fmt.Errorf("no compose file found in %q", dir)
}
-
return compose, nil
}
-func LoadComposeProject(ctx context.Context, composeFile, projectName string) (*composetypes.Project, error) {
+func LoadComposeProject(ctx context.Context, composeFile, projectName, projectsDirectory string) (*composetypes.Project, error) {
workdir := filepath.Dir(composeFile)
- envFile := filepath.Join(workdir, ".env")
- envMap := map[string]string{}
- for _, kv := range os.Environ() {
- if k, v, ok := strings.Cut(kv, "="); ok {
- envMap[k] = v
- }
+ projectsDir := projectsDirectory
+ if projectsDir == "" {
+ projectsDir = filepath.Dir(workdir)
}
- if info, err := os.Stat(envFile); err == nil && !info.IsDir() {
- if fileEnv, rerr := godotenv.Read(envFile); rerr == nil {
- for k, v := range fileEnv {
- if _, exists := envMap[k]; !exists {
- envMap[k] = v
- }
- }
- }
+
+ envLoader := NewEnvLoader(projectsDir, workdir)
+ envMap, injectionVars, err := envLoader.LoadEnvironment(ctx)
+ if err != nil {
+ slog.WarnContext(ctx, "Failed to load environment", "error", err)
}
cfg := composetypes.ConfigDetails{
@@ -76,11 +68,30 @@ func LoadComposeProject(ctx context.Context, composeFile, projectName string) (*
project = project.WithoutUnnecessaryResources()
- // Ensure Compose discovery labels via CustomLabels
+ injectServiceConfiguration(project, injectionVars, workdir, composeFile)
+
+ project.ComposeFiles = []string{composeFile}
+ return project, nil
+}
+
+func injectServiceConfiguration(project *composetypes.Project, injectionVars EnvMap, workdir, composeFile string) {
for i, s := range project.Services {
+ // Initialize environment if nil
+ if s.Environment == nil {
+ s.Environment = make(composetypes.MappingWithEquals)
+ }
+
+ for k, v := range injectionVars {
+ if _, exists := s.Environment[k]; !exists {
+ vcopy := v
+ s.Environment[k] = &vcopy
+ }
+ }
+
if s.CustomLabels == nil {
s.CustomLabels = composetypes.Labels{}
}
+
s.CustomLabels[api.ProjectLabel] = project.Name
s.CustomLabels[api.ServiceLabel] = s.Name
s.CustomLabels[api.VersionLabel] = api.ComposeVersion
@@ -90,19 +101,22 @@ func LoadComposeProject(ctx context.Context, composeFile, projectName string) (*
project.Services[i] = s
}
-
- project.ComposeFiles = []string{composeFile}
- return project, nil
}
-func LoadComposeProjectFromDir(ctx context.Context, dir, projectName string) (*composetypes.Project, string, error) {
+func LoadComposeProjectFromDir(ctx context.Context, dir, projectName, projectsDirectory string) (*composetypes.Project, string, error) {
composeFile, err := DetectComposeFile(dir)
if err != nil {
return nil, "", err
}
- proj, err := LoadComposeProject(ctx, composeFile, projectName)
+
+ if projectsDirectory == "" {
+ projectsDirectory = filepath.Dir(dir)
+ }
+
+ proj, err := LoadComposeProject(ctx, composeFile, projectName, projectsDirectory)
if err != nil {
return nil, "", err
}
+
return proj, composeFile, nil
}
diff --git a/frontend/messages/en.json b/frontend/messages/en.json
index 55f0416a6..7e620cf7d 100644
--- a/frontend/messages/en.json
+++ b/frontend/messages/en.json
@@ -1205,5 +1205,22 @@
"customize_save_failed": "Failed to save default templates",
"customize_save_success": "Default templates saved successfully",
"customize_reset_success": "Changes reset",
- "default_templates": "Default Templates"
+ "default_templates": "Default Templates",
+ "variables_title": "Global Variables",
+ "variables_subtitle": "Manage global environment variables that are available to all projects",
+ "variables_about_title": "About Global Environment Variables",
+ "variables_about_description": "These variables are stored in .env.global at the root of your projects directory and are available to all Docker Compose projects.",
+ "variables_env_vars_title": "Environment Variables",
+ "variables_count_configured": "{count} variable configured",
+ "variables_count_configured_plural": "{count} variables configured",
+ "variables_add_button": "Add",
+ "variables_no_variables_title": "No environment variables yet",
+ "variables_no_variables_description": "Click \"Add\" to create your first variable",
+ "variables_no_results_title": "No variables found",
+ "variables_no_results_description": "Try a different search term",
+ "variables_key_placeholder": "KEY",
+ "variables_value_placeholder": "value",
+ "variables_duplicate_keys_error": "Duplicate keys found. Please ensure all keys are unique.",
+ "variables_save_success": "Global variables saved successfully",
+ "variables_save_failed": "Failed to save. Please try again."
}
diff --git a/frontend/src/lib/config/navigation-config.ts b/frontend/src/lib/config/navigation-config.ts
index fcd79ce36..e4ff6af9b 100644
--- a/frontend/src/lib/config/navigation-config.ts
+++ b/frontend/src/lib/config/navigation-config.ts
@@ -15,6 +15,7 @@ import LockKeyholeIcon from '@lucide/svelte/icons/lock-keyhole';
import AlarmClockIcon from '@lucide/svelte/icons/alarm-clock';
import NavigationIcon from '@lucide/svelte/icons/navigation';
import PaletteIcon from '@lucide/svelte/icons/palette';
+import FileTextIcon from '@lucide/svelte/icons/file-text';
import { m } from '$lib/paraglide/messages';
export type NavigationItem = {
@@ -48,6 +49,11 @@ export const navigationItems: Record<string, NavigationItem[]> = {
title: m.registries_title(),
url: '/customize/registries',
icon: LockKeyholeIcon
+ },
+ {
+ title: m.variables_title(),
+ url: '/customize/variables',
+ icon: FileTextIcon
}
],
environmentItems: [
diff --git a/frontend/src/lib/services/template-service.ts b/frontend/src/lib/services/template-service.ts
index ff275844a..3cb6adc0b 100644
--- a/frontend/src/lib/services/template-service.ts
+++ b/frontend/src/lib/services/template-service.ts
@@ -1,5 +1,6 @@
import BaseAPIService from './api-service';
import type { TemplateRegistry, Template, RemoteRegistry } from '$lib/types/template.type';
+import type { Variable } from '$lib/types/variable.type';
export default class TemplateService extends BaseAPIService {
async loadAll(): Promise<Template[]> {
@@ -78,6 +79,15 @@ export default class TemplateService extends BaseAPIService {
async deleteRegistry(id: string): Promise<void> {
await this.api.delete(`/templates/registries/${id}`);
}
+
+ async getGlobalVariables(): Promise<Variable[]> {
+ const response = await this.api.get('/templates/variables');
+ return response.data?.data ?? [];
+ }
+
+ async updateGlobalVariables(variables: Variable[]): Promise<void> {
+ await this.api.put('/templates/variables', { variables });
+ }
}
export const templateService = new TemplateService();
diff --git a/frontend/src/lib/types/variable.type.ts b/frontend/src/lib/types/variable.type.ts
new file mode 100644
index 000000000..54fa19d7a
--- /dev/null
+++ b/frontend/src/lib/types/variable.type.ts
@@ -0,0 +1,12 @@
+export interface Variable {
+ key: string;
+ value: string;
+}
+
+export interface GlobalVariablesResponse {
+ variables: Variable[];
+}
+
+export interface UpdateGlobalVariablesRequest {
+ variables: Variable[];
+}
diff --git a/frontend/src/routes/customize/variables/+page.svelte b/frontend/src/routes/customize/variables/+page.svelte
new file mode 100644
index 000000000..7f04956f9
--- /dev/null
+++ b/frontend/src/routes/customize/variables/+page.svelte
@@ -0,0 +1,296 @@
+<script lang="ts">
+ import * as Card from '$lib/components/ui/card';
+ import { Button } from '$lib/components/ui/button';
+ import { Input } from '$lib/components/ui/input';
+ import { toast } from 'svelte-sonner';
+ import PlusIcon from '@lucide/svelte/icons/plus';
+ import XIcon from '@lucide/svelte/icons/x';
+ import SearchIcon from '@lucide/svelte/icons/search';
+ import KeyIcon from '@lucide/svelte/icons/key';
+ import AlertCircleIcon from '@lucide/svelte/icons/alert-circle';
+ import CopyIcon from '@lucide/svelte/icons/copy';
+ import settingsStore from '$lib/stores/config-store';
+ import { ResourcePageLayout, type ActionButton } from '$lib/layouts/index.js';
+ import { templateService } from '$lib/services/template-service.js';
+ import type { Variable } from '$lib/types/variable.type';
+ import { m } from '$lib/paraglide/messages';
+
+ let { data } = $props();
+ let envVars = $state<Variable[]>([...data.variables]);
+ let originalVars = $state<Variable[]>([...data.variables]);
+ let searchQuery = $state('');
+ let isLoading = $state(false);
+
+ const isReadOnly = $derived.by(() => $settingsStore.uiConfigDisabled);
+
+ const filteredVars = $derived.by(() => {
+ if (!searchQuery.trim()) return envVars;
+
+ const query = searchQuery.toLowerCase();
+ return envVars.filter((v) => v.key.toLowerCase().includes(query) || v.value.toLowerCase().includes(query));
+ });
+
+ const hasChanges = $derived.by(() => {
+ if (envVars.length !== originalVars.length) return true;
+
+ const originalMap = new Map(originalVars.map((v) => [v.key, v.value]));
+
+ for (const v of envVars) {
+ if (!v.key.trim()) continue;
+
+ const originalValue = originalMap.get(v.key);
+ if (originalValue !== v.value) return true;
+ if (originalValue === undefined) return true;
+ }
+
+ const currentMap = new Map(envVars.filter((v) => v.key.trim()).map((v) => [v.key, v.value]));
+ for (const key of originalMap.keys()) {
+ if (!currentMap.has(key)) return true;
+ }
+
+ return false;
+ });
+
+ function addEnvVar() {
+ envVars = [...envVars, { key: '', value: '' }];
+ }
+
+ function removeEnvVar(index: number) {
+ envVars = envVars.filter((_, i) => i !== index);
+ }
+
+ function duplicateEnvVar(index: number) {
+ const varToDuplicate = envVars[index];
+ let baseKey = varToDuplicate.key;
+ let counter = 1;
+
+ // Check if key already ends with _# pattern
+ const match = baseKey.match(/^(.+)_(\d+)$/);
+ if (match) {
+ baseKey = match[1];
+ counter = parseInt(match[2], 10) + 1;
+ }
+
+ let newKey = `${baseKey}_${counter}`;
+
+ // Find next available number
+ while (envVars.some((v) => v.key === newKey)) {
+ counter++;
+ newKey = `${baseKey}_${counter}`;
+ }
+
+ const newVar = {
+ key: newKey,
+ value: varToDuplicate.value
+ };
+ envVars = [...envVars.slice(0, index + 1), newVar, ...envVars.slice(index + 1)];
+ }
+
+ async function onSubmit() {
+ isLoading = true;
+
+ try {
+ const validVars = envVars
+ .filter((v) => v.key.trim() !== '')
+ .map((v) => ({
+ key: v.key.trim(),
+ value: v.value.trim()
+ }));
+
+ const keys = validVars.map((v) => v.key);
+ const uniqueKeys = new Set(keys);
+ if (keys.length !== uniqueKeys.size) {
+ toast.error(m.variables_duplicate_keys_error());
+ return;
+ }
+
+ await templateService.updateGlobalVariables(validVars);
+
+ originalVars = [...validVars];
+ envVars = [...validVars];
+
+ toast.success(m.variables_save_success());
+ } catch (error) {
+ console.error('Failed to save global environment variables:', error);
+ toast.error(m.variables_save_failed());
+ } finally {
+ isLoading = false;
+ }
+ }
+
+ function resetForm() {
+ envVars = [...originalVars];
+ searchQuery = '';
+ }
+
+ function handleKeyDown(event: KeyboardEvent, index: number) {
+ if (event.key === 'Enter') {
+ event.preventDefault();
+ addEnvVar();
+ }
+ }
+
+ $effect(() => {
+ if (envVars.length === 0) {
+ addEnvVar();
+ }
+ });
+
+ const actionButtons = $derived<ActionButton[]>([
+ {
+ id: 'reset',
+ action: 'restart',
+ label: m.common_reset(),
+ onclick: resetForm,
+ disabled: !hasChanges || isLoading
+ },
+ {
+ id: 'save',
+ action: 'save',
+ label: m.common_save(),
+ loadingLabel: m.common_saving(),
+ loading: isLoading,
+ disabled: isLoading || !hasChanges,
+ onclick: onSubmit
+ }
+ ]);
+</script>
+
+<ResourcePageLayout title={m.variables_title()} subtitle={m.variables_subtitle()} {actionButtons}>
+ {#snippet mainContent()}
+ <fieldset disabled={isReadOnly} class="relative">
+ <div class="space-y-4 sm:space-y-6">
+ <Card.Root class="border-primary/20 bg-primary/5 overflow-hidden pt-0">
+ <Card.Content class="px-3 py-4 sm:px-6">
+ <div class="flex items-start gap-3">
+ <div class="text-primary mt-0.5 shrink-0">
+ <AlertCircleIcon class="size-5" />
+ </div>
+ <div class="space-y-1 text-sm">
+ <p class="text-foreground font-medium">{m.variables_about_title()}</p>
+ <p class="text-muted-foreground">
+ {m.variables_about_description()}
+ </p>
+ </div>
+ </div>
+ </Card.Content>
+ </Card.Root>
+
+ <Card.Root class="overflow-hidden pt-0">
+ <Card.Header class="bg-muted/20 sticky top-0 z-10 border-b !py-3">
+ <div class="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
+ <div class="flex items-center gap-3">
+ <div class="bg-primary/10 text-primary ring-primary/20 flex size-8 items-center justify-center rounded-lg ring-1">
+ <KeyIcon class="size-4" />
+ </div>
+ <div>
+ <Card.Title class="text-base">{m.variables_env_vars_title()}</Card.Title>
+ <Card.Description class="text-xs">
+ {#if envVars.filter((v) => v.key.trim()).length === 1}
+ {m.variables_count_configured({ count: 1 })}
+ {:else}
+ {m.variables_count_configured_plural({ count: envVars.filter((v) => v.key.trim()).length })}
+ {/if}
+ </Card.Description>
+ </div>
+ </div>
+
+ <div class="flex items-center gap-2">
+ <div class="relative w-full sm:w-52">
+ <SearchIcon class="text-muted-foreground pointer-events-none absolute left-3 top-1/2 size-4 -translate-y-1/2" />
+ <Input type="text" placeholder={m.common_search()} bind:value={searchQuery} class="h-9 pl-10" />
+ </div>
+ <Button type="button" size="sm" onclick={addEnvVar} disabled={isLoading} class="shrink-0">
+ <PlusIcon class="mr-1.5 size-4" />
+ {m.variables_add_button()}
+ </Button>
+ </div>
+ </div>
+ </Card.Header>
+
+ <Card.Content class="px-3 py-4 sm:px-6">
+ {#if filteredVars.length === 0 && searchQuery.trim()}
+ <div class="text-muted-foreground flex flex-col items-center justify-center py-12 text-center">
+ <SearchIcon class="mb-3 size-12 opacity-20" />
+ <p class="text-sm font-medium">{m.variables_no_results_title()}</p>
+ <p class="text-xs">{m.variables_no_results_description()}</p>
+ </div>
+ {:else if filteredVars.length === 0}
+ <div class="text-muted-foreground flex flex-col items-center justify-center py-12 text-center">
+ <KeyIcon class="mb-3 size-12 opacity-20" />
+ <p class="text-sm font-medium">{m.variables_no_variables_title()}</p>
+ <p class="text-xs">{m.variables_no_variables_description()}</p>
+ </div>
+ {:else}
+ <div class="space-y-2">
+ {#each filteredVars as envVar, index (index)}
+ {@const actualIndex = envVars.indexOf(envVar)}
+ <div
+ class="bg-muted/30 hover:bg-muted/50 group flex flex-col gap-2 rounded-lg border p-2.5 transition-colors sm:flex-row sm:items-center"
+ >
+ <div class="flex flex-1 gap-2">
+ <Input
+ type="text"
+ placeholder={m.variables_key_placeholder()}
+ bind:value={envVar.key}
+ disabled={isLoading}
+ class="h-9 flex-1 font-mono text-sm sm:max-w-[200px]"
+ onkeydown={(e) => handleKeyDown(e, actualIndex)}
+ oninput={(e) => {
+ const target = e.target as HTMLInputElement;
+ const cursorPos = target.selectionStart || 0;
+ const oldValue = envVar.key;
+ const newValue = target.value.toUpperCase().replace(/\s/g, '_');
+
+ envVar.key = newValue;
+
+ requestAnimationFrame(() => {
+ const diff = newValue.length - oldValue.length;
+ target.setSelectionRange(cursorPos + diff, cursorPos + diff);
+ });
+ }}
+ />
+ <span class="text-muted-foreground flex items-center font-mono text-sm">=</span>
+ <Input
+ type="text"
+ placeholder={m.variables_value_placeholder()}
+ bind:value={envVar.value}
+ disabled={isLoading}
+ class="h-9 flex-[2] font-mono text-sm"
+ onkeydown={(e) => handleKeyDown(e, actualIndex)}
+ />
+ </div>
+ <div class="flex items-center gap-1 self-end sm:self-center">
+ <Button
+ type="button"
+ variant="ghost"
+ size="icon"
+ class="size-8 shrink-0 opacity-70 transition-opacity group-hover:opacity-100"
+ onclick={() => duplicateEnvVar(actualIndex)}
+ disabled={isLoading}
+ title="Duplicate"
+ >
+ <CopyIcon class="size-4" />
+ </Button>
+ <Button
+ type="button"
+ variant="ghost"
+ size="icon"
+ class="text-destructive hover:text-destructive hover:bg-destructive/10 size-8 shrink-0 opacity-70 transition-opacity group-hover:opacity-100"
+ onclick={() => removeEnvVar(actualIndex)}
+ disabled={isLoading}
+ title="Remove"
+ >
+ <XIcon class="size-4" />
+ </Button>
+ </div>
+ </div>
+ {/each}
+ </div>
+ {/if}
+ </Card.Content>
+ </Card.Root>
+ </div>
+ </fieldset>
+ {/snippet}
+</ResourcePageLayout>
diff --git a/frontend/src/routes/customize/variables/+page.ts b/frontend/src/routes/customize/variables/+page.ts
new file mode 100644
index 000000000..ce1ede446
--- /dev/null
+++ b/frontend/src/routes/customize/variables/+page.ts
@@ -0,0 +1,9 @@
+import type { PageLoad } from './$types';
+import { templateService } from '$lib/services/template-service';
+
+export const load: PageLoad = async () => {
+ const variables = await templateService.getGlobalVariables();
+ return {
+ variables
+ };
+};