fix(deployment): resolve shared env vars using main server

Use `$this->mainServer` when resolving environment variable values across
deployment env generation (runtime, buildtime, nixpacks, args, and secrets
hash) so shared server-scoped values are applied consistently.

Also add `server_id` to `SharedEnvironmentVariable::$fillable` and normalize
the Livewire Blade file newline.
This commit is contained in:
Andras Bacsai 2026-03-31 11:07:52 +02:00
parent cb97a18a78
commit 30751a60df
3 changed files with 15 additions and 14 deletions

View file

@ -1282,7 +1282,7 @@ private function generate_runtime_environment_variables()
});
foreach ($runtime_environment_variables as $env) {
$envs->push($env->key.'='.$env->getResolvedValueWithServer($this->server));
$envs->push($env->key.'='.$env->getResolvedValueWithServer($this->mainServer));
}
// Check for PORT environment variable mismatch with ports_exposes
@ -1348,7 +1348,7 @@ private function generate_runtime_environment_variables()
});
foreach ($runtime_environment_variables_preview as $env) {
$envs->push($env->key.'='.$env->getResolvedValueWithServer($this->server));
$envs->push($env->key.'='.$env->getResolvedValueWithServer($this->mainServer));
}
// Fall back to production env vars for keys not overridden by preview vars,
@ -1362,7 +1362,7 @@ private function generate_runtime_environment_variables()
return $env->is_runtime && ! in_array($env->key, $previewKeys);
});
foreach ($fallback_production_vars as $env) {
$envs->push($env->key.'='.$env->getResolvedValueWithServer($this->server));
$envs->push($env->key.'='.$env->getResolvedValueWithServer($this->mainServer));
}
}
@ -1604,7 +1604,7 @@ private function generate_buildtime_environment_variables()
}
foreach ($sorted_environment_variables as $env) {
$resolvedValue = $env->getResolvedValueWithServer($this->server);
$resolvedValue = $env->getResolvedValueWithServer($this->mainServer);
// For literal/multiline vars, real_value includes quotes that we need to remove
if ($env->is_literal || $env->is_multiline) {
// Strip outer quotes from real_value and apply proper bash escaping
@ -1656,7 +1656,7 @@ private function generate_buildtime_environment_variables()
}
foreach ($sorted_environment_variables as $env) {
$resolvedValue = $env->getResolvedValueWithServer($this->server);
$resolvedValue = $env->getResolvedValueWithServer($this->mainServer);
// For literal/multiline vars, real_value includes quotes that we need to remove
if ($env->is_literal || $env->is_multiline) {
// Strip outer quotes from real_value and apply proper bash escaping
@ -2394,7 +2394,7 @@ private function generate_nixpacks_env_variables()
$this->env_nixpacks_args = collect([]);
if ($this->pull_request_id === 0) {
foreach ($this->application->nixpacks_environment_variables as $env) {
$resolvedValue = $env->getResolvedValueWithServer($this->server);
$resolvedValue = $env->getResolvedValueWithServer($this->mainServer);
if (! is_null($resolvedValue) && $resolvedValue !== '') {
$value = ($env->is_literal || $env->is_multiline) ? trim($resolvedValue, "'") : $resolvedValue;
$this->env_nixpacks_args->push('--env '.escapeShellValue("{$env->key}={$value}"));
@ -2402,7 +2402,7 @@ private function generate_nixpacks_env_variables()
}
} else {
foreach ($this->application->nixpacks_environment_variables_preview as $env) {
$resolvedValue = $env->getResolvedValueWithServer($this->server);
$resolvedValue = $env->getResolvedValueWithServer($this->mainServer);
if (! is_null($resolvedValue) && $resolvedValue !== '') {
$value = ($env->is_literal || $env->is_multiline) ? trim($resolvedValue, "'") : $resolvedValue;
$this->env_nixpacks_args->push('--env '.escapeShellValue("{$env->key}={$value}"));
@ -2543,7 +2543,7 @@ private function generate_env_variables()
->get();
foreach ($envs as $env) {
$resolvedValue = $env->getResolvedValueWithServer($this->server);
$resolvedValue = $env->getResolvedValueWithServer($this->mainServer);
if (! is_null($resolvedValue)) {
$this->env_args->put($env->key, $resolvedValue);
}
@ -2555,7 +2555,7 @@ private function generate_env_variables()
->get();
foreach ($envs as $env) {
$resolvedValue = $env->getResolvedValueWithServer($this->server);
$resolvedValue = $env->getResolvedValueWithServer($this->mainServer);
if (! is_null($resolvedValue)) {
$this->env_args->put($env->key, $resolvedValue);
}
@ -3572,7 +3572,7 @@ private function generate_secrets_hash($variables)
} else {
$secrets_string = $variables
->map(function ($env) {
return "{$env->key}={$env->getResolvedValueWithServer($this->server)}";
return "{$env->key}={$env->getResolvedValueWithServer($this->mainServer)}";
})
->sort()
->implode('|');
@ -3638,7 +3638,7 @@ private function add_build_env_variables_to_dockerfile()
if (data_get($env, 'is_multiline') === true) {
$argsToInsert->push("ARG {$env->key}");
} else {
$argsToInsert->push("ARG {$env->key}={$env->getResolvedValueWithServer($this->server)}");
$argsToInsert->push("ARG {$env->key}={$env->getResolvedValueWithServer($this->mainServer)}");
}
}
// Add Coolify variables as ARGs
@ -3660,7 +3660,7 @@ private function add_build_env_variables_to_dockerfile()
if (data_get($env, 'is_multiline') === true) {
$argsToInsert->push("ARG {$env->key}");
} else {
$argsToInsert->push("ARG {$env->key}={$env->getResolvedValueWithServer($this->server)}");
$argsToInsert->push("ARG {$env->key}={$env->getResolvedValueWithServer($this->mainServer)}");
}
}
// Add Coolify variables as ARGs
@ -3696,7 +3696,7 @@ private function add_build_env_variables_to_dockerfile()
}
}
$envs_mapped = $envs->mapWithKeys(function ($env) {
return [$env->key => $env->getResolvedValueWithServer($this->server)];
return [$env->key => $env->getResolvedValueWithServer($this->mainServer)];
});
$secrets_hash = $this->generate_secrets_hash($envs_mapped);
$argsToInsert->push("ARG COOLIFY_BUILD_SECRETS_HASH={$secrets_hash}");

View file

@ -17,6 +17,7 @@ class SharedEnvironmentVariable extends Model
'team_id',
'project_id',
'environment_id',
'server_id',
// Boolean flags
'is_multiline',

View file

@ -22,4 +22,4 @@
</div>
@endforelse
</div>
</div>
</div>