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.
PHP coolifydatabasesdeploymentdockerdocker-composeinertiajslaravelmariadbmysqlnextjsnodejsphppostgresredisself-hostedself-hostingserverstatic-sitesveltesvelte5
Fix docker compose PR location default value
Andras Bacsai3f866a07d8d0
diff --git a/app/Http/Livewire/Project/Application/General.php b/app/Http/Livewire/Project/Application/General.phpindex da642ba771..2917fa4380 100644--- a/app/Http/Livewire/Project/Application/General.php+++ b/app/Http/Livewire/Project/Application/General.php@@ -60,7 +60,9 @@ class General extends Component'application.docker_compose_location' => 'nullable','application.docker_compose_pr_location' => 'nullable','application.docker_compose' => 'nullable',+ 'application.docker_compose_pr' => 'nullable','application.docker_compose_raw' => 'nullable',+ 'application.docker_compose_pr_raw' => 'nullable','application.custom_labels' => 'nullable','application.dockerfile_target_build' => 'nullable','application.settings.is_static' => 'boolean|required',@@ -88,7 +90,9 @@ class General extends Component'application.docker_compose_location' => 'Docker compose location','application.docker_compose_pr_location' => 'Docker compose location','application.docker_compose' => 'Docker compose',+ 'application.docker_compose_pr' => 'Docker compose','application.docker_compose_raw' => 'Docker compose raw',+ 'application.docker_compose_pr_raw' => 'Docker compose raw','application.custom_labels' => 'Custom labels','application.dockerfile_target_build' => 'Dockerfile target build','application.settings.is_static' => 'Is static',@@ -98,7 +102,6 @@ class General extends Component{try {$this->parsedServices = $this->application->parseCompose();- ray($this->parsedServices);} catch (\Throwable $e) {$this->emit('error', $e->getMessage());}@@ -196,7 +199,7 @@ class General extends Componentpublic function submit($showToaster = true){try {- if ($this->initialDockerComposeLocation !== $this->application->docker_compose_location) {+ if ($this->initialDockerComposeLocation !== $this->application->docker_compose_location || $this->initialDockerComposePrLocation !== $this->application->docker_compose_pr_location) {$this->loadComposeFile();}$this->validate();diff --git a/app/Models/Application.php b/app/Models/Application.phpindex a7c721800e..ea7e0a930d 100644--- a/app/Models/Application.php+++ b/app/Models/Application.php@@ -153,7 +153,7 @@ class Application extends BaseModelreturn Attribute::make(set: function ($value) {if (is_null($value) || $value === '') {- return '/docker-compose-pr.yaml';+ return '/docker-compose.yaml';} else {if ($value !== '/') {return Str::start(Str::replaceEnd('/', '', $value), '/');@@ -602,7 +602,11 @@ class Application extends BaseModelfunction parseCompose(int $pull_request_id = 0){if ($this->docker_compose_raw) {- return parseDockerComposeFile(resource: $this, isNew: false, pull_request_id: $pull_request_id);+ $mainCompose = parseDockerComposeFile(resource: $this, isNew: false, pull_request_id: $pull_request_id);+ if ($this->getMorphClass() === 'App\Models\Application' && $this->docker_compose_pr_raw) {+ parseDockerComposeFile(resource: $this, isNew: false, pull_request_id: $pull_request_id, is_pr: true);+ }+ return $mainCompose;} else {return collect([]);}@@ -620,11 +624,15 @@ class Application extends BaseModel$workdir = rtrim($this->base_directory, '/');$composeFile = $this->docker_compose_location;$prComposeFile = $this->docker_compose_pr_location;+ $fileList = collect([".$composeFile"]);+ if ($composeFile !== $prComposeFile) {+ $fileList->push(".$prComposeFile");+ }$commands = collect(["mkdir -p /tmp/{$uuid} && cd /tmp/{$uuid}",$cloneCommand,"git sparse-checkout init --cone",- "git sparse-checkout set .$workdir$composeFile .$workdir$prComposeFile",+ "git sparse-checkout set {$fileList->implode(' ')}","git read-tree -mu HEAD","cat .$workdir$composeFile",]);@@ -632,23 +640,30 @@ class Application extends BaseModelif (!$composeFileContent) {$this->docker_compose_location = $initialDockerComposeLocation;$this->save();- throw new \Exception("Could not load compose file from $workdir$composeFile");+ throw new \Exception("Could not load base compose file from $workdir$composeFile");} else {$this->docker_compose_raw = $composeFileContent;$this->save();}- $commands = collect([- "cat .$workdir$prComposeFile",- ]);- $composePrFileContent = instant_remote_process($commands, $this->destination->server, false);- if (!$composePrFileContent) {- $this->docker_compose_pr_location = $initialDockerComposePrLocation;+ if ($composeFile === $prComposeFile) {+ $this->docker_compose_pr_raw = $composeFileContent;$this->save();- throw new \Exception("Could not load compose file from $workdir$prComposeFile");} else {- $this->docker_compose_pr_raw = $composePrFileContent;- $this->save();+ $commands = collect([+ "cd /tmp/{$uuid}",+ "cat .$workdir$prComposeFile",+ ]);+ $composePrFileContent = instant_remote_process($commands, $this->destination->server, false);+ if (!$composePrFileContent) {+ $this->docker_compose_pr_location = $initialDockerComposePrLocation;+ $this->save();+ throw new \Exception("Could not load compose file from $workdir$prComposeFile");+ } else {+ $this->docker_compose_pr_raw = $composePrFileContent;+ $this->save();+ }}+$commands = collect(["rm -rf /tmp/{$uuid}",]);diff --git a/bootstrap/helpers/shared.php b/bootstrap/helpers/shared.phpindex fcf32d215d..2b2f32d33a 100644--- a/bootstrap/helpers/shared.php+++ b/bootstrap/helpers/shared.php@@ -579,7 +579,7 @@ function getTopLevelNetworks(Service|Application $resource)return $topLevelNetworks->keys();}}-function parseDockerComposeFile(Service|Application $resource, bool $isNew = false, int $pull_request_id)+function parseDockerComposeFile(Service|Application $resource, bool $isNew = false, int $pull_request_id, bool $is_pr = false){// ray()->clearAll();if ($resource->getMorphClass() === 'App\Models\Service') {@@ -1089,8 +1089,17 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = falreturn collect([]);}} else if ($resource->getMorphClass() === 'App\Models\Application') {- if ($pull_request_id !== 0 && $resource->dockerComposePrLocation() !== $resource->dockerComposeLocation()) {-+ $isSameDockerComposeFile = false;+ if ($resource->dockerComposePrLocation() === $resource->dockerComposeLocation()) {+ $isSameDockerComposeFile = true;+ $is_pr = false;+ }+ if ($is_pr) {+ try {+ $yaml = Yaml::parse($resource->docker_compose_pr_raw);+ } catch (\Exception $e) {+ throw new \Exception($e->getMessage());+ }} else {try {$yaml = Yaml::parse($resource->docker_compose_raw);@@ -1098,7 +1107,7 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = falthrow new \Exception($e->getMessage());}}-+ ray($yaml);$server = $resource->destination->server;$topLevelVolumes = collect(data_get($yaml, 'volumes', []));if ($pull_request_id !== 0) {@@ -1172,7 +1181,6 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = faldata_set($service, 'volumes', $serviceVolumes->toArray());}} else {-}// Decide if the service is a database$isDatabase = isDatabaseImage(data_get_str($service, 'image'));@@ -1469,8 +1477,20 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal'volumes' => $topLevelVolumes->toArray(),'networks' => $topLevelNetworks->toArray(),];- $resource->docker_compose_raw = Yaml::dump($yaml, 10, 2);- $resource->docker_compose = Yaml::dump($finalServices, 10, 2);+ if ($isSameDockerComposeFile) {+ $resource->docker_compose_pr_raw = Yaml::dump($yaml, 10, 2);+ $resource->docker_compose_pr = Yaml::dump($finalServices, 10, 2);+ $resource->docker_compose_raw = Yaml::dump($yaml, 10, 2);+ $resource->docker_compose = Yaml::dump($finalServices, 10, 2);+ } else {+ if ($is_pr) {+ $resource->docker_compose_pr_raw = Yaml::dump($yaml, 10, 2);+ $resource->docker_compose_pr = Yaml::dump($finalServices, 10, 2);+ } else {+ $resource->docker_compose_raw = Yaml::dump($yaml, 10, 2);+ $resource->docker_compose = Yaml::dump($finalServices, 10, 2);+ }+ }$resource->save();return collect($finalServices);}diff --git a/database/migrations/2023_11_24_080341_add_docker_compose_location.php b/database/migrations/2023_11_24_080341_add_docker_compose_location.phpindex eab705ff09..8508181a39 100644--- a/database/migrations/2023_11_24_080341_add_docker_compose_location.php+++ b/database/migrations/2023_11_24_080341_add_docker_compose_location.php@@ -13,7 +13,7 @@ return new class extends Migration{Schema::table('applications', function (Blueprint $table) {$table->string('docker_compose_location')->nullable()->default('/docker-compose.yaml')->after('dockerfile_location');- $table->string('docker_compose_pr_location')->nullable()->default('/docker-compose-pr.yaml')->after('docker_compose_location');+ $table->string('docker_compose_pr_location')->nullable()->default('/docker-compose.yaml')->after('docker_compose_location');$table->longText('docker_compose')->nullable()->after('docker_compose_location');$table->longText('docker_compose_pr')->nullable()->after('docker_compose_location');diff --git a/resources/views/livewire/project/application/general.blade.php b/resources/views/livewire/project/application/general.blade.phpindex b08a9fb600..daeac97105 100644--- a/resources/views/livewire/project/application/general.blade.php+++ b/resources/views/livewire/project/application/general.blade.php@@ -119,9 +119,9 @@<x-forms.input placeholder="/docker-compose.yaml" id="application.docker_compose_location"label="Docker Compose Location"helper="It is calculated together with the Base Directory:<br><span class='text-warning'>{{ Str::start($application->base_directory . $application->docker_compose_location, '/') }}</span>" />- <x-forms.input placeholder="/docker-compose.yaml" id="application.docker_compose_pr_location"+ {{-- <x-forms.input placeholder="/docker-compose.yaml" id="application.docker_compose_pr_location"label="Docker Compose Location For Pull Requests"- helper="It is calculated together with the Base Directory:<br><span class='text-warning'>{{ Str::start($application->base_directory . $application->docker_compose_pr_location, '/') }}</span>" />+ helper="It is calculated together with the Base Directory:<br><span class='text-warning'>{{ Str::start($application->base_directory . $application->docker_compose_pr_location, '/') }}</span>" /> --}}@endif@if ($application->build_pack === 'dockerfile')<x-forms.input id="application.dockerfile_target_build" label="Docker Build Stage Target"@@ -140,9 +140,10 @@@endif@if ($application->build_pack === 'dockercompose')<x-forms.button wire:click="loadComposeFile">Reload Compose File</x-forms.button>-<x-forms.textarea rows="10" readonly id="application.docker_compose" label="Docker Compose Content"helper="You need to modify the docker compose file." />+ {{-- <x-forms.textarea rows="10" readonly id="application.docker_compose_pr"+ label="Docker PR Compose Content" helper="You need to modify the docker compose file." /> --}}@endif@if ($application->dockerfile)