Skip to content

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.

fix(deployments): keep every domain intact in COOLIFY_URL and COOLIFY… (#11527)

Devin Dissanayakae0d233f933f5
diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php
index 7168aea5fb..48547c4d9b 100644
--- a/app/Jobs/ApplicationDeploymentJob.php
+++ b/app/Jobs/ApplicationDeploymentJob.php
@@ -2280,9 +2280,9 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue
$fqdn = $this->preview->fqdn;
}
if (isset($fqdn)) {
- $url = Url::fromString($fqdn);
- $fqdn = $url->getHost();
- $url = $url->withHost($fqdn)->withPort(null)->__toString();
+ $domains = str($fqdn)->explode(',')->map(fn (string $domain) => trim($domain))->filter();
+ $url = $domains->map(fn (string $domain) => Url::fromString($domain)->withPort(null)->__toString())->implode(',');
+ $fqdn = $domains->map(fn (string $domain) => Url::fromString($domain)->getHost())->implode(',');
if ((int) $this->application->compose_parsing_version >= 3) {
$this->coolify_variables .= 'COOLIFY_URL='.escapeShellValue($url).' ';
$this->coolify_variables .= 'COOLIFY_FQDN='.escapeShellValue($fqdn).' ';
diff --git a/tests/Unit/ApplicationDeploymentCoolifyUrlDomainsTest.php b/tests/Unit/ApplicationDeploymentCoolifyUrlDomainsTest.php
new file mode 100644
index 0000000000..f6684718e3
--- /dev/null
+++ b/tests/Unit/ApplicationDeploymentCoolifyUrlDomainsTest.php
@@ -0,0 +1,94 @@
+<?php
+
+use App\Jobs\ApplicationDeploymentJob;
+use App\Models\Application;
+use App\Models\Environment;
+use App\Models\Project;
+use App\Models\Server;
+use App\Models\StandaloneDocker;
+use App\Models\Team;
+use Illuminate\Foundation\Testing\RefreshDatabase;
+use Tests\TestCase;
+
+uses(TestCase::class, RefreshDatabase::class);
+
+class TestableCoolifyUrlDeploymentJob extends ApplicationDeploymentJob
+{
+ public function __construct() {}
+
+ public function execute_remote_command(...$commands): void {}
+}
+
+function coolifyVariablesForFqdn(string $fqdn, string $composeParsingVersion = '3'): string
+{
+ $team = Team::create([
+ 'name' => 'Coolify Url Team',
+ 'personal_team' => false,
+ 'show_boarding' => false,
+ ]);
+ $project = Project::create([
+ 'name' => 'Coolify Url Project',
+ 'team_id' => $team->id,
+ ]);
+ $environment = Environment::where('project_id', $project->id)->firstOrFail();
+ $server = Server::factory()->create(['team_id' => $team->id]);
+ $destination = $server->standaloneDockers()->firstOrFail();
+ $application = Application::factory()->create([
+ 'environment_id' => $environment->id,
+ 'destination_id' => $destination->id,
+ 'destination_type' => StandaloneDocker::class,
+ 'build_pack' => 'dockercompose',
+ 'fqdn' => $fqdn,
+ ]);
+
+ // The created hook resets this, so it has to be set afterwards.
+ $application->compose_parsing_version = $composeParsingVersion;
+ $application->save();
+
+ $job = new TestableCoolifyUrlDeploymentJob;
+ $reflection = new ReflectionClass(ApplicationDeploymentJob::class);
+
+ foreach ([
+ 'application' => $application->fresh(),
+ 'pull_request_id' => 0,
+ 'commit' => 'HEAD',
+ ] as $property => $value) {
+ $reflection->getProperty($property)->setValue($job, $value);
+ }
+
+ $reflection->getMethod('set_coolify_variables')->invoke($job);
+
+ return $reflection->getProperty('coolify_variables')->getValue($job);
+}
+
+it('keeps every domain intact when an application has multiple domains', function () {
+ $variables = coolifyVariablesForFqdn('https://a.example.com,https://b.example.com');
+
+ expect($variables)
+ ->toContain("COOLIFY_URL='https://a.example.com,https://b.example.com'")
+ ->toContain("COOLIFY_FQDN='a.example.com,b.example.com'");
+});
+
+it('strips the port from every domain when an application has multiple domains', function () {
+ $variables = coolifyVariablesForFqdn('https://a.example.com:8080,https://b.example.com:9000');
+
+ expect($variables)
+ ->toContain("COOLIFY_URL='https://a.example.com,https://b.example.com'")
+ ->toContain("COOLIFY_FQDN='a.example.com,b.example.com'");
+});
+
+it('keeps every domain intact on the legacy compose parsing version', function () {
+ $variables = coolifyVariablesForFqdn('https://a.example.com,https://b.example.com', '2');
+
+ expect($variables)
+ ->toContain("COOLIFY_URL='a.example.com,b.example.com'")
+ ->toContain("COOLIFY_FQDN='https://a.example.com,https://b.example.com'");
+});
+
+it('still resolves a single domain', function () {
+ $variables = coolifyVariablesForFqdn('https://a.example.com');
+
+ expect($variables)
+ ->toContain("COOLIFY_URL='https://a.example.com'")
+ ->toContain("COOLIFY_FQDN='a.example.com'");
+});