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(shared-variables): support direct mount params and comment field for server variables

Andras Bacsaiacb716cb900d
diff --cc app/Livewire/SharedVariables/Environment/Show.php
index 9405b452ac,9405b452ac..bfbdf9212f
--- a/app/Livewire/SharedVariables/Environment/Show.php
+++ b/app/Livewire/SharedVariables/Environment/Show.php
@@@ -51,11 -51,11 +51,14 @@@ class Show extends Componen
}
}
-- public function mount()
++ public function mount(?string $project_uuid = null, ?string $environment_uuid = null)
{
$this->parameters = get_route_parameters();
-- $this->project = Project::ownedByCurrentTeam()->where('uuid', request()->route('project_uuid'))->firstOrFail();
-- $this->environment = $this->project->environments()->where('uuid', request()->route('environment_uuid'))->firstOrFail();
++ $projectUuid = $project_uuid ?? request()->route('project_uuid');
++ $environmentUuid = $environment_uuid ?? request()->route('environment_uuid');
++
++ $this->project = Project::ownedByCurrentTeam()->where('uuid', $projectUuid)->firstOrFail();
++ $this->environment = $this->project->environments()->where('uuid', $environmentUuid)->firstOrFail();
$this->getDevView();
}
diff --cc app/Livewire/SharedVariables/Project/Show.php
index 7753a4027d,7753a4027d..c9f0dcd8ee
--- a/app/Livewire/SharedVariables/Project/Show.php
+++ b/app/Livewire/SharedVariables/Project/Show.php
@@@ -44,9 -44,9 +44,9 @@@ class Show extends Componen
}
}
-- public function mount()
++ public function mount(?string $project_uuid = null)
{
-- $projectUuid = request()->route('project_uuid');
++ $projectUuid = $project_uuid ?? request()->route('project_uuid');
$teamId = currentTeam()->id;
$project = Project::where('team_id', $teamId)->where('uuid', $projectUuid)->first();
if (! $project) {
diff --cc app/Livewire/SharedVariables/Server/Show.php
index 6078ef36fc,0000000000..a0498b2b7d
mode 100644,000000..100644
--- a/app/Livewire/SharedVariables/Server/Show.php
+++ b/app/Livewire/SharedVariables/Server/Show.php
@@@ -1,184 -1,0 +1,190 @@@
+<?php
+
+namespace App\Livewire\SharedVariables\Server;
+
+use App\Models\Server;
+use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
+use Illuminate\Support\Facades\DB;
+use Livewire\Component;
+
+class Show extends Component
+{
+ use AuthorizesRequests;
+
+ public Server $server;
+
+ public string $view = 'normal';
+
+ public ?string $variables = null;
+
+ protected $listeners = ['refreshEnvs' => 'refreshEnvs', 'saveKey' => 'saveKey', 'environmentVariableDeleted' => 'refreshEnvs'];
+
+ public function saveKey($data)
+ {
+ try {
+ $this->authorize('update', $this->server);
+
+ if (in_array($data['key'], ['COOLIFY_SERVER_UUID', 'COOLIFY_SERVER_NAME'])) {
+ throw new \Exception('Cannot create predefined variable.');
+ }
+
+ $found = $this->server->environment_variables()->where('key', $data['key'])->first();
+ if ($found) {
+ throw new \Exception('Variable already exists.');
+ }
+ $this->server->environment_variables()->create([
+ 'key' => $data['key'],
+ 'value' => $data['value'],
+ 'is_multiline' => $data['is_multiline'],
+ 'is_literal' => $data['is_literal'],
++ 'comment' => $data['comment'] ?? null,
+ 'type' => 'server',
+ 'team_id' => currentTeam()->id,
+ ]);
+ $this->server->refresh();
+ $this->getDevView();
+ } catch (\Throwable $e) {
+ return handleError($e, $this);
+ }
+ }
+
- public function mount()
++ public function mount(?string $server_uuid = null)
+ {
- $serverUuid = request()->route('server_uuid');
++ $serverUuid = $server_uuid ?? request()->route('server_uuid');
+ $teamId = currentTeam()->id;
+ $server = Server::where('team_id', $teamId)->where('uuid', $serverUuid)->first();
+ if (! $server) {
+ return redirect()->route('dashboard');
+ }
+ $this->authorize('view', $server);
+ $this->server = $server;
+ $this->getDevView();
+ }
+
+ public function switch()
+ {
+ $this->authorize('view', $this->server);
+ $this->view = $this->view === 'normal' ? 'dev' : 'normal';
+ $this->getDevView();
+ }
+
+ public function getDevView()
+ {
+ $this->variables = $this->formatEnvironmentVariables($this->server->environment_variables->whereNotIn('key', ['COOLIFY_SERVER_UUID', 'COOLIFY_SERVER_NAME'])->sortBy('key'));
+ }
+
+ private function formatEnvironmentVariables($variables)
+ {
+ return $variables->map(function ($item) {
+ if ($item->is_shown_once) {
+ return "$item->key=(Locked Secret, delete and add again to change)";
+ }
+ if ($item->is_multiline) {
+ return "$item->key=(Multiline environment variable, edit in normal view)";
+ }
+
+ return "$item->key=$item->value";
+ })->join("\n");
+ }
+
+ public function submit()
+ {
+ try {
+ $this->authorize('update', $this->server);
+ $this->handleBulkSubmit();
+ $this->getDevView();
+ } catch (\Throwable $e) {
+ return handleError($e, $this);
+ } finally {
+ $this->refreshEnvs();
+ }
+ }
+
+ private function handleBulkSubmit()
+ {
+ $variables = parseEnvFormatToArray($this->variables);
+
+ $changesMade = DB::transaction(function () use ($variables) {
+ // Delete removed variables
+ $deletedCount = $this->deleteRemovedVariables($variables);
+
+ // Update or create variables
+ $updatedCount = $this->updateOrCreateVariables($variables);
+
+ return $deletedCount > 0 || $updatedCount > 0;
+ });
+
+ if ($changesMade) {
+ $this->dispatch('success', 'Environment variables updated.');
+ }
+ }
+
+ private function deleteRemovedVariables($variables)
+ {
+ $variablesToDelete = $this->server->environment_variables()
+ ->whereNotIn('key', array_keys($variables))
+ ->whereNotIn('key', ['COOLIFY_SERVER_UUID', 'COOLIFY_SERVER_NAME'])
+ ->get();
+
+ if ($variablesToDelete->isEmpty()) {
+ return 0;
+ }
+
+ $this->server->environment_variables()
+ ->whereNotIn('key', array_keys($variables))
+ ->whereNotIn('key', ['COOLIFY_SERVER_UUID', 'COOLIFY_SERVER_NAME'])
+ ->delete();
+
+ return $variablesToDelete->count();
+ }
+
+ private function updateOrCreateVariables($variables)
+ {
+ $count = 0;
- foreach ($variables as $key => $value) {
++ foreach ($variables as $key => $data) {
++ $value = is_array($data) ? ($data['value'] ?? '') : $data;
++ $comment = is_array($data) ? ($data['comment'] ?? null) : null;
++
+ // Skip predefined variables
+ if (in_array($key, ['COOLIFY_SERVER_UUID', 'COOLIFY_SERVER_NAME'])) {
+ continue;
+ }
+ $found = $this->server->environment_variables()->where('key', $key)->first();
+
+ if ($found) {
+ if (! $found->is_shown_once && ! $found->is_multiline) {
- if ($found->value !== $value) {
++ if ($found->value !== $value || $found->comment !== $comment) {
+ $found->value = $value;
++ $found->comment = $comment;
+ $found->save();
+ $count++;
+ }
+ }
+ } else {
+ $this->server->environment_variables()->create([
+ 'key' => $key,
+ 'value' => $value,
++ 'comment' => $comment,
+ 'is_multiline' => false,
+ 'is_literal' => false,
+ 'type' => 'server',
+ 'team_id' => currentTeam()->id,
+ ]);
+ $count++;
+ }
+ }
+
+ return $count;
+ }
+
+ public function refreshEnvs()
+ {
+ $this->server->refresh();
+ $this->getDevView();
+ }
+
+ public function render()
+ {
+ return view('livewire.shared-variables.server.show');
+ }
+}
diff --cc database/migrations/2025_12_24_095507_add_server_to_shared_environment_variables_table.php
index a6a6fe872f,0000000000..66d5850692
mode 100644,000000..100644
--- a/database/migrations/2025_12_24_095507_add_server_to_shared_environment_variables_table.php
+++ b/database/migrations/2025_12_24_095507_add_server_to_shared_environment_variables_table.php
@@@ -1,43 -1,0 +1,47 @@@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+ /**
+ * Run the migrations.
+ */
+ public function up(): void
+ {
+ DB::transaction(function () {
- DB::statement('ALTER TABLE shared_environment_variables DROP CONSTRAINT IF EXISTS shared_environment_variables_type_check');
- DB::statement("ALTER TABLE shared_environment_variables ADD CONSTRAINT shared_environment_variables_type_check CHECK (type IN ('team', 'project', 'environment', 'server'))");
++ if (DB::getDriverName() !== 'sqlite') {
++ DB::statement('ALTER TABLE shared_environment_variables DROP CONSTRAINT IF EXISTS shared_environment_variables_type_check');
++ DB::statement("ALTER TABLE shared_environment_variables ADD CONSTRAINT shared_environment_variables_type_check CHECK (type IN ('team', 'project', 'environment', 'server'))");
++ }
+ Schema::table('shared_environment_variables', function (Blueprint $table) {
+ $table->foreignId('server_id')->nullable()->constrained()->onDelete('cascade');
+ // NULL != NULL in PostgreSQL unique indexes, so this only enforces uniqueness
+ // for server-scoped rows (where server_id is non-null). Other scopes are covered
+ // by existing unique constraints on ['key', 'project_id', 'team_id'] and ['key', 'environment_id', 'team_id'].
+ $table->unique(['key', 'server_id', 'team_id']);
+ });
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ */
+ public function down(): void
+ {
+ DB::transaction(function () {
+ Schema::table('shared_environment_variables', function (Blueprint $table) {
+ $table->dropUnique(['key', 'server_id', 'team_id']);
+ $table->dropForeign(['server_id']);
+ $table->dropColumn('server_id');
+ });
- DB::statement('ALTER TABLE shared_environment_variables DROP CONSTRAINT IF EXISTS shared_environment_variables_type_check');
- DB::statement("ALTER TABLE shared_environment_variables ADD CONSTRAINT shared_environment_variables_type_check CHECK (type IN ('team', 'project', 'environment'))");
++ if (DB::getDriverName() !== 'sqlite') {
++ DB::statement('ALTER TABLE shared_environment_variables DROP CONSTRAINT IF EXISTS shared_environment_variables_type_check');
++ DB::statement("ALTER TABLE shared_environment_variables ADD CONSTRAINT shared_environment_variables_type_check CHECK (type IN ('team', 'project', 'environment'))");
++ }
+ });
+ }
+};
diff --cc tests/Feature/SharedVariableDevViewTest.php
index 779be26a9a,779be26a9a..34767cf06d
--- a/tests/Feature/SharedVariableDevViewTest.php
+++ b/tests/Feature/SharedVariableDevViewTest.php
@@@ -1,7 -1,7 +1,11 @@@
<?php
++use App\Livewire\SharedVariables\Environment\Show;
++use App\Livewire\SharedVariables\Team\Index;
use App\Models\Environment;
++use App\Models\InstanceSettings;
use App\Models\Project;
++use App\Models\Server;
use App\Models\SharedEnvironmentVariable;
use App\Models\Team;
use App\Models\User;
@@@ -19,13 -19,13 +23,35 @@@ beforeEach(function ()
$this->environment = Environment::factory()->create([
'project_id' => $this->project->id,
]);
++ InstanceSettings::unguarded(function () {
++ InstanceSettings::updateOrCreate([
++ 'id' => 0,
++ ], [
++ 'is_registration_enabled' => true,
++ 'is_api_enabled' => true,
++ 'smtp_enabled' => true,
++ 'smtp_host' => 'localhost',
++ 'smtp_port' => 1025,
++ 'smtp_from_address' => '[email protected]',
++ 'smtp_from_name' => 'Coolify',
++ ]);
++ });
$this->actingAs($this->user);
session(['currentTeam' => $this->team]);
});
++afterEach(function () {
++ request()->setRouteResolver(function () {
++ return null;
++ });
++});
++
test('environment shared variable dev view saves without openssl_encrypt error', function () {
-- Livewire::test(\App\Livewire\SharedVariables\Environment\Show::class)
++ Livewire::test(Show::class, [
++ 'project_uuid' => $this->project->uuid,
++ 'environment_uuid' => $this->environment->uuid,
++ ])
->set('variables', "MY_VAR=my_value\nANOTHER_VAR=another_value")
->call('submit')
->assertHasNoErrors();
@@@ -38,7 -38,7 +64,9 @@@
});
test('project shared variable dev view saves without openssl_encrypt error', function () {
-- Livewire::test(\App\Livewire\SharedVariables\Project\Show::class)
++ Livewire::test(App\Livewire\SharedVariables\Project\Show::class, [
++ 'project_uuid' => $this->project->uuid,
++ ])
->set('variables', 'PROJ_VAR=proj_value')
->call('submit')
->assertHasNoErrors();
@@@ -49,7 -49,7 +77,7 @@@
});
test('team shared variable dev view saves without openssl_encrypt error', function () {
-- Livewire::test(\App\Livewire\SharedVariables\Team\Index::class)
++ Livewire::test(Index::class)
->set('variables', 'TEAM_VAR=team_value')
->call('submit')
->assertHasNoErrors();
@@@ -69,7 -69,7 +97,10 @@@ test('environment shared variable dev v
'team_id' => $this->team->id,
]);
-- Livewire::test(\App\Livewire\SharedVariables\Environment\Show::class)
++ Livewire::test(Show::class, [
++ 'project_uuid' => $this->project->uuid,
++ 'environment_uuid' => $this->environment->uuid,
++ ])
->set('variables', 'EXISTING_VAR=new_value')
->call('submit')
->assertHasNoErrors();
@@@ -77,3 -77,3 +108,63 @@@
$var = $this->environment->environment_variables()->where('key', 'EXISTING_VAR')->first();
expect($var->value)->toBe('new_value');
});
++
++test('server shared variable dev view saves without openssl_encrypt error', function () {
++ $this->server = Server::factory()->create(['team_id' => $this->team->id]);
++
++ Livewire::test(App\Livewire\SharedVariables\Server\Show::class, [
++ 'server_uuid' => $this->server->uuid,
++ ])
++ ->set('variables', "SERVER_VAR=server_value\nSECOND_SERVER_VAR=second_value")
++ ->call('submit')
++ ->assertHasNoErrors();
++
++ $vars = $this->server->environment_variables()->pluck('value', 'key')->toArray();
++
++ expect($vars)->toHaveKey('SERVER_VAR')
++ ->and($vars['SERVER_VAR'])->toBe('server_value')
++ ->and($vars)->toHaveKey('SECOND_SERVER_VAR')
++ ->and($vars['SECOND_SERVER_VAR'])->toBe('second_value');
++});
++
++test('server shared variable dev view preserves inline comments', function () {
++ $this->server = Server::factory()->create(['team_id' => $this->team->id]);
++
++ Livewire::test(App\Livewire\SharedVariables\Server\Show::class, [
++ 'server_uuid' => $this->server->uuid,
++ ])
++ ->set('variables', 'COMMENTED_SERVER_VAR=value # note from dev view')
++ ->call('submit')
++ ->assertHasNoErrors();
++
++ $var = $this->server->environment_variables()->where('key', 'COMMENTED_SERVER_VAR')->first();
++
++ expect($var)->not->toBeNull()
++ ->and($var->value)->toBe('value')
++ ->and($var->comment)->toBe('note from dev view');
++});
++
++test('server shared variable dev view updates existing variable', function () {
++ $this->server = Server::factory()->create(['team_id' => $this->team->id]);
++
++ SharedEnvironmentVariable::create([
++ 'key' => 'EXISTING_SERVER_VAR',
++ 'value' => 'old_value',
++ 'comment' => 'old comment',
++ 'type' => 'server',
++ 'server_id' => $this->server->id,
++ 'team_id' => $this->team->id,
++ ]);
++
++ Livewire::test(App\Livewire\SharedVariables\Server\Show::class, [
++ 'server_uuid' => $this->server->uuid,
++ ])
++ ->set('variables', 'EXISTING_SERVER_VAR=new_value # updated comment')
++ ->call('submit')
++ ->assertHasNoErrors();
++
++ $var = $this->server->environment_variables()->where('key', 'EXISTING_SERVER_VAR')->first();
++
++ expect($var->value)->toBe('new_value')
++ ->and($var->comment)->toBe('updated comment');
++});