Refactor environment variable saving logic in ApplicationDeploymentJob.php and escapeEnvVariables function in docker.php

This commit is contained in:
Andras Bacsai 2024-04-12 09:26:04 +02:00
parent a8db40e99a
commit 2382a10bba
2 changed files with 11 additions and 2 deletions

View File

@ -716,19 +716,27 @@ private function save_environment_variables()
{ {
$envs = collect([]); $envs = collect([]);
if ($this->pull_request_id !== 0) { if ($this->pull_request_id !== 0) {
$filename = ".env-pr-$this->pull_request_id";
foreach ($this->application->environment_variables_preview as $env) { foreach ($this->application->environment_variables_preview as $env) {
$envs->push($env->key . '=' . $env->real_value); $envs->push($env->key . '=' . $env->real_value);
} }
} else { } else {
$filename = ".env";
foreach ($this->application->environment_variables as $env) { foreach ($this->application->environment_variables as $env) {
$envs->push($env->key . '=' . $env->real_value); $envs->push($env->key . '=' . $env->real_value);
} }
} }
if ($envs->isEmpty()) {
return;
}
$envs_base64 = base64_encode($envs->implode("\n")); $envs_base64 = base64_encode($envs->implode("\n"));
$this->execute_remote_command( $this->execute_remote_command(
[ [
executeInDocker($this->deployment_uuid, "echo '$envs_base64' | base64 -d > $this->workdir/.env") 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 = Yaml::dump($docker_compose, 10);
$this->docker_compose_base64 = base64_encode($this->docker_compose); $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->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() private function generate_local_persistent_volumes()

View File

@ -582,7 +582,7 @@ function validateComposeFile(string $compose, int $server_id): string|Throwable
function escapeEnvVariables($value) function escapeEnvVariables($value)
{ {
$search = array("\\", "\r", "\t", "\x0", '"', "'", "$"); $search = array("\\", "\r", "\t", "\x0", '"', "'");
$replace = array("\\\\", "\\r", "\\t", "\\0", '\"', "\'", "$$"); $replace = array("\\\\", "\\r", "\\t", "\\0", '\"', "\'");
return str_replace($search, $replace, $value); return str_replace($search, $replace, $value);
} }