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.

test add_coolif_default_environment_variables

Vahorf6d649307c58
diff --git a/app/Actions/Database/StartClickhouse.php b/app/Actions/Database/StartClickhouse.php
index 7316bcd321..c8d787cb20 100644
--- a/app/Actions/Database/StartClickhouse.php
+++ b/app/Actions/Database/StartClickhouse.php
@@ -160,20 +160,7 @@ class StartClickhouse
$environment_variables->push("CLICKHOUSE_ADMIN_PASSWORD={$this->database->clickhouse_admin_password}");
}
- // TODO: move this in a shared function
- if ($environment_variables->where('key', 'COOLIFY_APP_NAME')->isEmpty()) {
- $environment_variables->push("COOLIFY_APP_NAME={$this->database->name}");
- }
- if ($environment_variables->where('key', 'COOLIFY_SERVER_IP')->isEmpty()) {
- $environment_variables->push("COOLIFY_SERVER_IP={$this->database->destination->server->ip}");
- }
- if ($environment_variables->where('key', 'COOLIFY_ENVIRONMENT_NAME')->isEmpty()) {
- $environment_variables->push("COOLIFY_ENVIRONMENT_NAME={$this->database->environment->name}");
- }
- if ($environment_variables->where('key', 'COOLIFY_PROJECT_NAME')->isEmpty()) {
- $environment_variables->push("COOLIFY_PROJECT_NAME={$this->database->project()->name}");
- }
-
+ add_coolify_default_environment_variables($this->database, $environment_variables, $environment_variables);
return $environment_variables->all();
}
diff --git a/app/Actions/Database/StartKeydb.php b/app/Actions/Database/StartKeydb.php
index 5b49b42e74..9290efc7ca 100644
--- a/app/Actions/Database/StartKeydb.php
+++ b/app/Actions/Database/StartKeydb.php
@@ -163,20 +163,7 @@ class StartKeydb
$environment_variables->push("REDIS_PASSWORD={$this->database->keydb_password}");
}
- // TODO: move this in a shared function
- if ($environment_variables->where('key', 'COOLIFY_APP_NAME')->isEmpty()) {
- $environment_variables->push("COOLIFY_APP_NAME={$this->database->name}");
- }
- if ($environment_variables->where('key', 'COOLIFY_SERVER_IP')->isEmpty()) {
- $environment_variables->push("COOLIFY_SERVER_IP={$this->database->destination->server->ip}");
- }
- if ($environment_variables->where('key', 'COOLIFY_ENVIRONMENT_NAME')->isEmpty()) {
- $environment_variables->push("COOLIFY_ENVIRONMENT_NAME={$this->database->environment->name}");
- }
- if ($environment_variables->where('key', 'COOLIFY_PROJECT_NAME')->isEmpty()) {
- $environment_variables->push("COOLIFY_PROJECT_NAME={$this->database->project()->name}");
- }
-
+ add_coolify_default_environment_variables($this->database, $environment_variables, $environment_variables);
return $environment_variables->all();
}
diff --git a/app/Actions/Database/StartMariadb.php b/app/Actions/Database/StartMariadb.php
index 045ae4014f..f37a5e3610 100644
--- a/app/Actions/Database/StartMariadb.php
+++ b/app/Actions/Database/StartMariadb.php
@@ -169,20 +169,7 @@ class StartMariadb
$environment_variables->push("MARIADB_PASSWORD={$this->database->mariadb_password}");
}
- // TODO: move this in a shared function
- if ($environment_variables->where('key', 'COOLIFY_APP_NAME')->isEmpty()) {
- $environment_variables->push("COOLIFY_APP_NAME={$this->database->name}");
- }
- if ($environment_variables->where('key', 'COOLIFY_SERVER_IP')->isEmpty()) {
- $environment_variables->push("COOLIFY_SERVER_IP={$this->database->destination->server->ip}");
- }
- if ($environment_variables->where('key', 'COOLIFY_ENVIRONMENT_NAME')->isEmpty()) {
- $environment_variables->push("COOLIFY_ENVIRONMENT_NAME={$this->database->environment->name}");
- }
- if ($environment_variables->where('key', 'COOLIFY_PROJECT_NAME')->isEmpty()) {
- $environment_variables->push("COOLIFY_PROJECT_NAME={$this->database->project()->name}");
- }
-
+ add_coolify_default_environment_variables($this->database, $environment_variables, $environment_variables);
return $environment_variables->all();
}
diff --git a/app/Actions/Database/StartMongodb.php b/app/Actions/Database/StartMongodb.php
index 1545108361..42fc8f3483 100644
--- a/app/Actions/Database/StartMongodb.php
+++ b/app/Actions/Database/StartMongodb.php
@@ -182,20 +182,7 @@ class StartMongodb
$environment_variables->push("MONGO_INITDB_DATABASE={$this->database->mongo_initdb_database}");
}
- // TODO: move this in a shared function
- if ($environment_variables->where('key', 'COOLIFY_APP_NAME')->isEmpty()) {
- $environment_variables->push("COOLIFY_APP_NAME={$this->database->name}");
- }
- if ($environment_variables->where('key', 'COOLIFY_SERVER_IP')->isEmpty()) {
- $environment_variables->push("COOLIFY_SERVER_IP={$this->database->destination->server->ip}");
- }
- if ($environment_variables->where('key', 'COOLIFY_ENVIRONMENT_NAME')->isEmpty()) {
- $environment_variables->push("COOLIFY_ENVIRONMENT_NAME={$this->database->environment->name}");
- }
- if ($environment_variables->where('key', 'COOLIFY_PROJECT_NAME')->isEmpty()) {
- $environment_variables->push("COOLIFY_PROJECT_NAME={$this->database->project()->name}");
- }
-
+ add_coolify_default_environment_variables($this->database, $environment_variables, $environment_variables);
return $environment_variables->all();
}
diff --git a/app/Actions/Database/StartMysql.php b/app/Actions/Database/StartMysql.php
index 0ffc935783..2043342fea 100644
--- a/app/Actions/Database/StartMysql.php
+++ b/app/Actions/Database/StartMysql.php
@@ -169,20 +169,7 @@ class StartMysql
$environment_variables->push("MYSQL_PASSWORD={$this->database->mysql_password}");
}
- // TODO: move this in a shared function
- if ($environment_variables->where('key', 'COOLIFY_APP_NAME')->isEmpty()) {
- $environment_variables->push("COOLIFY_APP_NAME={$this->database->name}");
- }
- if ($environment_variables->where('key', 'COOLIFY_SERVER_IP')->isEmpty()) {
- $environment_variables->push("COOLIFY_SERVER_IP={$this->database->destination->server->ip}");
- }
- if ($environment_variables->where('key', 'COOLIFY_ENVIRONMENT_NAME')->isEmpty()) {
- $environment_variables->push("COOLIFY_ENVIRONMENT_NAME={$this->database->environment->name}");
- }
- if ($environment_variables->where('key', 'COOLIFY_PROJECT_NAME')->isEmpty()) {
- $environment_variables->push("COOLIFY_PROJECT_NAME={$this->database->project()->name}");
- }
-
+ add_coolify_default_environment_variables($this->database, $environment_variables, $environment_variables);
return $environment_variables->all();
}
diff --git a/app/Actions/Database/StartPostgresql.php b/app/Actions/Database/StartPostgresql.php
index f719a0fed2..bc37fd5cf4 100644
--- a/app/Actions/Database/StartPostgresql.php
+++ b/app/Actions/Database/StartPostgresql.php
@@ -191,20 +191,7 @@ class StartPostgresql
$environment_variables->push("POSTGRES_DB={$this->database->postgres_db}");
}
- // TODO: move this in a shared function
- if ($environment_variables->where('key', 'COOLIFY_APP_NAME')->isEmpty()) {
- $environment_variables->push("COOLIFY_APP_NAME={$this->database->name}");
- }
- if ($environment_variables->where('key', 'COOLIFY_SERVER_IP')->isEmpty()) {
- $environment_variables->push("COOLIFY_SERVER_IP={$this->database->destination->server->ip}");
- }
- if ($environment_variables->where('key', 'COOLIFY_ENVIRONMENT_NAME')->isEmpty()) {
- $environment_variables->push("COOLIFY_ENVIRONMENT_NAME={$this->database->environment->name}");
- }
- if ($environment_variables->where('key', 'COOLIFY_PROJECT_NAME')->isEmpty()) {
- $environment_variables->push("COOLIFY_PROJECT_NAME={$this->database->project()->name}");
- }
-
+ add_coolify_default_environment_variables($this->database, $environment_variables, $environment_variables);
return $environment_variables->all();
}
diff --git a/app/Actions/Database/StartRedis.php b/app/Actions/Database/StartRedis.php
index be33a4834b..b837414d6e 100644
--- a/app/Actions/Database/StartRedis.php
+++ b/app/Actions/Database/StartRedis.php
@@ -168,20 +168,7 @@ class StartRedis
$environment_variables->push("REDIS_PASSWORD={$this->database->redis_password}");
}
- // TODO: move this in a shared function
- if ($environment_variables->where('key', 'COOLIFY_APP_NAME')->isEmpty()) {
- $environment_variables->push("COOLIFY_APP_NAME={$this->database->name}");
- }
- if ($environment_variables->where('key', 'COOLIFY_SERVER_IP')->isEmpty()) {
- $environment_variables->push("COOLIFY_SERVER_IP={$this->database->destination->server->ip}");
- }
- if ($environment_variables->where('key', 'COOLIFY_ENVIRONMENT_NAME')->isEmpty()) {
- $environment_variables->push("COOLIFY_ENVIRONMENT_NAME={$this->database->environment->name}");
- }
- if ($environment_variables->where('key', 'COOLIFY_PROJECT_NAME')->isEmpty()) {
- $environment_variables->push("COOLIFY_PROJECT_NAME={$this->database->project()->name}");
- }
-
+ add_coolify_default_environment_variables($this->database, $environment_variables, $environment_variables);
return $environment_variables->all();
}
diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php
index 0cddf1d54e..725b84c700 100644
--- a/app/Jobs/ApplicationDeploymentJob.php
+++ b/app/Jobs/ApplicationDeploymentJob.php
@@ -906,19 +906,7 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue
}
}
- // TODO: move this in a shared function
- if ($this->application->environment_variables_preview->where('key', 'COOLIFY_APP_NAME')->isEmpty()) {
- $envs->push("COOLIFY_APP_NAME={$this->application->name}");
- }
- if ($this->application->environment_variables_preview->where('key', 'COOLIFY_SERVER_IP')->isEmpty()) {
- $envs->push("COOLIFY_SERVER_IP={$this->application->destination->server->ip}");
- }
- if ($this->application->environment_variables_preview->where('key', 'COOLIFY_ENVIRONMENT_NAME')->isEmpty()) {
- $envs->push("COOLIFY_ENVIRONMENT_NAME={$this->application->environment->name}");
- }
- if ($this->application->environment_variables_preview->where('key', 'COOLIFY_PROJECT_NAME')->isEmpty()) {
- $envs->push("COOLIFY_PROJECT_NAME={$this->application->project()->name}");
- }
+ add_coolify_default_environment_variables($this->application, $environment, $this->application->environment_variables_preview);
foreach ($sorted_environment_variables_preview as $env) {
$real_value = $env->real_value;
@@ -969,19 +957,7 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue
}
}
- // TODO: move this in a shared function
- if ($this->application->environment_variables->where('key', 'COOLIFY_APP_NAME')->isEmpty()) {
- $envs->push("COOLIFY_APP_NAME={$this->application->name}");
- }
- if ($this->application->environment_variables->where('key', 'COOLIFY_SERVER_IP')->isEmpty()) {
- $envs->push("COOLIFY_SERVER_IP={$this->application->destination->server->ip}");
- }
- if ($this->application->environment_variables->where('key', 'COOLIFY_ENVIRONMENT_NAME')->isEmpty()) {
- $envs->push("COOLIFY_ENVIRONMENT_NAME={$this->application->environment->name}");
- }
- if ($this->application->environment_variables->where('key', 'COOLIFY_PROJECT_NAME')->isEmpty()) {
- $envs->push("COOLIFY_PROJECT_NAME={$this->application->project()->name}");
- }
+ add_coolify_default_environment_variables($this->application, $envs, $this->application->environment_variables);
foreach ($sorted_environment_variables as $env) {
$real_value = $env->real_value;
diff --git a/app/Models/Application.php b/app/Models/Application.php
index 604568df45..a24301abc3 100644
--- a/app/Models/Application.php
+++ b/app/Models/Application.php
@@ -1464,19 +1464,7 @@ class Application extends BaseModel
if ($this->environment_variables->where('key', 'COOLIFY_CONTAINER_NAME')->isEmpty()) {
$environment->put('COOLIFY_CONTAINER_NAME', $containerName);
}
- // TODO: move this in a shared function
- if ($this->environment_variables->where('key', 'COOLIFY_APP_NAME')->isEmpty()) {
- $environment->push("COOLIFY_APP_NAME={$this->database->name}");
- }
- if ($this->environment->where('key', 'COOLIFY_SERVER_IP')->isEmpty()) {
- $environment->push("COOLIFY_SERVER_IP={$this->database->destination->server->ip}");
- }
- if ($this->environment->where('key', 'COOLIFY_ENVIRONMENT_NAME')->isEmpty()) {
- $environment->push("COOLIFY_ENVIRONMENT_NAME={$this->database->environment->name}");
- }
- if ($this->environment->where('key', 'COOLIFY_PROJECT_NAME')->isEmpty()) {
- $environment->push("COOLIFY_PROJECT_NAME={$this->database->project()->name}");
- }
+ add_coolify_default_environment_variables($this->database, $environment, $this->environment_variables);
// Remove SERVICE_FQDN and SERVICE_URL from environment
$environment = $environment->filter(function ($value, $key) {
diff --git a/bootstrap/helpers/shared.php b/bootstrap/helpers/shared.php
index a48c9754ef..99f2b9d8d2 100644
--- a/bootstrap/helpers/shared.php
+++ b/bootstrap/helpers/shared.php
@@ -3346,3 +3346,32 @@ function generate_fluentd_configuration() : array {
]
];
}
+
+/**
+* This method adds the default environment variables to the resource.
+* - COOLIFY_APP_NAME
+* - COOLIFY_PROJECT_NAME
+* - COOLIFY_SERVER_IP
+* - COOLIFY_ENVIRONMENT_NAME
+*
+* Theses variables are added in place to the $where_to_add array.
+*
+* @param StandaloneRedis|StandalonePostgresql|StandaloneMongodb|StandaloneMysql|StandaloneMariadb|StandaloneKeydb|StandaloneDragonfly|StandaloneClickhouse|Application $resource
+* @param Collection $where_to_add
+* @param Collection|null $where_to_check
+*
+*/
+function add_coolify_default_environment_variables(StandaloneRedis|StandalonePostgresql|StandaloneMongodb|StandaloneMysql|StandaloneMariadb|StandaloneKeydb|StandaloneDragonfly|StandaloneClickhouse|Application $resource, Collection &$where_to_add, ?Collection $where_to_check = null) {
+ if ($where_to_check != null && $where_to_check->where('key', 'COOLIFY_APP_NAME')->isEmpty()) {
+ $where_to_add->push("COOLIFY_APP_NAME={$resource->name}");
+ }
+ if ($where_to_check != null && $where_to_check->where('key', 'COOLIFY_SERVER_IP')->isEmpty()) {
+ $where_to_add->push("COOLIFY_SERVER_IP={$resource->destination->server->ip}");
+ }
+ if ($where_to_check != null && $where_to_check->where('key', 'COOLIFY_ENVIRONMENT_NAME')->isEmpty()) {
+ $where_to_add->push("COOLIFY_ENVIRONMENT_NAME={$resource->environment->name}");
+ }
+ if ($where_to_check != null && $where_to_check->where('key', 'COOLIFY_PROJECT_NAME')->isEmpty()) {
+ $where_to_add->push("COOLIFY_PROJECT_NAME={$resource->project()->name}");
+ }
+}