From 2382a10bbac364664aab8cb0f3ebe2764f87c1d7 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 12 Apr 2024 09:26:04 +0200 Subject: [PATCH] Refactor environment variable saving logic in ApplicationDeploymentJob.php and escapeEnvVariables function in docker.php --- app/Jobs/ApplicationDeploymentJob.php | 9 +++++++++ bootstrap/helpers/docker.php | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index 729b2e049..c8bc5c117 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -716,19 +716,27 @@ private function save_environment_variables() { $envs = collect([]); if ($this->pull_request_id !== 0) { + $filename = ".env-pr-$this->pull_request_id"; foreach ($this->application->environment_variables_preview as $env) { $envs->push($env->key . '=' . $env->real_value); } } else { + $filename = ".env"; foreach ($this->application->environment_variables as $env) { $envs->push($env->key . '=' . $env->real_value); } } + if ($envs->isEmpty()) { + return; + } $envs_base64 = base64_encode($envs->implode("\n")); $this->execute_remote_command( [ executeInDocker($this->deployment_uuid, "echo '$envs_base64' | base64 -d > $this->workdir/.env") ], + [ + "echo '$envs_base64' | base64 -d > $this->configuration_dir/$filename" + ] ); } @@ -1334,6 +1342,7 @@ private function generate_compose_file() $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/bootstrap/helpers/docker.php b/bootstrap/helpers/docker.php index aed77a7bb..afcc4622b 100644 --- a/bootstrap/helpers/docker.php +++ b/bootstrap/helpers/docker.php @@ -582,7 +582,7 @@ function validateComposeFile(string $compose, int $server_id): string|Throwable function escapeEnvVariables($value) { - $search = array("\\", "\r", "\t", "\x0", '"', "'", "$"); - $replace = array("\\\\", "\\r", "\\t", "\\0", '\"', "\'", "$$"); + $search = array("\\", "\r", "\t", "\x0", '"', "'"); + $replace = array("\\\\", "\\r", "\\t", "\\0", '\"', "\'"); return str_replace($search, $replace, $value); }