Skip to content

Commit 390f633

Browse files
authored
Merge pull request #6757 from coollabsio/next
feat(deployments): generate SERVICE_NAME environment variables from Docker Compose services
2 parents d7819be + f8e9750 commit 390f633

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

app/Models/Service.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1263,6 +1263,21 @@ public function saveComposeConfigs()
12631263
$commands[] = "cd $workdir";
12641264
$commands[] = 'rm -f .env || true';
12651265

1266+
$envs = collect([]);
1267+
1268+
// Generate SERVICE_NAME_* environment variables from docker-compose services
1269+
if ($this->docker_compose) {
1270+
try {
1271+
$dockerCompose = \Symfony\Component\Yaml\Yaml::parse($this->docker_compose);
1272+
$services = data_get($dockerCompose, 'services', []);
1273+
foreach ($services as $serviceName => $_) {
1274+
$envs->push('SERVICE_NAME_'.str($serviceName)->replace('-', '_')->replace('.', '_')->upper().'='.$serviceName);
1275+
}
1276+
} catch (\Exception $e) {
1277+
ray($e->getMessage());
1278+
}
1279+
}
1280+
12661281
$envs_from_coolify = $this->environment_variables()->get();
12671282
$sorted = $envs_from_coolify->sortBy(function ($env) {
12681283
if (str($env->key)->startsWith('SERVICE_')) {
@@ -1274,7 +1289,6 @@ public function saveComposeConfigs()
12741289

12751290
return 3;
12761291
});
1277-
$envs = collect([]);
12781292
foreach ($sorted as $env) {
12791293
$envs->push("{$env->key}={$env->real_value}");
12801294
}

bootstrap/helpers/parsers.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,9 @@ function serviceParser(Service $resource): Collection
11721172

11731173
$parsedServices = collect([]);
11741174

1175+
// Generate SERVICE_NAME variables for docker compose services
1176+
$serviceNameEnvironments = generateDockerComposeServiceName($services);
1177+
11751178
$allMagicEnvironments = collect([]);
11761179
// Presave services
11771180
foreach ($services as $serviceName => $service) {
@@ -1988,7 +1991,7 @@ function serviceParser(Service $resource): Collection
19881991
$payload['volumes'] = $volumesParsed;
19891992
}
19901993
if ($environment->count() > 0 || $coolifyEnvironments->count() > 0) {
1991-
$payload['environment'] = $environment->merge($coolifyEnvironments);
1994+
$payload['environment'] = $environment->merge($coolifyEnvironments)->merge($serviceNameEnvironments);
19921995
}
19931996
if ($logging) {
19941997
$payload['logging'] = $logging;

0 commit comments

Comments
 (0)