From 5b36f07493547ecdb85d95e6cf8838e43fc4f407 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Mon, 15 Apr 2024 12:46:22 +0200 Subject: [PATCH] feat: literal env variables --- app/Jobs/ApplicationDeploymentJob.php | 13 ++++++- app/Livewire/Project/Edit.php | 1 + app/Livewire/Project/EnvironmentEdit.php | 1 + .../Shared/EnvironmentVariable/Add.php | 6 ++++ .../Shared/EnvironmentVariable/Show.php | 3 ++ app/Livewire/TeamSharedVariablesIndex.php | 1 + app/Models/SharedEnvironmentVariable.php | 1 + bootstrap/helpers/docker.php | 6 ++++ ...024_04_15_094703_add_literal_variables.php | 34 +++++++++++++++++++ .../views/livewire/project/edit.blade.php | 2 +- .../project/environment-edit.blade.php | 2 +- .../shared/environment-variable/add.blade.php | 5 +++ .../environment-variable/show.blade.php | 14 ++++++-- .../team-shared-variables-index.blade.php | 2 +- 14 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 database/migrations/2024_04_15_094703_add_literal_variables.php diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index d4650002f..df034a2f7 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -1129,7 +1129,6 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted $persistent_storages = $this->generate_local_persistent_volumes(); $volume_names = $this->generate_local_persistent_volumes_only_volume_names(); $environment_variables = $this->generate_environment_variables($ports); - if (data_get($this->application, 'custom_labels')) { $this->application->parseContainerLabels(); $labels = collect(preg_split("/\r\n|\n|\r/", base64_decode($this->application->custom_labels))); @@ -1419,6 +1418,9 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted } else { $real_value = escapeEnvVariables($env->real_value); } + if ($env->is_literal) { + $real_value = escapeDollarSign($real_value); + } $environment_variables->push("$env->key=$real_value"); } foreach ($this->application->nixpacks_environment_variables as $env) { @@ -1427,6 +1429,9 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted } else { $real_value = escapeEnvVariables($env->real_value); } + if ($env->is_literal) { + $real_value = escapeDollarSign($real_value); + } $environment_variables->push("$env->key=$real_value"); } } else { @@ -1436,6 +1441,9 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted } else { $real_value = escapeEnvVariables($env->real_value); } + if ($env->is_literal) { + $real_value = escapeDollarSign($real_value); + } $environment_variables->push("$env->key=$real_value"); } foreach ($this->application->nixpacks_environment_variables_preview as $env) { @@ -1444,6 +1452,9 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted } else { $real_value = escapeEnvVariables($env->real_value); } + if ($env->is_literal) { + $real_value = escapeDollarSign($real_value); + } $environment_variables->push("$env->key=$real_value"); } } diff --git a/app/Livewire/Project/Edit.php b/app/Livewire/Project/Edit.php index 17cb6902b..d222917a6 100644 --- a/app/Livewire/Project/Edit.php +++ b/app/Livewire/Project/Edit.php @@ -25,6 +25,7 @@ class Edit extends Component 'key' => $data['key'], 'value' => $data['value'], 'is_multiline' => $data['is_multiline'], + 'is_literal' => $data['is_literal'], 'type' => 'project', 'team_id' => currentTeam()->id, ]); diff --git a/app/Livewire/Project/EnvironmentEdit.php b/app/Livewire/Project/EnvironmentEdit.php index c5e4ccf11..173d946f3 100644 --- a/app/Livewire/Project/EnvironmentEdit.php +++ b/app/Livewire/Project/EnvironmentEdit.php @@ -29,6 +29,7 @@ class EnvironmentEdit extends Component 'key' => $data['key'], 'value' => $data['value'], 'is_multiline' => $data['is_multiline'], + 'is_literal' => $data['is_literal'], 'type' => 'environment', 'team_id' => currentTeam()->id, ]); diff --git a/app/Livewire/Project/Shared/EnvironmentVariable/Add.php b/app/Livewire/Project/Shared/EnvironmentVariable/Add.php index c04242963..df808ba52 100644 --- a/app/Livewire/Project/Shared/EnvironmentVariable/Add.php +++ b/app/Livewire/Project/Shared/EnvironmentVariable/Add.php @@ -7,11 +7,13 @@ use Livewire\Component; class Add extends Component { public $parameters; + public bool $shared = false; public bool $is_preview = false; public string $key; public ?string $value = null; public bool $is_build_time = false; public bool $is_multiline = false; + public bool $is_literal = false; protected $listeners = ['clearAddEnv' => 'clear']; protected $rules = [ @@ -19,12 +21,14 @@ class Add extends Component 'value' => 'nullable', 'is_build_time' => 'required|boolean', 'is_multiline' => 'required|boolean', + 'is_literal' => 'required|boolean', ]; protected $validationAttributes = [ 'key' => 'key', 'value' => 'value', 'is_build_time' => 'build', 'is_multiline' => 'multiline', + 'is_literal' => 'literal', ]; public function mount() @@ -47,6 +51,7 @@ class Add extends Component 'value' => $this->value, 'is_build_time' => $this->is_build_time, 'is_multiline' => $this->is_multiline, + 'is_literal' => $this->is_literal, 'is_preview' => $this->is_preview, ]); $this->clear(); @@ -58,5 +63,6 @@ class Add extends Component $this->value = ''; $this->is_build_time = false; $this->is_multiline = false; + $this->is_literal = false; } } diff --git a/app/Livewire/Project/Shared/EnvironmentVariable/Show.php b/app/Livewire/Project/Shared/EnvironmentVariable/Show.php index 7903e8e51..a9854c196 100644 --- a/app/Livewire/Project/Shared/EnvironmentVariable/Show.php +++ b/app/Livewire/Project/Shared/EnvironmentVariable/Show.php @@ -22,6 +22,7 @@ class Show extends Component 'env.value' => 'nullable', 'env.is_build_time' => 'required|boolean', 'env.is_multiline' => 'required|boolean', + 'env.is_literal' => 'required|boolean', 'env.is_shown_once' => 'required|boolean', 'env.real_value' => 'nullable', ]; @@ -30,6 +31,7 @@ class Show extends Component 'env.value' => 'Value', 'env.is_build_time' => 'Build Time', 'env.is_multiline' => 'Multiline', + 'env.is_literal' => 'Literal', 'env.is_shown_once' => 'Shown Once', ]; @@ -41,6 +43,7 @@ class Show extends Component $this->modalId = new Cuid2(7); $this->parameters = get_route_parameters(); $this->checkEnvs(); + ray($this->env); } public function checkEnvs() { diff --git a/app/Livewire/TeamSharedVariablesIndex.php b/app/Livewire/TeamSharedVariablesIndex.php index dbb64ab65..eeee30ab9 100644 --- a/app/Livewire/TeamSharedVariablesIndex.php +++ b/app/Livewire/TeamSharedVariablesIndex.php @@ -21,6 +21,7 @@ class TeamSharedVariablesIndex extends Component 'key' => $data['key'], 'value' => $data['value'], 'is_multiline' => $data['is_multiline'], + 'is_literal' => $data['is_literal'], 'type' => 'team', 'team_id' => currentTeam()->id, ]); diff --git a/app/Models/SharedEnvironmentVariable.php b/app/Models/SharedEnvironmentVariable.php index 260f16afb..5fad8fd96 100644 --- a/app/Models/SharedEnvironmentVariable.php +++ b/app/Models/SharedEnvironmentVariable.php @@ -2,6 +2,7 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Model; class SharedEnvironmentVariable extends Model diff --git a/bootstrap/helpers/docker.php b/bootstrap/helpers/docker.php index afcc4622b..14ea491ee 100644 --- a/bootstrap/helpers/docker.php +++ b/bootstrap/helpers/docker.php @@ -586,3 +586,9 @@ function escapeEnvVariables($value) $replace = array("\\\\", "\\r", "\\t", "\\0", '\"', "\'"); return str_replace($search, $replace, $value); } +function escapeDollarSign($value) +{ + $search = array('$'); + $replace = array('$$'); + return str_replace($search, $replace, $value); +} diff --git a/database/migrations/2024_04_15_094703_add_literal_variables.php b/database/migrations/2024_04_15_094703_add_literal_variables.php new file mode 100644 index 000000000..c03194975 --- /dev/null +++ b/database/migrations/2024_04_15_094703_add_literal_variables.php @@ -0,0 +1,34 @@ +boolean('is_literal')->default(false); + }); + Schema::table('shared_environment_variables', function (Blueprint $table) { + $table->boolean('is_literal')->default(false); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('environment_variables', function (Blueprint $table) { + $table->dropColumn('is_literal'); + }); + Schema::table('shared_environment_variables', function (Blueprint $table) { + $table->dropColumn('is_literal'); + }); + } +}; diff --git a/resources/views/livewire/project/edit.blade.php b/resources/views/livewire/project/edit.blade.php index c8fd27f9c..9fd47a70a 100644 --- a/resources/views/livewire/project/edit.blade.php +++ b/resources/views/livewire/project/edit.blade.php @@ -17,7 +17,7 @@

