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.
PHP coolifydatabasesdeploymentdockerdocker-composeinertiajslaravelmariadbmysqlnextjsnodejsphppostgresredisself-hostedself-hostingserverstatic-sitesveltesvelte5
Merge remote-tracking branch 'origin/next' into pr-7764-shadow/add-shared-server-env
Andras Bacsaicb97a18a78de
diff --cc app/Jobs/ApplicationDeploymentJob.phpindex 2e681ec740,833e6bfe88..b77fa85b87--- a/app/Jobs/ApplicationDeploymentJob.php+++ b/app/Jobs/ApplicationDeploymentJob.php@@@ -1293,8 -1348,24 +1348,24 @@@ class ApplicationDeploymentJob implemen});foreach ($runtime_environment_variables_preview as $env) {- $envs->push($env->key.'='.$env->real_value);+ $envs->push($env->key.'='.$env->getResolvedValueWithServer($this->server));}++ // Fall back to production env vars for keys not overridden by preview vars,+ // but only when preview vars are configured. This ensures variables like+ // DB_PASSWORD that are only set for production will be available in the+ // preview .env file (fixing ${VAR} interpolation in docker-compose YAML),+ // while avoiding leaking production values when previews aren't configured.+ if ($runtime_environment_variables_preview->isNotEmpty()) {+ $previewKeys = $runtime_environment_variables_preview->pluck('key')->toArray();+ $fallback_production_vars = $sorted_environment_variables->filter(function ($env) use ($previewKeys) {+ return $env->is_runtime && ! in_array($env->key, $previewKeys);+ });+ foreach ($fallback_production_vars as $env) {- $envs->push($env->key.'='.$env->real_value);++ $envs->push($env->key.'='.$env->getResolvedValueWithServer($this->server));+ }+ }+// Add PORT if not exists, use the first port as defaultif ($this->build_pack !== 'dockercompose') {if ($this->application->environment_variables_preview->where('key', 'PORT')->isEmpty()) {@@@ -1533,10 -1604,10 +1604,11 @@@}foreach ($sorted_environment_variables as $env) {++ $resolvedValue = $env->getResolvedValueWithServer($this->server);// For literal/multiline vars, real_value includes quotes that we need to removeif ($env->is_literal || $env->is_multiline) {// Strip outer quotes from real_value and apply proper bash escaping-- $value = trim($env->real_value, "'");++ $value = trim($resolvedValue, "'");$escapedValue = escapeBashEnvValue($value);if (isDev() && isset($envs_dict[$env->key])) {@@@ -1548,13 -1619,13 +1620,13 @@@if (isDev()) {$this->application_deployment_queue->addLogEntry("[DEBUG] Build-time env: {$env->key}");$this->application_deployment_queue->addLogEntry('[DEBUG] Type: literal/multiline');-- $this->application_deployment_queue->addLogEntry("[DEBUG] raw real_value: {$env->real_value}");++ $this->application_deployment_queue->addLogEntry("[DEBUG] raw real_value: {$resolvedValue}");$this->application_deployment_queue->addLogEntry("[DEBUG] stripped value: {$value}");$this->application_deployment_queue->addLogEntry("[DEBUG] final escaped: {$escapedValue}");}} else {// For normal vars, use double quotes to allow $VAR expansion-- $escapedValue = escapeBashDoubleQuoted($env->real_value);++ $escapedValue = escapeBashDoubleQuoted($resolvedValue);if (isDev() && isset($envs_dict[$env->key])) {$this->application_deployment_queue->addLogEntry("[DEBUG] User override: {$env->key} (was: {$envs_dict[$env->key]}, now: {$escapedValue})");@@@ -1565,7 -1636,7 +1637,7 @@@if (isDev()) {$this->application_deployment_queue->addLogEntry("[DEBUG] Build-time env: {$env->key}");$this->application_deployment_queue->addLogEntry('[DEBUG] Type: normal (allows expansion)');-- $this->application_deployment_queue->addLogEntry("[DEBUG] real_value: {$env->real_value}");++ $this->application_deployment_queue->addLogEntry("[DEBUG] real_value: {$resolvedValue}");$this->application_deployment_queue->addLogEntry("[DEBUG] final escaped: {$escapedValue}");}}@@@ -1584,10 -1655,10 +1656,11 @@@}foreach ($sorted_environment_variables as $env) {++ $resolvedValue = $env->getResolvedValueWithServer($this->server);// For literal/multiline vars, real_value includes quotes that we need to removeif ($env->is_literal || $env->is_multiline) {// Strip outer quotes from real_value and apply proper bash escaping-- $value = trim($env->real_value, "'");++ $value = trim($resolvedValue, "'");$escapedValue = escapeBashEnvValue($value);if (isDev() && isset($envs_dict[$env->key])) {@@@ -1599,13 -1670,13 +1672,13 @@@if (isDev()) {$this->application_deployment_queue->addLogEntry("[DEBUG] Build-time env: {$env->key}");$this->application_deployment_queue->addLogEntry('[DEBUG] Type: literal/multiline');-- $this->application_deployment_queue->addLogEntry("[DEBUG] raw real_value: {$env->real_value}");++ $this->application_deployment_queue->addLogEntry("[DEBUG] raw real_value: {$resolvedValue}");$this->application_deployment_queue->addLogEntry("[DEBUG] stripped value: {$value}");$this->application_deployment_queue->addLogEntry("[DEBUG] final escaped: {$escapedValue}");}} else {// For normal vars, use double quotes to allow $VAR expansion-- $escapedValue = escapeBashDoubleQuoted($env->real_value);++ $escapedValue = escapeBashDoubleQuoted($resolvedValue);if (isDev() && isset($envs_dict[$env->key])) {$this->application_deployment_queue->addLogEntry("[DEBUG] User override: {$env->key} (was: {$envs_dict[$env->key]}, now: {$escapedValue})");@@@ -1616,7 -1687,7 +1689,7 @@@if (isDev()) {$this->application_deployment_queue->addLogEntry("[DEBUG] Build-time env: {$env->key}");$this->application_deployment_queue->addLogEntry('[DEBUG] Type: normal (allows expansion)');-- $this->application_deployment_queue->addLogEntry("[DEBUG] real_value: {$env->real_value}");++ $this->application_deployment_queue->addLogEntry("[DEBUG] real_value: {$resolvedValue}");$this->application_deployment_queue->addLogEntry("[DEBUG] final escaped: {$escapedValue}");}}@@@ -2313,16 -2392,16 +2394,18 @@@$this->env_nixpacks_args = collect([]);if ($this->pull_request_id === 0) {foreach ($this->application->nixpacks_environment_variables as $env) {- if (! is_null($env->real_value) && $env->real_value !== '') {- $value = ($env->is_literal || $env->is_multiline) ? trim($env->real_value, "'") : $env->real_value;+ $resolvedValue = $env->getResolvedValueWithServer($this->server);+ if (! is_null($resolvedValue) && $resolvedValue !== '') {- $this->env_nixpacks_args->push("--env {$env->key}={$resolvedValue}");++ $value = ($env->is_literal || $env->is_multiline) ? trim($resolvedValue, "'") : $resolvedValue;+ $this->env_nixpacks_args->push('--env '.escapeShellValue("{$env->key}={$value}"));}}} else {foreach ($this->application->nixpacks_environment_variables_preview as $env) {- if (! is_null($env->real_value) && $env->real_value !== '') {- $value = ($env->is_literal || $env->is_multiline) ? trim($env->real_value, "'") : $env->real_value;+ $resolvedValue = $env->getResolvedValueWithServer($this->server);+ if (! is_null($resolvedValue) && $resolvedValue !== '') {- $this->env_nixpacks_args->push("--env {$env->key}={$resolvedValue}");++ $value = ($env->is_literal || $env->is_multiline) ? trim($resolvedValue, "'") : $resolvedValue;+ $this->env_nixpacks_args->push('--env '.escapeShellValue("{$env->key}={$value}"));}}}@@@ -3449,7 -3566,7 +3572,7 @@@ COPY ./nginx.conf /etc/nginx/conf.d/def} else {$secrets_string = $variables->map(function ($env) {-- return "{$env->key}={$env->real_value}";++ return "{$env->key}={$env->getResolvedValueWithServer($this->server)}";})->sort()->implode('|');@@@ -3515,7 -3632,7 +3638,7 @@@if (data_get($env, 'is_multiline') === true) {$argsToInsert->push("ARG {$env->key}");} else {-- $argsToInsert->push("ARG {$env->key}={$env->real_value}");++ $argsToInsert->push("ARG {$env->key}={$env->getResolvedValueWithServer($this->server)}");}}// Add Coolify variables as ARGs@@@ -3537,7 -3654,7 +3660,7 @@@if (data_get($env, 'is_multiline') === true) {$argsToInsert->push("ARG {$env->key}");} else {-- $argsToInsert->push("ARG {$env->key}={$env->real_value}");++ $argsToInsert->push("ARG {$env->key}={$env->getResolvedValueWithServer($this->server)}");}}// Add Coolify variables as ARGs@@@ -3573,7 -3690,7 +3696,7 @@@}}$envs_mapped = $envs->mapWithKeys(function ($env) {-- return [$env->key => $env->real_value];++ return [$env->key => $env->getResolvedValueWithServer($this->server)];});$secrets_hash = $this->generate_secrets_hash($envs_mapped);$argsToInsert->push("ARG COOLIFY_BUILD_SECRETS_HASH={$secrets_hash}");diff --cc app/Models/EnvironmentVariable.phpindex 9308b9ce68,5acd4c1e4e..83212267c8--- a/app/Models/EnvironmentVariable.php+++ b/app/Models/EnvironmentVariable.php@@@ -122,18 -152,13 +152,24 @@@ class EnvironmentVariable extends BaseMreturn null;}+ // Load relationships needed for shared variable resolution+ if (! $resource->relationLoaded('environment')) {+ $resource->load('environment');+ }+ if (! $resource->relationLoaded('server') && method_exists($resource, 'server')) {+ $resource->load('server');+ }+ if (! $resource->relationLoaded('destination') && method_exists($resource, 'destination')) {+ $resource->load('destination.server');+ }+$real_value = $this->get_real_environment_variables($this->value, $resource);++ // Skip escaping for valid JSON objects/arrays to prevent quote corruption (see #6160)+ if (json_validate($real_value) && (str_starts_with($real_value, '{') || str_starts_with($real_value, '['))) {+ return $real_value;+ }+if ($this->is_literal || $this->is_multiline) {$real_value = '\''.$real_value.'\'';} else {@@@ -192,93 -217,11 +228,101 @@@);}+ public function get_real_environment_variables_with_server(?string $environment_variable = null, $resource = null, $server = null)+ {+ return $this->get_real_environment_variables_internal($environment_variable, $resource, $server);+ }++ public function getResolvedValueWithServer($server = null)+ {+ if (! $this->relationLoaded('resourceable')) {+ $this->load('resourceable');+ }+ $resource = $this->resourceable;+ if (! $resource) {+ return null;+ }++ // Load relationships needed for shared variable resolution+ if (! $resource->relationLoaded('environment')) {+ $resource->load('environment');+ }+ if (! $resource->relationLoaded('server') && method_exists($resource, 'server')) {+ $resource->load('server');+ }+ if (! $resource->relationLoaded('destination') && method_exists($resource, 'destination')) {+ $resource->load('destination.server');+ }++ $real_value = $this->get_real_environment_variables_internal($this->value, $resource, $server);++++ // Skip escaping for valid JSON objects/arrays to prevent quote corruption (see #6160)++ if (json_validate($real_value) && (str_starts_with($real_value, '{') || str_starts_with($real_value, '['))) {++ return $real_value;++ }+++ if ($this->is_literal || $this->is_multiline) {+ $real_value = '\''.$real_value.'\'';+ } else {+ $real_value = escapeEnvVariables($real_value);+ }++ return $real_value;+ }++ private function get_real_environment_variables(?string $environment_variable = null, $resource = null)+ {- return resolveSharedEnvironmentVariables($environment_variable, $resource);++ return $this->get_real_environment_variables_internal($environment_variable, $resource);++ }+++ private function get_real_environment_variables_internal(?string $environment_variable = null, $resource = null, $serverOverride = null)+ {- if ((is_null($environment_variable) && $environment_variable === '') || is_null($resource)) {- return null;++ if (is_null($environment_variable) || $environment_variable === '' || is_null($resource)) {++ return $environment_variable;+ }+ $environment_variable = trim($environment_variable);+ $sharedEnvsFound = str($environment_variable)->matchAll('/{{(.*?)}}/');+ if ($sharedEnvsFound->isEmpty()) {+ return $environment_variable;+ }+ foreach ($sharedEnvsFound as $sharedEnv) {+ $type = str($sharedEnv)->trim()->match('/(.*?)\./');+ if (! collect(SHARED_VARIABLE_TYPES)->contains($type)) {+ continue;+ }+ $variable = str($sharedEnv)->trim()->match('/\.(.*)/');++ $id = null;+ if ($type->value() === 'environment') {+ $id = $resource->environment->id;+ } elseif ($type->value() === 'project') {+ $id = $resource->environment->project->id;+ } elseif ($type->value() === 'team') {+ $id = $resource->team()->id;+ } elseif ($type->value() === 'server') {- // Use server override if provided (for deployment context), otherwise use resource's server+ if ($serverOverride) {+ $id = $serverOverride->id;+ } elseif (isset($resource->server) && $resource->server) {+ $id = $resource->server->id;+ } elseif (isset($resource->destination) && $resource->destination && isset($resource->destination->server)) {+ $id = $resource->destination->server->id;- } else {- $id = null;+ }+ }+ if (is_null($id)) {+ continue;+ }- $environment_variable_found = SharedEnvironmentVariable::where('type', $type)->where('key', $variable)->where('team_id', $resource->team()->id)->where("{$type}_id", $id)->first();- if ($environment_variable_found) {- $environment_variable = str($environment_variable)->replace("{{{$sharedEnv}}}", $environment_variable_found->value);++ $found = SharedEnvironmentVariable::where('type', $type)++ ->where('key', $variable)++ ->where('team_id', $resource->team()->id)++ ->where("{$type}_id", $id)++ ->first();++ if ($found) {++ $environment_variable = str($environment_variable)->replace("{{{$sharedEnv}}}", $found->value);+ }+ }++ return str($environment_variable)->value();}- private function get_real_environment_variables(?string $environment_variable = null, $resource = null)- {- return $this->get_real_environment_variables_internal($environment_variable, $resource);- }-private function get_environment_variables(?string $environment_variable = null): ?string{if (! $environment_variable) {diff --cc resources/views/livewire/project/shared/environment-variable/show.blade.phpindex 4dc46bbbbf,d8d448700f..059595221c--- a/resources/views/livewire/project/shared/environment-variable/show.blade.php+++ b/resources/views/livewire/project/shared/environment-variable/show.blade.php@@@ -103,16 -126,42 +126,63 @@@@else@can('update', $this->env)@if ($isDisabled)+ <div class="flex flex-col w-full gap-2">+ <div class="flex flex-col w-full gap-2 lg:flex-row">+ <x-forms.input disabled id="key" />- <x-forms.input disabled type="password" id="value" />++ <x-forms.env-var-input++ disabled++ type="password"++ id="value"++ :availableVars="$isSharedVariable ? [] : $this->availableSharedVariables"++ :projectUuid="data_get($parameters, 'project_uuid')"++ :environmentUuid="data_get($parameters, 'environment_uuid')"++ :serverUuid="data_get($parameters, 'server_uuid')" />+ @if ($is_shared)+ <x-forms.input disabled type="password" id="real_value" />+ @endif+ </div>+ <x-forms.input instantSave id="comment" label="Comment"+ placeholder="{{ $isMagicVariable ? 'This env cannot be edited manually, it is handled by Coolify.' : '' }}"+ helper="Add a note to document what this environment variable is used for." maxlength="256" />+ </div>+ @else+ <div class="flex flex-col w-full gap-2">+ <div class="flex flex-col w-full gap-2 lg:flex-row">+ @if ($is_multiline)+ <x-forms.input :required="$is_redis_credential" isMultiline="{{ $is_multiline }}" id="key" />+ <x-forms.textarea :required="$is_redis_credential" type="password" id="value" />+ @else+ <x-forms.input :disabled="$is_redis_credential" :required="$is_redis_credential" id="key" />- <x-forms.input :required="$is_redis_credential" type="password" id="value" />++ <x-forms.env-var-input++ :required="$is_redis_credential"++ type="password"++ id="value"++ :availableVars="$isSharedVariable ? [] : $this->availableSharedVariables"++ :projectUuid="data_get($parameters, 'project_uuid')"++ :environmentUuid="data_get($parameters, 'environment_uuid')"++ :serverUuid="data_get($parameters, 'server_uuid')" />+ @endif+ @if ($is_shared)+ <x-forms.input :disabled="$is_redis_credential" :required="$is_redis_credential" disabled+ type="password" id="real_value" />+ @endif+ </div>+ <x-forms.input instantSave id="comment" label="Comment"+ helper="Add a note to document what this environment variable is used for." maxlength="256" />+ </div>+ @endif+ @else+ <div class="flex flex-col w-full gap-2"><div class="flex flex-col w-full gap-2 lg:flex-row"><x-forms.input disabled id="key" />- <x-forms.input disabled type="password" id="value" />+ <x-forms.env-var-input+ disabled+ type="password"+ id="value"+ :availableVars="$isSharedVariable ? [] : $this->availableSharedVariables"+ :projectUuid="data_get($parameters, 'project_uuid')"+ :environmentUuid="data_get($parameters, 'environment_uuid')"+ :serverUuid="data_get($parameters, 'server_uuid')" />@if ($is_shared)<x-forms.input disabled type="password" id="real_value" />@endif