refactor(environment): standardize service name formatting by replacing '-' and '.' with '_' in environment variable keys

This commit is contained in:
Andras Bacsai
2025-09-11 13:59:02 +02:00
parent 13af4811f5
commit 501e6a2650
6 changed files with 20 additions and 19 deletions

View File

@@ -955,7 +955,7 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue
} }
} }
} else { } else {
$this->env_filename = addPreviewDeploymentSuffix(".env", $this->pull_request_id); $this->env_filename = addPreviewDeploymentSuffix('.env', $this->pull_request_id);
foreach ($sorted_environment_variables_preview as $env) { foreach ($sorted_environment_variables_preview as $env) {
$envs->push($env->key.'='.$env->real_value); $envs->push($env->key.'='.$env->real_value);
} }
@@ -991,7 +991,7 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue
$rawDockerCompose = Yaml::parse($this->application->docker_compose_raw); $rawDockerCompose = Yaml::parse($this->application->docker_compose_raw);
$rawServices = data_get($rawDockerCompose, 'services', []); $rawServices = data_get($rawDockerCompose, 'services', []);
foreach ($rawServices as $rawServiceName => $_) { foreach ($rawServices as $rawServiceName => $_) {
$envs->push('SERVICE_NAME_'.str($rawServiceName)->upper().'='.addPreviewDeploymentSuffix($rawServiceName, $this->pull_request_id)); $envs->push('SERVICE_NAME_'.str($rawServiceName)->upper().'='.addPreviewDeploymentSuffix($rawServiceName, $this->pull_request_id));
} }
} }
} }

View File

