From 4520070df3f3fb78e51805debdf8461a107af0ea Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Wed, 1 Nov 2023 15:39:47 +0100 Subject: [PATCH] fix: pull requests deployments feat: filter deployments logs by pull requests --- .../Controllers/ApplicationController.php | 2 +- .../Project/Application/Deployments.php | 16 +- app/Jobs/ApplicationDeploymentJob.php | 21 ++- app/Jobs/ContainerStatusJob.php | 11 +- .../application/deployment-logs.blade.php | 6 +- .../project/application/deployments.blade.php | 144 ++++++++++-------- .../project/application/previews.blade.php | 6 + .../project/shared/get-logs.blade.php | 6 +- 8 files changed, 124 insertions(+), 88 deletions(-) diff --git a/app/Http/Controllers/ApplicationController.php b/app/Http/Controllers/ApplicationController.php index aa2787de7..6f58c71e6 100644 --- a/app/Http/Controllers/ApplicationController.php +++ b/app/Http/Controllers/ApplicationController.php @@ -41,7 +41,7 @@ public function deployments() if (!$application) { return redirect()->route('dashboard'); } - ['deployments' => $deployments, 'count' => $count] = $application->deployments(0, 8); + ['deployments' => $deployments, 'count' => $count] = $application->deployments(0, 40); return view('project.application.deployments', ['application' => $application, 'deployments' => $deployments, 'deployments_count' => $count]); } diff --git a/app/Http/Livewire/Project/Application/Deployments.php b/app/Http/Livewire/Project/Application/Deployments.php index 1dd80d710..4f4d5ef67 100644 --- a/app/Http/Livewire/Project/Application/Deployments.php +++ b/app/Http/Livewire/Project/Application/Deployments.php @@ -3,24 +3,31 @@ namespace App\Http\Livewire\Project\Application; use App\Models\Application; +use Illuminate\Support\Collection; use Livewire\Component; class Deployments extends Component { public Application $application; - public $deployments = []; + public Array|Collection $deployments = []; public int $deployments_count = 0; public string $current_url; public int $skip = 0; - public int $default_take = 8; + public int $default_take = 40; public bool $show_next = false; - + public ?string $pull_request_id = null; + protected $queryString = ['pull_request_id']; public function mount() { $this->current_url = url()->current(); + $this->show_pull_request_only(); $this->show_more(); } - + private function show_pull_request_only() { + if ($this->pull_request_id) { + $this->deployments = $this->deployments->where('pull_request_id', $this->pull_request_id); + } + } private function show_more() { if (count($this->deployments) !== 0) { @@ -47,6 +54,7 @@ public function load_deployments(int|null $take = null) ['deployments' => $deployments, 'count' => $count] = $this->application->deployments($this->skip, $take); $this->deployments = $deployments; $this->deployments_count = $count; + $this->show_pull_request_only(); $this->show_more(); } } diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index c056fb650..fcd07d49f 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -487,9 +487,7 @@ private function health_check() } private function deploy_pull_request() { - $this->build_image_name = Str::lower("{$this->application->uuid}:pr-{$this->pull_request_id}-build"); - $this->production_image_name = Str::lower("{$this->application->uuid}:pr-{$this->pull_request_id}"); - // ray('Build Image Name: ' . $this->build_image_name . ' & Production Image Name: ' . $this->production_image_name)->green(); + $this->generate_image_names(); $this->execute_remote_command([ "echo 'Starting pull request (#{$this->pull_request_id}) deployment of {$this->application->git_repository}:{$this->application->git_branch}.'", ]); @@ -505,7 +503,12 @@ private function deploy_pull_request() // $this->generate_build_env_variables(); // $this->add_build_env_variables_to_dockerfile(); $this->build_image(); - $this->stop_running_container(); + if ($this->currently_running_container_name) { + $this->execute_remote_command( + ["echo -n 'Removing old version of your application.'"], + [executeInDocker($this->deployment_uuid, "docker rm -f $this->currently_running_container_name >/dev/null 2>&1"), "hidden" => true, "ignore_errors" => true], + ); + } $this->execute_remote_command( ["echo -n 'Starting preview deployment.'"], [executeInDocker($this->deployment_uuid, "docker compose --project-directory {$this->workdir} up -d"), "hidden" => true], @@ -743,6 +746,16 @@ private function generate_compose_file() } else { $labels = collect(generateLabelsApplication($this->application, $this->preview)); } + if ($this->pull_request_id !== 0) { + $labels = $labels->reject(function ($label) { + return str($label)->contains('Host'); + }); + $newLabels = collect(generateLabelsApplication($this->application, $this->preview)); + $hostLabels = $newLabels->filter(function ($label) { + return str($label)->contains('Host'); + }); + $labels = $labels->merge($hostLabels); + } $labels = $labels->merge(defaultLabels($this->application->id, $this->application->uuid, $this->pull_request_id))->toArray(); $docker_compose = [ 'version' => '3.8', diff --git a/app/Jobs/ContainerStatusJob.php b/app/Jobs/ContainerStatusJob.php index ad1593fac..99a1c561b 100644 --- a/app/Jobs/ContainerStatusJob.php +++ b/app/Jobs/ContainerStatusJob.php @@ -138,11 +138,10 @@ public function handle(): void $containerStatus = "$containerStatus ($containerHealth)"; $labels = data_get($container, 'Config.Labels'); $labels = Arr::undot(format_docker_labels_to_json($labels)); - $labelId = data_get($labels, 'coolify.applicationId'); - if ($labelId) { - if (str_contains($labelId, '-pr-')) { - $pullRequestId = data_get($labels, 'coolify.pullRequestId'); - $applicationId = (int) Str::before($labelId, '-pr-'); + $applicationId = data_get($labels, 'coolify.applicationId'); + if ($applicationId) { + $pullRequestId = data_get($labels, 'coolify.pullRequestId'); + if ($pullRequestId) { $preview = ApplicationPreview::where('application_id', $applicationId)->where('pull_request_id', $pullRequestId)->first(); if ($preview) { $foundApplicationPreviews[] = $preview->id; @@ -154,7 +153,7 @@ public function handle(): void //Notify user that this container should not be there. } } else { - $application = $applications->where('id', $labelId)->first(); + $application = $applications->where('id', $applicationId)->first(); if ($application) { $foundApplications[] = $application->id; $statusFromDb = $application->status; diff --git a/resources/views/livewire/project/application/deployment-logs.blade.php b/resources/views/livewire/project/application/deployment-logs.blade.php index a37f7e377..b47d6dc38 100644 --- a/resources/views/livewire/project/application/deployment-logs.blade.php +++ b/resources/views/livewire/project/application/deployment-logs.blade.php @@ -16,14 +16,14 @@ class="text-warning">{{ Str::headline(data_get($application_deployment_queue, 's
- -
diff --git a/resources/views/livewire/project/application/previews.blade.php b/resources/views/livewire/project/application/previews.blade.php index 5e5d4d024..19e61376b 100644 --- a/resources/views/livewire/project/application/previews.blade.php +++ b/resources/views/livewire/project/application/previews.blade.php @@ -83,6 +83,12 @@ Preview @endif + + + Get Deployment Logs + + @endforeach diff --git a/resources/views/livewire/project/shared/get-logs.blade.php b/resources/views/livewire/project/shared/get-logs.blade.php index ee1b6b07e..137bd9dbb 100644 --- a/resources/views/livewire/project/shared/get-logs.blade.php +++ b/resources/views/livewire/project/shared/get-logs.blade.php @@ -17,13 +17,13 @@
- -