refactor(deployment): streamline environment variable handling for dockercompose and improve sorting of runtime variables

This commit is contained in:
Andras Bacsai
2025-09-15 15:39:07 +02:00
parent 77c7da39e2
commit 54a55be8e5

View File

@@ -911,24 +911,8 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue
}); });
if ($this->pull_request_id === 0) { if ($this->pull_request_id === 0) {
$this->env_filename = '.env'; $this->env_filename = '.env';
// Filter out buildtime-only variables from runtime environment
$runtime_environment_variables = $sorted_environment_variables->filter(function ($env) {
return ! $env->is_buildtime_only;
});
foreach ($runtime_environment_variables as $env) {
$envs->push($env->key.'='.$env->real_value);
}
// Add PORT if not exists, use the first port as default
if ($this->build_pack !== 'dockercompose') {
if ($this->application->environment_variables->where('key', 'PORT')->isEmpty()) {
$envs->push("PORT={$ports[0]}");
}
}
// Add HOST if not exists
if ($this->application->environment_variables->where('key', 'HOST')->isEmpty()) {
$envs->push('HOST=0.0.0.0');
}
// Generate SERVICE_ variables first for dockercompose
if ($this->build_pack === 'dockercompose') { if ($this->build_pack === 'dockercompose') {
$domains = collect(json_decode($this->application->docker_compose_domains)) ?? collect([]); $domains = collect(json_decode($this->application->docker_compose_domains)) ?? collect([]);
@@ -957,26 +941,38 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue
$envs->push('SERVICE_NAME_'.str($serviceName)->upper().'='.$serviceName); $envs->push('SERVICE_NAME_'.str($serviceName)->upper().'='.$serviceName);
} }
} }
} else {
$this->env_filename = '.env'; // Filter out buildtime-only variables from runtime environment
// Filter out buildtime-only variables from runtime environment for preview $runtime_environment_variables = $sorted_environment_variables->filter(function ($env) {
$runtime_environment_variables_preview = $sorted_environment_variables_preview->filter(function ($env) {
return ! $env->is_buildtime_only; return ! $env->is_buildtime_only;
}); });
foreach ($runtime_environment_variables_preview as $env) {
// Sort runtime environment variables: those referencing SERVICE_ variables come after others
$runtime_environment_variables = $runtime_environment_variables->sortBy(function ($env) {
if (str($env->value)->startsWith('$SERVICE_') || str($env->value)->contains('${SERVICE_')) {
return 2;
}
return 1;
});
foreach ($runtime_environment_variables as $env) {
$envs->push($env->key.'='.$env->real_value); $envs->push($env->key.'='.$env->real_value);
} }
// Add PORT if not exists, use the first port as default // Add PORT if not exists, use the first port as default
if ($this->build_pack !== 'dockercompose') { if ($this->build_pack !== 'dockercompose') {
if ($this->application->environment_variables_preview->where('key', 'PORT')->isEmpty()) { if ($this->application->environment_variables->where('key', 'PORT')->isEmpty()) {
$envs->push("PORT={$ports[0]}"); $envs->push("PORT={$ports[0]}");
} }
} }
// Add HOST if not exists // Add HOST if not exists
if ($this->application->environment_variables_preview->where('key', 'HOST')->isEmpty()) { if ($this->application->environment_variables->where('key', 'HOST')->isEmpty()) {
$envs->push('HOST=0.0.0.0'); $envs->push('HOST=0.0.0.0');
} }
} else {
$this->env_filename = '.env';
// Generate SERVICE_ variables first for dockercompose preview
if ($this->build_pack === 'dockercompose') { if ($this->build_pack === 'dockercompose') {
$domains = collect(json_decode(data_get($this->preview, 'docker_compose_domains'))) ?? collect([]); $domains = collect(json_decode(data_get($this->preview, 'docker_compose_domains'))) ?? collect([]);
@@ -1001,6 +997,34 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue
$envs->push('SERVICE_NAME_'.str($rawServiceName)->upper().'='.addPreviewDeploymentSuffix($rawServiceName, $this->pull_request_id)); $envs->push('SERVICE_NAME_'.str($rawServiceName)->upper().'='.addPreviewDeploymentSuffix($rawServiceName, $this->pull_request_id));
} }
} }
// Filter out buildtime-only variables from runtime environment for preview
$runtime_environment_variables_preview = $sorted_environment_variables_preview->filter(function ($env) {
return ! $env->is_buildtime_only;
});
// Sort runtime environment variables: those referencing SERVICE_ variables come after others
$runtime_environment_variables_preview = $runtime_environment_variables_preview->sortBy(function ($env) {
if (str($env->value)->startsWith('$SERVICE_') || str($env->value)->contains('${SERVICE_')) {
return 2;
}
return 1;
});
foreach ($runtime_environment_variables_preview as $env) {
$envs->push($env->key.'='.$env->real_value);
}
// Add PORT if not exists, use the first port as default
if ($this->build_pack !== 'dockercompose') {
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->where('key', 'HOST')->isEmpty()) {
$envs->push('HOST=0.0.0.0');
}
} }
if ($envs->isEmpty()) { if ($envs->isEmpty()) {
if ($this->env_filename) { if ($this->env_filename) {