diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index df034a2f7..fd5d741e0 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -708,17 +708,67 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted private function save_environment_variables() { $envs = collect([]); + $ports = $this->application->settings->is_static ? [80] : $this->application->ports_exposes_array; if ($this->pull_request_id !== 0) { $this->env_filename = ".env-pr-$this->pull_request_id"; foreach ($this->application->environment_variables_preview as $env) { - $envs->push($env->key . '=' . $env->real_value); + $real_value = $env->real_value; + if ($env->version === '4.0.0-beta.239') { + $real_value = $env->real_value; + } else { + $real_value = escapeEnvVariables($env->real_value); + } + if ($env->is_literal) { + $real_value = '\'' . $real_value . '\''; + } + $envs->push($env->key . '=' . $real_value); + } + // Add PORT if not exists, use the first port as default + if ($this->application->environment_variables_preview->filter(fn ($env) => Str::of($env)->startsWith('PORT'))->isEmpty()) { + $envs->push("PORT={$ports[0]}"); + } + // Add HOST if not exists + if ($this->application->environment_variables_preview->filter(fn ($env) => Str::of($env)->startsWith('HOST'))->isEmpty()) { + $envs->push("HOST=0.0.0.0"); + } + if ($this->application->environment_variables_preview->filter(fn ($env) => Str::of($env)->startsWith('SOURCE_COMMIT'))->isEmpty()) { + if (!is_null($this->commit)) { + $envs->push("SOURCE_COMMIT={$this->commit}"); + } else { + $envs->push("SOURCE_COMMIT=unknown"); + } } } else { $this->env_filename = ".env"; foreach ($this->application->environment_variables as $env) { - $envs->push($env->key . '=' . $env->real_value); + $real_value = $env->real_value; + if ($env->version === '4.0.0-beta.239') { + $real_value = $env->real_value; + } else { + $real_value = escapeEnvVariables($env->real_value); + } + if ($env->is_literal) { + $real_value = '\'' . $real_value . '\''; + } + $envs->push($env->key . '=' . $real_value); + } + // Add PORT if not exists, use the first port as default + if ($this->application->environment_variables->filter(fn ($env) => Str::of($env)->startsWith('PORT'))->isEmpty()) { + $envs->push("PORT={$ports[0]}"); + } + // Add HOST if not exists + if ($this->application->environment_variables->filter(fn ($env) => Str::of($env)->startsWith('HOST'))->isEmpty()) { + $envs->push("HOST=0.0.0.0"); + } + if ($this->application->environment_variables->filter(fn ($env) => Str::of($env)->startsWith('SOURCE_COMMIT'))->isEmpty()) { + if (!is_null($this->commit)) { + $envs->push("SOURCE_COMMIT={$this->commit}"); + } else { + $envs->push("SOURCE_COMMIT=unknown"); + } } } + if ($envs->isEmpty()) { $this->env_filename = null; $this->execute_remote_command( @@ -1128,7 +1178,8 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted } $persistent_storages = $this->generate_local_persistent_volumes(); $volume_names = $this->generate_local_persistent_volumes_only_volume_names(); - $environment_variables = $this->generate_environment_variables($ports); + // $environment_variables = $this->generate_environment_variables($ports); + $this->save_environment_variables(); if (data_get($this->application, 'custom_labels')) { $this->application->parseContainerLabels(); $labels = collect(preg_split("/\r\n|\n|\r/", base64_decode($this->application->custom_labels))); @@ -1182,7 +1233,8 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted 'image' => $this->production_image_name, 'container_name' => $this->container_name, 'restart' => RESTART_MODE, - 'environment' => $environment_variables, + // 'env_file' => $this->env_filename, + // 'environment' => $environment_variables, 'expose' => $ports, 'networks' => [ $this->destination->network, @@ -1361,7 +1413,6 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted $this->docker_compose = Yaml::dump($docker_compose, 10); $this->docker_compose_base64 = base64_encode($this->docker_compose); $this->execute_remote_command([executeInDocker($this->deployment_uuid, "echo '{$this->docker_compose_base64}' | base64 -d > {$this->workdir}/docker-compose.yml"), "hidden" => true]); - $this->save_environment_variables(); } private function generate_local_persistent_volumes() diff --git a/app/Livewire/Project/Application/General.php b/app/Livewire/Project/Application/General.php index 7eb910533..f10da3563 100644 --- a/app/Livewire/Project/Application/General.php +++ b/app/Livewire/Project/Application/General.php @@ -165,7 +165,6 @@ class General extends Component return handleError($e, $this); } finally { $this->initLoadingCompose = false; - } } public function generateDomain(string $serviceName) diff --git a/app/Livewire/Project/Shared/EnvironmentVariable/All.php b/app/Livewire/Project/Shared/EnvironmentVariable/All.php index c4e14c905..e7ce3894a 100644 --- a/app/Livewire/Project/Shared/EnvironmentVariable/All.php +++ b/app/Livewire/Project/Shared/EnvironmentVariable/All.php @@ -15,8 +15,10 @@ class All extends Component public ?string $variables = null; public ?string $variablesPreview = null; public string $view = 'normal'; - protected $listeners = ['refreshEnvs', 'saveKey' => 'submit']; - + protected $listeners = [ + 'refreshEnvs', + 'saveKey' => 'submit', + ]; public function mount() { $resourceClass = get_class($this->resource); diff --git a/app/Livewire/Project/Shared/EnvironmentVariable/Show.php b/app/Livewire/Project/Shared/EnvironmentVariable/Show.php index a9854c196..65e91e60a 100644 --- a/app/Livewire/Project/Shared/EnvironmentVariable/Show.php +++ b/app/Livewire/Project/Shared/EnvironmentVariable/Show.php @@ -16,6 +16,9 @@ class Show extends Component public bool $isLocked = false; public bool $isSharedVariable = false; public string $type; + protected $listeners = [ + "compose_loaded" => '$refresh', + ]; protected $rules = [ 'env.key' => 'required|string', @@ -43,7 +46,6 @@ class Show extends Component $this->modalId = new Cuid2(7); $this->parameters = get_route_parameters(); $this->checkEnvs(); - ray($this->env); } public function checkEnvs() { diff --git a/app/Models/EnvironmentVariable.php b/app/Models/EnvironmentVariable.php index 03930fba4..32bc04285 100644 --- a/app/Models/EnvironmentVariable.php +++ b/app/Models/EnvironmentVariable.php @@ -6,6 +6,7 @@ use App\Models\EnvironmentVariable as ModelsEnvironmentVariable; use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Str; +use Symfony\Component\Yaml\Yaml; class EnvironmentVariable extends Model { @@ -81,6 +82,44 @@ class EnvironmentVariable extends Model } ); } + protected function isFoundInCompose(): Attribute + { + return Attribute::make( + get: function () { + if (!$this->application_id) { + return true; + } + $found_in_compose = false; + $resource = $this->resource(); + $compose = data_get($resource, 'docker_compose_raw'); + if (!$compose) { + return true; + } + $yaml = Yaml::parse($compose); + $services = collect(data_get($yaml, 'services')); + if ($services->isEmpty()) { + return false; + } + foreach ($services as $service) { + $environments = collect(data_get($service, 'environment')); + if ($environments->isEmpty()) { + $found_in_compose = false; + break; + } + $found_in_compose = $environments->contains(function ($item) { + if (str($item)->contains('=')) { + $item = str($item)->before('='); + } + return strpos($item, $this->key) !== false; + }); + if ($found_in_compose) { + break; + } + } + return $found_in_compose; + } + ); + } protected function isShared(): Attribute { return Attribute::make( diff --git a/config/livewire.php b/config/livewire.php index 83229fcea..cf9bcd206 100644 --- a/config/livewire.php +++ b/config/livewire.php @@ -146,4 +146,5 @@ return [ */ 'pagination_theme' => 'tailwind', + 'lazy_placeholder' => 'components.page-loading', ]; diff --git a/resources/views/components/page-loading.blade.php b/resources/views/components/page-loading.blade.php new file mode 100644 index 000000000..ca2a89e31 --- /dev/null +++ b/resources/views/components/page-loading.blade.php @@ -0,0 +1,13 @@ +@props(['text' => "Loading..."]) +