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.

feat: sort envs alphabetically and creation date

Andras Bacsai431cc796d873
diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php
index 9270b648aa..d9ccfd49af 100644
--- a/app/Jobs/ApplicationDeploymentJob.php
+++ b/app/Jobs/ApplicationDeploymentJob.php
@@ -713,30 +713,17 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted
private function save_environment_variables()
{
$envs = collect([]);
+ $sort = $this->application->settings->is_env_sorting_enabled;
+ if ($sort) {
+ $sorted_environment_variables = $this->application->environment_variables->sortBy('key');
+ $sorted_environment_variables_preview = $this->application->environment_variables_preview->sortBy('key');
+ } else {
+ $sorted_environment_variables = $this->application->environment_variables->sortBy('id');
+ $sorted_environment_variables_preview = $this->application->environment_variables_preview->sortBy('id');
+ }
$ports = $this->application->main_port();
if ($this->pull_request_id !== 0) {
$this->env_filename = ".env-pr-$this->pull_request_id";
- foreach ($this->application->environment_variables_preview as $env) {
- $real_value = $env->real_value;
- if ($env->version === '4.0.0-beta.239') {
- $real_value = $env->real_value;
- } else {
- if ($env->is_literal) {
- $real_value = '\'' . $real_value . '\'';
- } else {
- $real_value = escapeEnvVariables($env->real_value);
- }
- }
- $envs->push($env->key . '=' . $real_value);
- }
- // Add PORT if not exists, use the first port as default
- if ($this->application->environment_variables_preview->where('key', 'PORT')->isEmpty()) {
- $envs->push("PORT={$ports[0]}");
- }
- // Add HOST if not exists
- if ($this->application->environment_variables_preview->where('key', 'HOST')->isEmpty()) {
- $envs->push("HOST=0.0.0.0");
- }
// Add SOURCE_COMMIT if not exists
if ($this->application->environment_variables_preview->where('key', 'SOURCE_COMMIT')->isEmpty()) {
if (!is_null($this->commit)) {
@@ -755,12 +742,7 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted
if ($this->application->environment_variables_preview->where('key', 'COOLIFY_BRANCH')->isEmpty()) {
$envs->push("COOLIFY_BRANCH={$this->application->git_branch}");
}
- $envs = $envs->sort(function ($a, $b) {
- return strpos($a, '$') === false ? -1 : 1;
- });
- } else {
- $this->env_filename = ".env";
- foreach ($this->application->environment_variables as $env) {
+ foreach ($sorted_environment_variables_preview as $env) {
$real_value = $env->real_value;
if ($env->version === '4.0.0-beta.239') {
$real_value = $env->real_value;
@@ -774,13 +756,15 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted
$envs->push($env->key . '=' . $real_value);
}
// Add PORT if not exists, use the first port as default
- if ($this->application->environment_variables->where('key', 'PORT')->isEmpty()) {
+ if ($this->application->environment_variables_preview->where('key', 'PORT')->isEmpty()) {
$envs->push("PORT={$ports[0]}");
}
// Add HOST if not exists
- if ($this->application->environment_variables->where('key', 'HOST')->isEmpty()) {
+ if ($this->application->environment_variables_preview->where('key', 'HOST')->isEmpty()) {
$envs->push("HOST=0.0.0.0");
}
+ } else {
+ $this->env_filename = ".env";
// Add SOURCE_COMMIT if not exists
if ($this->application->environment_variables->where('key', 'SOURCE_COMMIT')->isEmpty()) {
if (!is_null($this->commit)) {
@@ -799,9 +783,27 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted
if ($this->application->environment_variables_preview->where('key', 'COOLIFY_BRANCH')->isEmpty()) {
$envs->push("COOLIFY_BRANCH={$this->application->git_branch}");
}
- $envs = $envs->sort(function ($a, $b) {
- return strpos($a, '$') === false ? -1 : 1;
- });
+ foreach ($sorted_environment_variables as $env) {
+ $real_value = $env->real_value;
+ if ($env->version === '4.0.0-beta.239') {
+ $real_value = $env->real_value;
+ } else {
+ if ($env->is_literal) {
+ $real_value = '\'' . $real_value . '\'';
+ } else {
+ $real_value = escapeEnvVariables($env->real_value);
+ }
+ }
+ $envs->push($env->key . '=' . $real_value);
+ }
+ // Add PORT if not exists, use the first port as default
+ if ($this->application->environment_variables->where('key', 'PORT')->isEmpty()) {
+ $envs->push("PORT={$ports[0]}");
+ }
+ // Add HOST if not exists
+ if ($this->application->environment_variables->where('key', 'HOST')->isEmpty()) {
+ $envs->push("HOST=0.0.0.0");
+ }
}
if ($envs->isEmpty()) {
@@ -1272,6 +1274,7 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted
private function generate_env_variables()
{
$this->env_args = collect([]);
+ $this->env_args->put('SOURCE_COMMIT', $this->commit);
if ($this->pull_request_id === 0) {
foreach ($this->application->build_environment_variables as $env) {
if (!is_null($env->real_value)) {
@@ -1285,7 +1288,6 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted
}
}
}
- $this->env_args->put('SOURCE_COMMIT', $this->commit);
}
private function generate_compose_file()
diff --git a/app/Livewire/Project/Shared/EnvironmentVariable/All.php b/app/Livewire/Project/Shared/EnvironmentVariable/All.php
index 6a6d941427..9a61f639a5 100644
--- a/app/Livewire/Project/Shared/EnvironmentVariable/All.php
+++ b/app/Livewire/Project/Shared/EnvironmentVariable/All.php
@@ -5,11 +5,11 @@ namespace App\Livewire\Project\Shared\EnvironmentVariable;
use App\Models\EnvironmentVariable;
use Livewire\Component;
use Visus\Cuid2\Cuid2;
-use Illuminate\Support\Str;
class All extends Component
{
public $resource;
+ public string $resourceClass;
public bool $showPreview = false;
public ?string $modalId = null;
public ?string $variables = null;
@@ -19,17 +19,42 @@ class All extends Component
'refreshEnvs',
'saveKey' => 'submit',
];
+ protected $rules = [
+ 'resource.settings.is_env_sorting_enabled' => 'required|boolean',
+ ];
+
public function mount()
{
- $resourceClass = get_class($this->resource);
+ $this->resourceClass = get_class($this->resource);
$resourceWithPreviews = ['App\Models\Application'];
$simpleDockerfile = !is_null(data_get($this->resource, 'dockerfile'));
- if (Str::of($resourceClass)->contains($resourceWithPreviews) && !$simpleDockerfile) {
+ if (str($this->resourceClass)->contains($resourceWithPreviews) && !$simpleDockerfile) {
$this->showPreview = true;
}
$this->modalId = new Cuid2(7);
+ $this->sortMe();
$this->getDevView();
}
+
+ public function sortMe()
+ {
+ if ($this->resourceClass === 'App\Models\Application' && $this->resource->settings->is_env_sorting_enabled) {
+ $this->resource->environment_variables = $this->resource->environment_variables->sortBy('key');
+ $this->resource->environment_variables_preview = $this->resource->environment_variables_preview->sortBy('key');
+ } else {
+ $this->resource->environment_variables = $this->resource->environment_variables->sortBy('id');
+ $this->resource->environment_variables_preview = $this->resource->environment_variables_preview->sortBy('id');
+ }
+ $this->getDevView();
+ }
+ public function instantSave()
+ {
+ if ($this->resourceClass === 'App\Models\Application') {
+ $this->resource->settings->save();
+ $this->dispatch('success', 'Environment variable settings updated.');
+ $this->sortMe();
+ }
+ }
public function getDevView()
{
$this->variables = $this->resource->environment_variables->map(function ($item) {
@@ -40,7 +65,7 @@ class All extends Component
return "$item->key=(multiline, edit in normal view)";
}
return "$item->key=$item->value";
- })->sort()->join('
+ })->join('
');
if ($this->showPreview) {
$this->variablesPreview = $this->resource->environment_variables_preview->map(function ($item) {
@@ -51,13 +76,18 @@ class All extends Component
return "$item->key=(multiline, edit in normal view)";
}
return "$item->key=$item->value";
- })->sort()->join('
+ })->join('
');
}
}
public function switch()
{
- $this->view = $this->view === 'normal' ? 'dev' : 'normal';
+ if ($this->view === 'normal') {
+ $this->view = 'dev';
+ } else {
+ $this->view = 'normal';
+ }
+ $this->sortMe();
}
public function saveVariables($isPreview)
{
@@ -66,6 +96,7 @@ class All extends Component
$this->resource->environment_variables_preview()->whereNotIn('key', array_keys($variables))->delete();
} else {
$variables = parseEnvFormatToArray($this->variables);
+ ray($variables, $this->variables);
$this->resource->environment_variables()->whereNotIn('key', array_keys($variables))->delete();
}
foreach ($variables as $key => $variable) {
diff --git a/database/migrations/2024_05_17_082012_add_env_sorting_toggle.php b/database/migrations/2024_05_17_082012_add_env_sorting_toggle.php
new file mode 100644
index 0000000000..d4e120e2b4
--- /dev/null
+++ b/database/migrations/2024_05_17_082012_add_env_sorting_toggle.php
@@ -0,0 +1,28 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+ /**
+ * Run the migrations.
+ */
+ public function up(): void
+ {
+ Schema::table('application_settings', function (Blueprint $table) {
+ $table->boolean('is_env_sorting_enabled')->default(true);
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ */
+ public function down(): void
+ {
+ Schema::table('application_settings', function (Blueprint $table) {
+ $table->dropColumn('is_env_sorting_enabled');
+ });
+ }
+};
diff --git a/resources/views/livewire/project/shared/environment-variable/all.blade.php b/resources/views/livewire/project/shared/environment-variable/all.blade.php
index 06df0871c7..41a8a6d599 100644
--- a/resources/views/livewire/project/shared/environment-variable/all.blade.php
+++ b/resources/views/livewire/project/shared/environment-variable/all.blade.php
@@ -11,12 +11,18 @@
wire:click='switch'>{{ $view === 'normal' ? 'Developer view' : 'Normal view' }}</x-forms.button>
</div>
<div>Environment variables (secrets) for this resource.</div>
+ @if ($this->resourceClass === 'App\Models\Application')
+ <div class="w-64 pt-2">
+ <x-forms.checkbox id="resource.settings.is_env_sorting_enabled" label="Sort alphabetically"
+ helper="Turn this off if one environment is dependent on an other. It will be sorted by creation order." instantSave></x-forms.checkbox>
+ </div>
+ @endif
@if ($resource->type() === 'service' || $resource?->build_pack === 'dockercompose')
<div class="pt-4 dark:text-warning text-coollabs">Hardcoded variables are not shown here.</div>
@endif
</div>
@if ($view === 'normal')
- @forelse ($resource->environment_variables->sort()->sortBy('key') as $env)
+ @forelse ($resource->environment_variables as $env)
<livewire:project.shared.environment-variable.show wire:key="environment-{{ $env->id }}"
:env="$env" :type="$resource->type()" />
@empty
@@ -27,7 +33,7 @@
<h3>Preview Deployments</h3>
<div>Environment (secrets) variables for Preview Deployments.</div>
</div>
- @foreach ($resource->environment_variables_preview->sort()->sortBy('key') as $env)
+ @foreach ($resource->environment_variables_preview as $env)
<livewire:project.shared.environment-variable.show wire:key="environment-{{ $env->id }}"
:env="$env" :type="$resource->type()" />
@endforeach