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: Add comprehensive PR cleanup to GitLab, Bitbucket, and Gitea webhooks
Andras Bacsaid27070b215fa
diff --git a/app/Actions/Application/CleanupPreviewDeployment.php b/app/Actions/Application/CleanupPreviewDeployment.phpnew file mode 100644index 0000000000..83f7299597--- /dev/null+++ b/app/Actions/Application/CleanupPreviewDeployment.php@@ -0,0 +1,175 @@+<?php++namespace App\Actions\Application;++use App\Enums\ApplicationDeploymentStatus;+use App\Jobs\DeleteResourceJob;+use App\Models\Application;+use App\Models\ApplicationDeploymentQueue;+use App\Models\ApplicationPreview;+use Lorisleiva\Actions\Concerns\AsAction;++class CleanupPreviewDeployment+{+ use AsAction;++ public string $jobQueue = 'high';++ /**+ * Clean up a PR preview deployment completely.+ *+ * This handles:+ * 1. Cancelling active deployments for the PR (QUEUED/IN_PROGRESS → CANCELLED_BY_USER)+ * 2. Killing helper containers by deployment_uuid+ * 3. Stopping/removing all running PR containers+ * 4. Dispatching DeleteResourceJob for thorough cleanup (volumes, networks, database records)+ *+ * This unifies the cleanup logic from GitHub webhook handler to be used across all providers.+ */+ public function handle(+ Application $application,+ int $pull_request_id,+ ?ApplicationPreview $preview = null+ ): array {+ $result = [+ 'cancelled_deployments' => 0,+ 'killed_containers' => 0,+ 'status' => 'success',+ ];++ $server = $application->destination->server;++ if (! $server->isFunctional()) {+ return [+ ...$result,+ 'status' => 'failed',+ 'message' => 'Server is not functional',+ ];+ }++ // Step 1: Cancel all active deployments for this PR and kill helper containers+ $result['cancelled_deployments'] = $this->cancelActiveDeployments(+ $application,+ $pull_request_id,+ $server+ );++ // Step 2: Stop and remove all running PR containers+ $result['killed_containers'] = $this->stopRunningContainers(+ $application,+ $pull_request_id,+ $server+ );++ // Step 3: Find or use provided preview, then dispatch cleanup job for thorough cleanup+ if (! $preview) {+ $preview = ApplicationPreview::where('application_id', $application->id)+ ->where('pull_request_id', $pull_request_id)+ ->first();+ }++ if ($preview) {+ DeleteResourceJob::dispatch($preview);+ }++ return $result;+ }++ /**+ * Cancel all active (QUEUED/IN_PROGRESS) deployments for this PR.+ */+ private function cancelActiveDeployments(+ Application $application,+ int $pull_request_id,+ $server+ ): int {+ $activeDeployments = ApplicationDeploymentQueue::where('application_id', $application->id)+ ->where('pull_request_id', $pull_request_id)+ ->whereIn('status', [+ ApplicationDeploymentStatus::QUEUED->value,+ ApplicationDeploymentStatus::IN_PROGRESS->value,+ ])+ ->get();++ $cancelled = 0;+ foreach ($activeDeployments as $deployment) {+ try {+ // Mark deployment as cancelled+ $deployment->update([+ 'status' => ApplicationDeploymentStatus::CANCELLED_BY_USER->value,+ ]);++ // Add cancellation log entry+ $deployment->addLogEntry('Deployment cancelled: Pull request closed.', 'stderr');++ // Try to kill helper container if it exists+ $this->killHelperContainer($deployment->deployment_uuid, $server);+ $cancelled++;+ } catch (\Throwable $e) {+ \Log::warning("Failed to cancel deployment {$deployment->id}: {$e->getMessage()}");+ }+ }++ return $cancelled;+ }++ /**+ * Kill the helper container used during deployment.+ */+ private function killHelperContainer(string $deployment_uuid, $server): void+ {+ try {+ $escapedUuid = escapeshellarg($deployment_uuid);+ $checkCommand = "docker ps -a --filter name={$escapedUuid} --format '{{.Names}}'";+ $containerExists = instant_remote_process([$checkCommand], $server);++ if ($containerExists && str($containerExists)->trim()->isNotEmpty()) {+ instant_remote_process(["docker rm -f {$escapedUuid}"], $server);+ }+ } catch (\Throwable $e) {+ // Silently handle - container may already be gone+ }+ }++ /**+ * Stop and remove all running containers for this PR.+ */+ private function stopRunningContainers(+ Application $application,+ int $pull_request_id,+ $server+ ): int {+ $killed = 0;++ try {+ if ($server->isSwarm()) {+ $escapedStackName = escapeshellarg("{$application->uuid}-{$pull_request_id}");+ instant_remote_process(["docker stack rm {$escapedStackName}"], $server);+ $killed++;+ } else {+ $containers = getCurrentApplicationContainerStatus(+ $server,+ $application->id,+ $pull_request_id+ );++ if ($containers->isNotEmpty()) {+ foreach ($containers as $container) {+ $containerName = data_get($container, 'Names');+ if ($containerName) {+ instant_remote_process(+ ["docker rm -f $containerName"],+ $server+ );+ $killed++;+ }+ }+ }+ }+ } catch (\Throwable $e) {+ \Log::warning("Error stopping containers for PR #{$pull_request_id}: {$e->getMessage()}");+ }++ return $killed;+ }+}diff --git a/app/Http/Controllers/Webhook/Bitbucket.php b/app/Http/Controllers/Webhook/Bitbucket.phpindex 2f228119de..d322452d38 100644--- a/app/Http/Controllers/Webhook/Bitbucket.php+++ b/app/Http/Controllers/Webhook/Bitbucket.php@@ -2,6 +2,7 @@namespace App\Http\Controllers\Webhook;+use App\Actions\Application\CleanupPreviewDeployment;use App\Http\Controllers\Controller;use App\Models\Application;use App\Models\ApplicationPreview;@@ -167,9 +168,10 @@ class Bitbucket extends Controllerif ($x_bitbucket_event === 'pullrequest:rejected' || $x_bitbucket_event === 'pullrequest:fulfilled') {$found = ApplicationPreview::where('application_id', $application->id)->where('pull_request_id', $pull_request_id)->first();if ($found) {- $found->delete();- $container_name = generateApplicationContainerName($application, $pull_request_id);- instant_remote_process(["docker rm -f $container_name"], $application->destination->server);+ // Use comprehensive cleanup that cancels active deployments,+ // kills helper containers, and removes all PR containers+ CleanupPreviewDeployment::run($application, $pull_request_id, $found);+$return_payloads->push(['application' => $application->name,'status' => 'success',diff --git a/app/Http/Controllers/Webhook/Gitea.php b/app/Http/Controllers/Webhook/Gitea.phpindex e41825abad..f85d14089b 100644--- a/app/Http/Controllers/Webhook/Gitea.php+++ b/app/Http/Controllers/Webhook/Gitea.php@@ -2,6 +2,7 @@namespace App\Http\Controllers\Webhook;+use App\Actions\Application\CleanupPreviewDeployment;use App\Http\Controllers\Controller;use App\Models\Application;use App\Models\ApplicationPreview;@@ -192,9 +193,10 @@ class Gitea extends Controllerif ($action === 'closed') {$found = ApplicationPreview::where('application_id', $application->id)->where('pull_request_id', $pull_request_id)->first();if ($found) {- $found->delete();- $container_name = generateApplicationContainerName($application, $pull_request_id);- instant_remote_process(["docker rm -f $container_name"], $application->destination->server);+ // Use comprehensive cleanup that cancels active deployments,+ // kills helper containers, and removes all PR containers+ CleanupPreviewDeployment::run($application, $pull_request_id, $found);+$return_payloads->push(['application' => $application->name,'status' => 'success',diff --git a/app/Http/Controllers/Webhook/Github.php b/app/Http/Controllers/Webhook/Github.phpindex 2402b71aed..93f2257739 100644--- a/app/Http/Controllers/Webhook/Github.php+++ b/app/Http/Controllers/Webhook/Github.php@@ -2,10 +2,10 @@namespace App\Http\Controllers\Webhook;+use App\Actions\Application\CleanupPreviewDeployment;use App\Enums\ProcessStatus;use App\Http\Controllers\Controller;use App\Jobs\ApplicationPullRequestUpdateJob;-use App\Jobs\DeleteResourceJob;use App\Jobs\GithubAppPermissionJob;use App\Models\Application;use App\Models\ApplicationPreview;@@ -221,41 +221,10 @@ class Github extends Controllerif ($action === 'closed') {$found = ApplicationPreview::where('application_id', $application->id)->where('pull_request_id', $pull_request_id)->first();if ($found) {- // Cancel any active deployments for this PR immediately- $activeDeployment = \App\Models\ApplicationDeploymentQueue::where('application_id', $application->id)- ->where('pull_request_id', $pull_request_id)- ->whereIn('status', [- \App\Enums\ApplicationDeploymentStatus::QUEUED->value,- \App\Enums\ApplicationDeploymentStatus::IN_PROGRESS->value,- ])- ->first();+ // Use comprehensive cleanup that cancels active deployments,+ // kills helper containers, and removes all PR containers+ CleanupPreviewDeployment::run($application, $pull_request_id, $found);- if ($activeDeployment) {- try {- // Mark deployment as cancelled- $activeDeployment->update([- 'status' => \App\Enums\ApplicationDeploymentStatus::CANCELLED_BY_USER->value,- ]);-- // Add cancellation log entry- $activeDeployment->addLogEntry('Deployment cancelled: Pull request closed.', 'stderr');-- // Check if helper container exists and kill it- $deployment_uuid = $activeDeployment->deployment_uuid;- $server = $application->destination->server;- $checkCommand = "docker ps -a --filter name={$deployment_uuid} --format '{{.Names}}'";- $containerExists = instant_remote_process([$checkCommand], $server);-- if ($containerExists && str($containerExists)->trim()->isNotEmpty()) {- instant_remote_process(["docker rm -f {$deployment_uuid}"], $server);- $activeDeployment->addLogEntry('Deployment container stopped.');- }- } catch (\Throwable $e) {- // Silently handle errors during deployment cancellation- }- }-- DeleteResourceJob::dispatch($found);$return_payloads->push(['application' => $application->name,'status' => 'success',@@ -466,53 +435,12 @@ class Github extends Controllerif ($action === 'closed' || $action === 'close') {$found = ApplicationPreview::where('application_id', $application->id)->where('pull_request_id', $pull_request_id)->first();if ($found) {- // Cancel any active deployments for this PR immediately- $activeDeployment = \App\Models\ApplicationDeploymentQueue::where('application_id', $application->id)- ->where('pull_request_id', $pull_request_id)- ->whereIn('status', [- \App\Enums\ApplicationDeploymentStatus::QUEUED->value,- \App\Enums\ApplicationDeploymentStatus::IN_PROGRESS->value,- ])- ->first();-- if ($activeDeployment) {- try {- // Mark deployment as cancelled- $activeDeployment->update([- 'status' => \App\Enums\ApplicationDeploymentStatus::CANCELLED_BY_USER->value,- ]);-- // Add cancellation log entry- $activeDeployment->addLogEntry('Deployment cancelled: Pull request closed.', 'stderr');-- // Check if helper container exists and kill it- $deployment_uuid = $activeDeployment->deployment_uuid;- $server = $application->destination->server;- $checkCommand = "docker ps -a --filter name={$deployment_uuid} --format '{{.Names}}'";- $containerExists = instant_remote_process([$checkCommand], $server);-- if ($containerExists && str($containerExists)->trim()->isNotEmpty()) {- instant_remote_process(["docker rm -f {$deployment_uuid}"], $server);- $activeDeployment->addLogEntry('Deployment container stopped.');- }-- } catch (\Throwable $e) {- // Silently handle errors during deployment cancellation- }- }-- // Clean up any deployed containers- $containers = getCurrentApplicationContainerStatus($application->destination->server, $application->id, $pull_request_id);- if ($containers->isNotEmpty()) {- $containers->each(function ($container) use ($application) {- $container_name = data_get($container, 'Names');- instant_remote_process(["docker rm -f $container_name"], $application->destination->server);- });- }-+ // Delete the PR comment on GitHub (GitHub-specific feature)ApplicationPullRequestUpdateJob::dispatchSync(application: $application, preview: $found, status: ProcessStatus::CLOSED);- DeleteResourceJob::dispatch($found);+ // Use comprehensive cleanup that cancels active deployments,+ // kills helper containers, and removes all PR containers+ CleanupPreviewDeployment::run($application, $pull_request_id, $found);$return_payloads->push(['application' => $application->name,diff --git a/app/Http/Controllers/Webhook/Gitlab.php b/app/Http/Controllers/Webhook/Gitlab.phpindex 56a9c0d1b8..9062d2875f 100644--- a/app/Http/Controllers/Webhook/Gitlab.php+++ b/app/Http/Controllers/Webhook/Gitlab.php@@ -2,6 +2,7 @@namespace App\Http\Controllers\Webhook;+use App\Actions\Application\CleanupPreviewDeployment;use App\Http\Controllers\Controller;use App\Models\Application;use App\Models\ApplicationPreview;@@ -224,22 +225,22 @@ class Gitlab extends Controller} elseif ($action === 'closed' || $action === 'close' || $action === 'merge') {$found = ApplicationPreview::where('application_id', $application->id)->where('pull_request_id', $pull_request_id)->first();if ($found) {- $found->delete();- $container_name = generateApplicationContainerName($application, $pull_request_id);- instant_remote_process(["docker rm -f $container_name"], $application->destination->server);+ // Use comprehensive cleanup that cancels active deployments,+ // kills helper containers, and removes all PR containers+ CleanupPreviewDeployment::run($application, $pull_request_id, $found);+$return_payloads->push(['application' => $application->name,'status' => 'success',- 'message' => 'Preview Deployment closed',+ 'message' => 'Preview deployment closed.',+ ]);+ } else {+ $return_payloads->push([+ 'application' => $application->name,+ 'status' => 'failed',+ 'message' => 'No preview deployment found.',]);-- return response($return_payloads);}- $return_payloads->push([- 'application' => $application->name,- 'status' => 'failed',- 'message' => 'No Preview Deployment found',- ]);} else {$return_payloads->push(['application' => $application->name,