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(ApplicationDeploymentJob): streamline environment variable handling by introducing generate_coolify_env_variables method and consolidating logic for pull request and main branch scenarios

Andras Bacsai4c8ebe146ca7
diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php
index 7bea73aa27..f97ff853cd 100644
--- a/app/Jobs/ApplicationDeploymentJob.php
+++ b/app/Jobs/ApplicationDeploymentJob.php
@@ -899,40 +899,14 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue
$sorted_environment_variables_preview = $this->application->environment_variables_preview->sortBy('id');
}
$ports = $this->application->main_port();
- if ($this->pull_request_id !== 0) {
- $this->env_filename = ".env-pr-$this->pull_request_id";
- // 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 {
- $envs->push('SOURCE_COMMIT=unknown');
- }
- }
- if ($this->application->environment_variables_preview->where('key', 'COOLIFY_FQDN')->isEmpty()) {
- $envs->push("COOLIFY_FQDN={$this->preview->fqdn}");
- $envs->push("COOLIFY_DOMAIN_URL={$this->preview->fqdn}");
- }
- if ($this->application->environment_variables_preview->where('key', 'COOLIFY_URL')->isEmpty()) {
- $url = str($this->preview->fqdn)->replace('http://', '')->replace('https://', '');
- $envs->push("COOLIFY_URL={$url}");
- $envs->push("COOLIFY_DOMAIN_FQDN={$url}");
- }
- if ($this->application->build_pack !== 'dockercompose' || $this->application->compose_parsing_version === '1' || $this->application->compose_parsing_version === '2') {
- if ($this->application->environment_variables_preview->where('key', 'COOLIFY_BRANCH')->isEmpty()) {
- $envs->push("COOLIFY_BRANCH=\"{$local_branch}\"");
- }
- if ($this->application->environment_variables_preview->where('key', 'COOLIFY_RESOURCE_UUID')->isEmpty()) {
- $envs->push("COOLIFY_RESOURCE_UUID={$this->application->uuid}");
- }
- if ($this->application->environment_variables_preview->where('key', 'COOLIFY_CONTAINER_NAME')->isEmpty()) {
- $envs->push("COOLIFY_CONTAINER_NAME={$this->container_name}");
- }
- }
-
- add_coolify_default_environment_variables($this->application, $envs, $this->application->environment_variables_preview);
+ $coolify_envs = $this->generate_coolify_env_variables();
+ $coolify_envs->each(function ($item, $key) use ($envs) {
+ $envs->push($key.'='.$item);
+ });
+ if ($this->pull_request_id === 0) {
+ $this->env_filename = '.env';
- foreach ($sorted_environment_variables_preview as $env) {
+ foreach ($sorted_environment_variables as $env) {
$real_value = $env->real_value;
if ($env->version === '4.0.0-beta.239') {
$real_value = $env->real_value;
@@ -947,54 +921,17 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue
}
// 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';
- // 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 {
- $envs->push('SOURCE_COMMIT=unknown');
- }
- }
- if ($this->application->environment_variables->where('key', 'COOLIFY_FQDN')->isEmpty()) {
- if ((int) $this->application->compose_parsing_version >= 3) {
- $envs->push("COOLIFY_URL={$this->application->fqdn}");
- } else {
- $envs->push("COOLIFY_FQDN={$this->application->fqdn}");
- }
- }
- if ($this->application->environment_variables->where('key', 'COOLIFY_URL')->isEmpty()) {
- $url = str($this->application->fqdn)->replace('http://', '')->replace('https://', '');
- if ((int) $this->application->compose_parsing_version >= 3) {
- $envs->push("COOLIFY_FQDN={$url}");
- } else {
- $envs->push("COOLIFY_URL={$url}");
- }
- }
- if ($this->application->build_pack !== 'dockercompose' || $this->application->compose_parsing_version === '1' || $this->application->compose_parsing_version === '2') {
- if ($this->application->environment_variables->where('key', 'COOLIFY_BRANCH')->isEmpty()) {
- $envs->push("COOLIFY_BRANCH=\"{$local_branch}\"");
- }
- if ($this->application->environment_variables->where('key', 'COOLIFY_RESOURCE_UUID')->isEmpty()) {
- $envs->push("COOLIFY_RESOURCE_UUID={$this->application->uuid}");
- }
- if ($this->application->environment_variables->where('key', 'COOLIFY_CONTAINER_NAME')->isEmpty()) {
- $envs->push("COOLIFY_CONTAINER_NAME={$this->container_name}");
- }
- }
-
- add_coolify_default_environment_variables($this->application, $envs, $this->application->environment_variables);
-
- foreach ($sorted_environment_variables as $env) {
+ $this->env_filename = ".env-pr-$this->pull_request_id";
+ foreach ($sorted_environment_variables_preview as $env) {
$real_value = $env->real_value;
if ($env->version === '4.0.0-beta.239') {
$real_value = $env->real_value;
@@ -1009,14 +946,15 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue
}
// 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()) {
+ 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->where('key', 'HOST')->isEmpty()) {
+ if ($this->application->environment_variables_preview->where('key', 'HOST')->isEmpty()) {
$envs->push('HOST=0.0.0.0');
}
+
}
if ($envs->isEmpty()) {
$this->env_filename = null;
@@ -1625,20 +1563,128 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue
$this->env_nixpacks_args = $this->env_nixpacks_args->implode(' ');
}
+ private function generate_coolify_env_variables(): Collection
+ {
+ $coolify_envs = collect([]);
+ $local_branch = $this->branch;
+ if ($this->pull_request_id !== 0) {
+ // Add SOURCE_COMMIT if not exists
+ if ($this->application->environment_variables_preview->where('key', 'SOURCE_COMMIT')->isEmpty()) {
+ if (! is_null($this->commit)) {
+ $coolify_envs->put('SOURCE_COMMIT', $this->commit);
+ } else {
+ $coolify_envs->put('SOURCE_COMMIT', 'unknown');
+ }
+ }
+ if ($this->application->environment_variables_preview->where('key', 'COOLIFY_FQDN')->isEmpty()) {
+ $coolify_envs->put('COOLIFY_FQDN', $this->preview->fqdn);
+ $coolify_envs->put('COOLIFY_DOMAIN_URL', $this->preview->fqdn);
+ }
+ if ($this->application->environment_variables_preview->where('key', 'COOLIFY_URL')->isEmpty()) {
+ $url = str($this->preview->fqdn)->replace('http://', '')->replace('https://', '');
+ $coolify_envs->put('COOLIFY_URL', $url);
+ $coolify_envs->put('COOLIFY_DOMAIN_FQDN', $url);
+ }
+ if ($this->application->build_pack !== 'dockercompose' || $this->application->compose_parsing_version === '1' || $this->application->compose_parsing_version === '2') {
+ if ($this->application->environment_variables_preview->where('key', 'COOLIFY_BRANCH')->isEmpty()) {
+ $coolify_envs->put('COOLIFY_BRANCH', $local_branch);
+ }
+ if ($this->application->environment_variables_preview->where('key', 'COOLIFY_RESOURCE_UUID')->isEmpty()) {
+ $coolify_envs->put('COOLIFY_RESOURCE_UUID', $this->application->uuid);
+ }
+ if ($this->application->environment_variables_preview->where('key', 'COOLIFY_CONTAINER_NAME')->isEmpty()) {
+ $coolify_envs->put('COOLIFY_CONTAINER_NAME', $this->container_name);
+ }
+ }
+
+ add_coolify_default_environment_variables($this->application, $coolify_envs, $this->application->environment_variables_preview);
+
+ } else {
+ // Add SOURCE_COMMIT if not exists
+ if ($this->application->environment_variables->where('key', 'SOURCE_COMMIT')->isEmpty()) {
+ if (! is_null($this->commit)) {
+ $coolify_envs->put('SOURCE_COMMIT', $this->commit);
+ } else {
+ $coolify_envs->put('SOURCE_COMMIT', 'unknown');
+ }
+ }
+ if ($this->application->environment_variables->where('key', 'COOLIFY_FQDN')->isEmpty()) {
+ if ((int) $this->application->compose_parsing_version >= 3) {
+ $coolify_envs->put('COOLIFY_URL', $this->application->fqdn);
+ } else {
+ $coolify_envs->put('COOLIFY_FQDN', $this->application->fqdn);
+ }
+ }
+ if ($this->application->environment_variables->where('key', 'COOLIFY_URL')->isEmpty()) {
+ $url = str($this->application->fqdn)->replace('http://', '')->replace('https://', '');
+ if ((int) $this->application->compose_parsing_version >= 3) {
+ $coolify_envs->put('COOLIFY_FQDN', $url);
+ } else {
+ $coolify_envs->put('COOLIFY_URL', $url);
+ }
+ }
+ if ($this->application->build_pack !== 'dockercompose' || $this->application->compose_parsing_version === '1' || $this->application->compose_parsing_version === '2') {
+ if ($this->application->environment_variables->where('key', 'COOLIFY_BRANCH')->isEmpty()) {
+ $coolify_envs->put('COOLIFY_BRANCH', $local_branch);
+ }
+ if ($this->application->environment_variables->where('key', 'COOLIFY_RESOURCE_UUID')->isEmpty()) {
+ $coolify_envs->put('COOLIFY_RESOURCE_UUID', $this->application->uuid);
+ }
+ if ($this->application->environment_variables->where('key', 'COOLIFY_CONTAINER_NAME')->isEmpty()) {
+ $coolify_envs->put('COOLIFY_CONTAINER_NAME', $this->container_name);
+ }
+ }
+
+ add_coolify_default_environment_variables($this->application, $coolify_envs, $this->application->environment_variables);
+
+ }
+
+ return $coolify_envs;
+ }
+
private function generate_env_variables()
{
$this->env_args = collect([]);
$this->env_args->put('SOURCE_COMMIT', $this->commit);
+ $coolify_envs = $this->generate_coolify_env_variables();
if ($this->pull_request_id === 0) {
foreach ($this->application->build_environment_variables as $env) {
if (! is_null($env->real_value)) {
$this->env_args->put($env->key, $env->real_value);
+ if (str($env->real_value)->startsWith('$')) {
+ $variable_key = str($env->real_value)->after('$');
+ if ($variable_key->startsWith('COOLIFY_')) {
+ $variable = $coolify_envs->get($variable_key->value());
+ if (filled($variable)) {
+ $this->env_args->prepend($variable, $variable_key->value());
+ }
+ } else {
+ $variable = $this->application->environment_variables()->where('key', $variable_key)->first();
+ if ($variable) {
+ $this->env_args->prepend($variable->real_value, $env->key);
+ }
+ }
+ }
}
}
} else {
foreach ($this->application->build_environment_variables_preview as $env) {
if (! is_null($env->real_value)) {
$this->env_args->put($env->key, $env->real_value);
+ if (str($env->real_value)->startsWith('$')) {
+ $variable_key = str($env->real_value)->after('$');
+ if ($variable_key->startsWith('COOLIFY_')) {
+ $variable = $coolify_envs->get($variable_key->value());
+ if (filled($variable)) {
+ $this->env_args->prepend($variable, $variable_key->value());
+ }
+ } else {
+ $variable = $this->application->environment_variables_preview()->where('key', $variable_key)->first();
+ if ($variable) {
+ $this->env_args->prepend($variable->real_value, $env->key);
+ }
+ }
+ }
}
}
}