refactor(deployment): streamline environment variable generation in ApplicationDeploymentJob

- Removed redundant logic for merging COOLIFY_* variables into env_args.
- Simplified the process of adding environment variables by directly incorporating generated COOLIFY environment variables.
- Enhanced clarity and maintainability of the generate_env_variables method.
This commit is contained in:
Andras Bacsai 2025-10-04 15:06:49 +02:00
parent c14497303f
commit 158747c8b1

View file

@ -1965,10 +1965,14 @@ private function generate_env_variables()
{ {
$this->env_args = collect([]); $this->env_args = collect([]);
$this->env_args->put('SOURCE_COMMIT', $this->commit); $this->env_args->put('SOURCE_COMMIT', $this->commit);
$coolify_envs = $this->generate_coolify_env_variables(); $coolify_envs = $this->generate_coolify_env_variables();
$coolify_envs->each(function ($value, $key) {
$this->env_args->put($key, $value);
});
// For build process, include only environment variables where is_buildtime = true // For build process, include only environment variables where is_buildtime = true
if ($this->pull_request_id === 0) { if ($this->pull_request_id === 0) {
// Get environment variables that are marked as available during build
$envs = $this->application->environment_variables() $envs = $this->application->environment_variables()
->where('key', 'not like', 'NIXPACKS_%') ->where('key', 'not like', 'NIXPACKS_%')
->where('is_buildtime', true) ->where('is_buildtime', true)
@ -1977,24 +1981,9 @@ private function generate_env_variables()
foreach ($envs as $env) { foreach ($envs as $env) {
if (! is_null($env->real_value)) { if (! is_null($env->real_value)) {
$this->env_args->put($env->key, $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 { } else {
// Get preview environment variables that are marked as available during build
$envs = $this->application->environment_variables_preview() $envs = $this->application->environment_variables_preview()
->where('key', 'not like', 'NIXPACKS_%') ->where('key', 'not like', 'NIXPACKS_%')
->where('is_buildtime', true) ->where('is_buildtime', true)
@ -2003,29 +1992,9 @@ private function generate_env_variables()
foreach ($envs as $env) { foreach ($envs as $env) {
if (! is_null($env->real_value)) { if (! is_null($env->real_value)) {
$this->env_args->put($env->key, $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);
}
}
}
} }
} }
} }
// Merge COOLIFY_* variables into env_args for build process
// This ensures they're available for both build args and build secrets
$coolify_envs->each(function ($value, $key) {
$this->env_args->put($key, $value);
});
} }
private function generate_compose_file() private function generate_compose_file()