From eb748554c5ff9a160c46fc8aba12d660bdf0aa91 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 26 Apr 2024 12:59:51 +0200 Subject: [PATCH] Fix environment variable generation in ApplicationDeploymentJob.php and Application.php --- app/Jobs/ApplicationDeploymentJob.php | 18 ++++++++++-------- app/Models/Application.php | 4 ++++ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index 3d04be7d3..1717c5d08 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -710,7 +710,7 @@ private function check_image_locally_or_remotely() private function save_environment_variables() { $envs = collect([]); - $ports = $this->application->settings->is_static ? [80] : $this->application->ports_exposes_array; + $ports = $this->application->main_port(); if ($this->pull_request_id !== 0) { $this->env_filename = ".env-pr-$this->pull_request_id"; foreach ($this->application->environment_variables_preview as $env) { @@ -727,14 +727,15 @@ private function save_environment_variables() $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()) { + if ($this->application->environment_variables_preview->where('key', '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()) { + if ($this->application->environment_variables_preview->where('key', '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()) { + // Add SOURCE_COMMIT if not exists + if ($this->application->environment_variables_preview->where('key', 'SOURCE_COMMIT')->isEmpty()) { if (!is_null($this->commit)) { $envs->push("SOURCE_COMMIT={$this->commit}"); } else { @@ -760,14 +761,15 @@ private function save_environment_variables() $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()) { + if ($this->application->environment_variables->where('key', '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()) { + if ($this->application->environment_variables->where('key', 'HOST')->isEmpty()) { $envs->push("HOST=0.0.0.0"); } - if ($this->application->environment_variables->filter(fn ($env) => Str::of($env)->startsWith('SOURCE_COMMIT'))->isEmpty()) { + // Add SOURCE_COMMIT if not exists + if ($this->application->environment_variables->where('key', 'SOURCE_COMMIT')->isEmpty()) { if (!is_null($this->commit)) { $envs->push("SOURCE_COMMIT={$this->commit}"); } else { @@ -1214,7 +1216,7 @@ private function generate_env_variables() private function generate_compose_file() { $this->create_workdir(); - $ports = $this->application->settings->is_static ? [80] : $this->application->ports_exposes_array; + $ports = $this->application->main_port(); $onlyPort = null; if (count($ports) > 0) { $onlyPort = $ports[0]; diff --git a/app/Models/Application.php b/app/Models/Application.php index 971f1e894..f28d389f4 100644 --- a/app/Models/Application.php +++ b/app/Models/Application.php @@ -346,6 +346,10 @@ public function serviceType() } return null; } + public function main_port() + { + return $this->settings->is_static ? [80] : $this->ports_exposes_array; + } public function environment_variables(): HasMany { return $this->hasMany(EnvironmentVariable::class)->where('is_preview', false)->orderBy('key', 'asc');