@@ -671,7 +671,7 @@ class General extends Component
$domains = collect(json_decode($this->application->docker_compose_domains, true)) ?? collect([]); $domains = collect(json_decode($this->application->docker_compose_domains, true)) ?? collect([]);
foreach ($domains as $serviceName => $service) { foreach ($domains as $serviceName => $service) {
$serviceNameFormatted = str($serviceName)->upper()->replace('-', '_'); $serviceNameFormatted = str($serviceName)->upper()->replace('-', '_')->replace('.', '_');
$domain = data_get($service, 'domain'); $domain = data_get($service, 'domain');
// Delete SERVICE_FQDN_ and SERVICE_URL_ variables if domain is removed // Delete SERVICE_FQDN_ and SERVICE_URL_ variables if domain is removed
$this->application->environment_variables()->where('resourceable_type', Application::class) $this->application->environment_variables()->where('resourceable_type', Application::class)

View File

@@ -1474,14 +1474,14 @@ class Application extends BaseModel
$json = collect(json_decode($this->docker_compose_domains)); $json = collect(json_decode($this->docker_compose_domains));
foreach ($json as $key => $value) { foreach ($json as $key => $value) {
if (str($key)->contains('-')) { if (str($key)->contains('-')) {
$key = str($key)->replace('-', '_'); $key = str($key)->replace('-', '_')->replace('.', '_');
} }
$json->put((string) $key, $value); $json->put((string) $key, $value);
} }
$services = collect(data_get($parsedServices, 'services', [])); $services = collect(data_get($parsedServices, 'services', []));
foreach ($services as $name => $service) { foreach ($services as $name => $service) {
if (str($name)->contains('-')) { if (str($name)->contains('-')) {
$replacedName = str($name)->replace('-', '_'); $replacedName = str($name)->replace('-', '_')->replace('.', '_');
$services->put((string) $replacedName, $service); $services->put((string) $replacedName, $service);
$services->forget((string) $name); $services->forget((string) $name);
} }

View File

@@ -373,7 +373,7 @@ function applicationParser(Application $resource, int $pull_request_id = 0, ?int
$fqdnFor = $key->after('SERVICE_FQDN_')->lower()->value(); $fqdnFor = $key->after('SERVICE_FQDN_')->lower()->value();
$originalFqdnFor = str($fqdnFor)->replace('_', '-'); $originalFqdnFor = str($fqdnFor)->replace('_', '-');
if (str($fqdnFor)->contains('-')) { if (str($fqdnFor)->contains('-')) {
$fqdnFor = str($fqdnFor)->replace('-', '_'); $fqdnFor = str($fqdnFor)->replace('-', '_')->replace('.', '_');
} }
// Generated FQDN & URL // Generated FQDN & URL
$fqdn = generateFqdn(server: $server, random: "$originalFqdnFor-$uuid", parserVersion: $resource->compose_parsing_version); $fqdn = generateFqdn(server: $server, random: "$originalFqdnFor-$uuid", parserVersion: $resource->compose_parsing_version);
@@ -409,7 +409,7 @@ function applicationParser(Application $resource, int $pull_request_id = 0, ?int
$urlFor = $key->after('SERVICE_URL_')->lower()->value(); $urlFor = $key->after('SERVICE_URL_')->lower()->value();
$originalUrlFor = str($urlFor)->replace('_', '-'); $originalUrlFor = str($urlFor)->replace('_', '-');
if (str($urlFor)->contains('-')) { if (str($urlFor)->contains('-')) {
$urlFor = str($urlFor)->replace('-', '_'); $urlFor = str($urlFor)->replace('-', '_')->replace('.', '_');
} }
$url = generateUrl(server: $server, random: "$originalUrlFor-$uuid"); $url = generateUrl(server: $server, random: "$originalUrlFor-$uuid");
$resource->environment_variables()->firstOrCreate([ $resource->environment_variables()->firstOrCreate([
@@ -864,13 +864,13 @@ function applicationParser(Application $resource, int $pull_request_id = 0, ?int
if ($resource->build_pack !== 'dockercompose') { if ($resource->build_pack !== 'dockercompose') {
$domains = collect([]); $domains = collect([]);
} }
$changedServiceName = str($serviceName)->replace('-', '_')->value(); $changedServiceName = str($serviceName)->replace('-', '_')->replace('.', '_')->value();
$fqdns = data_get($domains, "$changedServiceName.domain"); $fqdns = data_get($domains, "$changedServiceName.domain");
// Generate SERVICE_FQDN & SERVICE_URL for dockercompose // Generate SERVICE_FQDN & SERVICE_URL for dockercompose
if ($resource->build_pack === 'dockercompose') { if ($resource->build_pack === 'dockercompose') {
foreach ($domains as $forServiceName => $domain) { foreach ($domains as $forServiceName => $domain) {
$parsedDomain = data_get($domain, 'domain'); $parsedDomain = data_get($domain, 'domain');
$serviceNameFormatted = str($serviceName)->upper()->replace('-', '_'); $serviceNameFormatted = str($serviceName)->upper()->replace('-', '_')->replace('.', '_');
if (filled($parsedDomain)) { if (filled($parsedDomain)) {
$parsedDomain = str($parsedDomain)->explode(',')->first(); $parsedDomain = str($parsedDomain)->explode(',')->first();
@@ -878,12 +878,12 @@ function applicationParser(Application $resource, int $pull_request_id = 0, ?int
$coolifyScheme = $coolifyUrl->getScheme(); $coolifyScheme = $coolifyUrl->getScheme();
$coolifyFqdn = $coolifyUrl->getHost(); $coolifyFqdn = $coolifyUrl->getHost();
$coolifyUrl = $coolifyUrl->withScheme($coolifyScheme)->withHost($coolifyFqdn)->withPort(null); $coolifyUrl = $coolifyUrl->withScheme($coolifyScheme)->withHost($coolifyFqdn)->withPort(null);
$coolifyEnvironments->put('SERVICE_URL_'.str($forServiceName)->upper()->replace('-', '_'), $coolifyUrl->__toString()); $coolifyEnvironments->put('SERVICE_URL_'.str($forServiceName)->upper()->replace('-', '_')->replace('.', '_'), $coolifyUrl->__toString());
$coolifyEnvironments->put('SERVICE_FQDN_'.str($forServiceName)->upper()->replace('-', '_'), $coolifyFqdn); $coolifyEnvironments->put('SERVICE_FQDN_'.str($forServiceName)->upper()->replace('-', '_')->replace('.', '_'), $coolifyFqdn);
$resource->environment_variables()->updateOrCreate([ $resource->environment_variables()->updateOrCreate([
'resourceable_type' => Application::class, 'resourceable_type' => Application::class,
'resourceable_id' => $resource->id, 'resourceable_id' => $resource->id,
'key' => 'SERVICE_URL_'.str($forServiceName)->upper()->replace('-', '_'), 'key' => 'SERVICE_URL_'.str($forServiceName)->upper()->replace('-', '_')->replace('.', '_'),
], [ ], [
'value' => $coolifyUrl->__toString(), 'value' => $coolifyUrl->__toString(),
'is_build_time' => false, 'is_build_time' => false,
@@ -892,7 +892,7 @@ function applicationParser(Application $resource, int $pull_request_id = 0, ?int
$resource->environment_variables()->updateOrCreate([ $resource->environment_variables()->updateOrCreate([
'resourceable_type' => Application::class, 'resourceable_type' => Application::class,
'resourceable_id' => $resource->id, 'resourceable_id' => $resource->id,
'key' => 'SERVICE_FQDN_'.str($forServiceName)->upper()->replace('-', '_'), 'key' => 'SERVICE_FQDN_'.str($forServiceName)->upper()->replace('-', '_')->replace('.', '_'),
], [ ], [
'value' => $coolifyFqdn, 'value' => $coolifyFqdn,
'is_build_time' => false, 'is_build_time' => false,

View File

@@ -114,14 +114,14 @@ function updateCompose(ServiceApplication|ServiceDatabase $resource)
$resource->save(); $resource->save();
} }
$serviceName = str($resource->name)->upper()->replace('-', '_'); $serviceName = str($resource->name)->upper()->replace('-', '_')->replace('.', '_');
$resource->service->environment_variables()->where('key', 'LIKE', "SERVICE_FQDN_{$serviceName}%")->delete(); $resource->service->environment_variables()->where('key', 'LIKE', "SERVICE_FQDN_{$serviceName}%")->delete();
$resource->service->environment_variables()->where('key', 'LIKE', "SERVICE_URL_{$serviceName}%")->delete(); $resource->service->environment_variables()->where('key', 'LIKE', "SERVICE_URL_{$serviceName}%")->delete();
if ($resource->fqdn) { if ($resource->fqdn) {
$resourceFqdns = str($resource->fqdn)->explode(','); $resourceFqdns = str($resource->fqdn)->explode(',');
$resourceFqdns = $resourceFqdns->first(); $resourceFqdns = $resourceFqdns->first();
$variableName = 'SERVICE_URL_'.str($resource->name)->upper()->replace('-', '_'); $variableName = 'SERVICE_URL_'.str($resource->name)->upper()->replace('-', '_')->replace('.', '_');
$url = Url::fromString($resourceFqdns); $url = Url::fromString($resourceFqdns);
$port = $url->getPort(); $port = $url->getPort();
$path = $url->getPath(); $path = $url->getPath();
@@ -148,7 +148,7 @@ function updateCompose(ServiceApplication|ServiceDatabase $resource)
'is_preview' => false, 'is_preview' => false,
]); ]);
} }
$variableName = 'SERVICE_FQDN_'.str($resource->name)->upper()->replace('-', '_'); $variableName = 'SERVICE_FQDN_'.str($resource->name)->upper()->replace('-', '_')->replace('.', '_');
$fqdn = Url::fromString($resourceFqdns); $fqdn = Url::fromString($resourceFqdns);
$port = $fqdn->getPort(); $port = $fqdn->getPort();
$path = $fqdn->getPath(); $path = $fqdn->getPath();

View File

@@ -3003,14 +3003,15 @@ function parseDockerfileInterval(string $something)
function addPreviewDeploymentSuffix(string $name, int $pull_request_id = 0): string function addPreviewDeploymentSuffix(string $name, int $pull_request_id = 0): string
{ {
return ($pull_request_id === 0)? $name : $name.'-pr-'.$pull_request_id; return ($pull_request_id === 0) ? $name : $name.'-pr-'.$pull_request_id;
} }
function generateDockerComposeServiceName(mixed $services, int $pullRequestId = 0) : Collection function generateDockerComposeServiceName(mixed $services, int $pullRequestId = 0): Collection
{ {
$collection = collect([]); $collection = collect([]);
foreach ($services as $serviceName => $_) { foreach ($services as $serviceName => $_) {
$collection->put('SERVICE_NAME_'.str($serviceName)->upper(), addPreviewDeploymentSuffix($serviceName,$pullRequestId)); $collection->put('SERVICE_NAME_'.str($serviceName)->replace('-', '_')->replace('.', '_')->upper(), addPreviewDeploymentSuffix($serviceName, $pullRequestId));
} }
return $collection; return $collection;
} }