Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

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

Andras Bacsai54a55be8e573
diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php
index 81628a6291..49cfd61410 100644
--- a/app/Jobs/ApplicationDeploymentJob.php
+++ b/app/Jobs/ApplicationDeploymentJob.php
@@ -911,24 +911,8 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue
});
if ($this->pull_request_id === 0) {
$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') {
$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);
}
}
- } else {
- $this->env_filename = '.env';
- // Filter out buildtime-only variables from runtime environment for preview
- $runtime_environment_variables_preview = $sorted_environment_variables_preview->filter(function ($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_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);
}
// 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()) {
+ if ($this->application->environment_variables->where('key', 'PORT')->isEmpty()) {
$envs->push("PORT={$ports[0]}");
}
}
// 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');
}
+ } else {
+ $this->env_filename = '.env';
+ // Generate SERVICE_ variables first for dockercompose preview
if ($this->build_pack === 'dockercompose') {
$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));
}
}
+
+ // 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 ($this->env_filename) {