Shared Variables

- +
diff --git a/resources/views/livewire/project/environment-edit.blade.php b/resources/views/livewire/project/environment-edit.blade.php index 02fbb1c69..23bb8748b 100644 --- a/resources/views/livewire/project/environment-edit.blade.php +++ b/resources/views/livewire/project/environment-edit.blade.php @@ -45,7 +45,7 @@

Shared Variables

- +
You can use these variables anywhere with @{{environment.VARIABLENAME}} @endif + @if (!$shared) + + @endif Save diff --git a/resources/views/livewire/project/shared/environment-variable/show.blade.php b/resources/views/livewire/project/shared/environment-variable/show.blade.php index 68e728f7e..fca2d2bf4 100644 --- a/resources/views/livewire/project/shared/environment-variable/show.blade.php +++ b/resources/views/livewire/project/shared/environment-variable/show.blade.php @@ -45,9 +45,19 @@ @else @if ($env->is_shared) + @else - - + @if ($type === 'team' || $type === 'environment' || $type === 'project') + + @else + + + + @endif @endif @endif
diff --git a/resources/views/livewire/team-shared-variables-index.blade.php b/resources/views/livewire/team-shared-variables-index.blade.php index 9f8a709f0..4d36dc950 100644 --- a/resources/views/livewire/team-shared-variables-index.blade.php +++ b/resources/views/livewire/team-shared-variables-index.blade.php @@ -3,7 +3,7 @@

Shared Variables

- +
You can use these variables anywhere with