From 90ccaeba51f6bb1b260baa83dd67184205ba72c3 Mon Sep 17 00:00:00 2001 From: Andras Bacsai <5845193+andrasbacsai@users.noreply.github.com> Date: Fri, 18 Jul 2025 20:48:51 +0200 Subject: [PATCH] feat(application): implement environment variable updates for Docker Compose applications, including creation, updating, and deletion of SERVICE_FQDN and SERVICE_URL variables --- app/Livewire/Project/Application/General.php | 88 ++++++++++++++++++++ bootstrap/helpers/services.php | 8 ++ 2 files changed, 96 insertions(+) diff --git a/app/Livewire/Project/Application/General.php b/app/Livewire/Project/Application/General.php index 3e89ad663..ac2e6794d 100644 --- a/app/Livewire/Project/Application/General.php +++ b/app/Livewire/Project/Application/General.php @@ -4,6 +4,7 @@ namespace App\Livewire\Project\Application; use App\Actions\Application\GenerateConfig; use App\Models\Application; +use App\Models\EnvironmentVariable; use Illuminate\Support\Collection; use Livewire\Component; use Spatie\Url\Url; @@ -488,6 +489,12 @@ class General extends Component } $this->application->custom_labels = base64_encode($this->customLabels); $this->application->save(); + + // Update SERVICE_FQDN_ and SERVICE_URL_ environment variables for Docker Compose applications + if ($this->application->build_pack === 'dockercompose') { + $this->updateServiceEnvironmentVariables(); + } + $showToaster && ! $warning && $this->dispatch('success', 'Application settings updated!'); } catch (\Throwable $e) { $originalFqdn = $this->application->getOriginal('fqdn'); @@ -513,4 +520,85 @@ class General extends Component 'Content-Disposition' => 'attachment; filename='.$fileName, ]); } + + private function updateServiceEnvironmentVariables() + { + $domains = collect(json_decode($this->application->docker_compose_domains, true)) ?? collect([]); + + foreach ($domains as $serviceName => $service) { + $serviceNameFormatted = str($serviceName)->upper()->replace('-', '_'); + $domain = data_get($service, 'domain'); + + if ($domain) { + // Create or update SERVICE_FQDN_ and SERVICE_URL_ variables + $fqdn = Url::fromString($domain); + $port = $fqdn->getPort(); + $path = $fqdn->getPath(); + $fqdnValue = $fqdn->getScheme().'://'.$fqdn->getHost(); + if ($path !== '/') { + $fqdnValue = $fqdnValue.$path; + } + $urlValue = str($domain)->after('://'); + if ($path !== '/') { + $urlValue = $urlValue.$path; + } + + // Create/update SERVICE_FQDN_ + EnvironmentVariable::updateOrCreate([ + 'resourceable_type' => Application::class, + 'resourceable_id' => $this->application->id, + 'key' => "SERVICE_FQDN_{$serviceNameFormatted}", + ], [ + 'value' => $fqdnValue, + 'is_build_time' => false, + 'is_preview' => false, + ]); + + // Create/update SERVICE_URL_ + EnvironmentVariable::updateOrCreate([ + 'resourceable_type' => Application::class, + 'resourceable_id' => $this->application->id, + 'key' => "SERVICE_URL_{$serviceNameFormatted}", + ], [ + 'value' => $urlValue, + 'is_build_time' => false, + 'is_preview' => false, + ]); + + // Create/update port-specific variables if port exists + if ($port) { + EnvironmentVariable::updateOrCreate([ + 'resourceable_type' => Application::class, + 'resourceable_id' => $this->application->id, + 'key' => "SERVICE_FQDN_{$serviceNameFormatted}_{$port}", + ], [ + 'value' => $fqdnValue, + 'is_build_time' => false, + 'is_preview' => false, + ]); + + EnvironmentVariable::updateOrCreate([ + 'resourceable_type' => Application::class, + 'resourceable_id' => $this->application->id, + 'key' => "SERVICE_URL_{$serviceNameFormatted}_{$port}", + ], [ + 'value' => $urlValue, + 'is_build_time' => false, + 'is_preview' => false, + ]); + } + } else { + // Delete SERVICE_FQDN_ and SERVICE_URL_ variables if domain is removed + EnvironmentVariable::where('resourceable_type', Application::class) + ->where('resourceable_id', $this->application->id) + ->where('key', 'LIKE', "SERVICE_FQDN_{$serviceNameFormatted}%") + ->delete(); + + EnvironmentVariable::where('resourceable_type', Application::class) + ->where('resourceable_id', $this->application->id) + ->where('key', 'LIKE', "SERVICE_URL_{$serviceNameFormatted}%") + ->delete(); + } + } + } } diff --git a/bootstrap/helpers/services.php b/bootstrap/helpers/services.php index 0adccd023..1e1d2a073 100644 --- a/bootstrap/helpers/services.php +++ b/bootstrap/helpers/services.php @@ -131,6 +131,8 @@ function updateCompose(ServiceApplication|ServiceDatabase $resource) 'key' => $variableName, ], [ 'value' => $fqdnValue, + 'is_build_time' => false, + 'is_preview' => false, ]); if ($port) { $variableName = $variableName."_$port"; @@ -140,6 +142,8 @@ function updateCompose(ServiceApplication|ServiceDatabase $resource) 'key' => $variableName, ], [ 'value' => $fqdnValue, + 'is_build_time' => false, + 'is_preview' => false, ]); } $variableName = 'SERVICE_URL_'.str($resource->name)->upper()->replace('-', '_'); @@ -157,6 +161,8 @@ function updateCompose(ServiceApplication|ServiceDatabase $resource) 'key' => $variableName, ], [ 'value' => $urlValue, + 'is_build_time' => false, + 'is_preview' => false, ]); if ($port) { $variableName = $variableName."_$port"; @@ -166,6 +172,8 @@ function updateCompose(ServiceApplication|ServiceDatabase $resource) 'key' => $variableName, ], [ 'value' => $urlValue, + 'is_build_time' => false, + 'is_preview' => false, ]); } } elseif ($resourceFqdns->count() > 1) {