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
refactor(github-webhook): restructure application processing by grouping applications by server for improved deployment handling
Andras Bacsai52312e9de6d4
diff --git a/app/Http/Controllers/Webhook/Github.php b/app/Http/Controllers/Webhook/Github.phpindex b940bf3943..5ba9c08e76 100644--- a/app/Http/Controllers/Webhook/Github.php+++ b/app/Http/Controllers/Webhook/Github.php@@ -97,162 +97,168 @@ class Github extends Controllerreturn response("Nothing to do. No applications found with branch '$base_branch'.");}}- foreach ($applications as $application) {- $webhook_secret = data_get($application, 'manual_webhook_secret_github');- $hmac = hash_hmac('sha256', $request->getContent(), $webhook_secret);- if (! hash_equals($x_hub_signature_256, $hmac) && ! isDev()) {- $return_payloads->push([- 'application' => $application->name,- 'status' => 'failed',- 'message' => 'Invalid signature.',- ]);+ $applicationsByServer = $applications->groupBy(function ($app) {+ return $app->destination->server_id;+ });- continue;- }- $isFunctional = $application->destination->server->isFunctional();- if (! $isFunctional) {- $return_payloads->push([- 'application' => $application->name,- 'status' => 'failed',- 'message' => 'Server is not functional.',- ]);+ foreach ($applicationsByServer as $serverId => $serverApplications) {+ foreach ($serverApplications as $application) {+ $webhook_secret = data_get($application, 'manual_webhook_secret_github');+ $hmac = hash_hmac('sha256', $request->getContent(), $webhook_secret);+ if (! hash_equals($x_hub_signature_256, $hmac) && ! isDev()) {+ $return_payloads->push([+ 'application' => $application->name,+ 'status' => 'failed',+ 'message' => 'Invalid signature.',+ ]);- continue;- }- if ($x_github_event === 'push') {- if ($application->isDeployable()) {- $is_watch_path_triggered = $application->isWatchPathsTriggered($changed_files);- if ($is_watch_path_triggered || is_null($application->watch_paths)) {- $deployment_uuid = new Cuid2;- $result = queue_application_deployment(- application: $application,- deployment_uuid: $deployment_uuid,- force_rebuild: false,- commit: data_get($payload, 'after', 'HEAD'),- is_webhook: true,- );- if ($result['status'] === 'skipped') {- $return_payloads->push([- 'application' => $application->name,- 'status' => 'skipped',- 'message' => $result['message'],- ]);+ continue;+ }+ $isFunctional = $application->destination->server->isFunctional();+ if (! $isFunctional) {+ $return_payloads->push([+ 'application' => $application->name,+ 'status' => 'failed',+ 'message' => 'Server is not functional.',+ ]);++ continue;+ }+ if ($x_github_event === 'push') {+ if ($application->isDeployable()) {+ $is_watch_path_triggered = $application->isWatchPathsTriggered($changed_files);+ if ($is_watch_path_triggered || is_null($application->watch_paths)) {+ $deployment_uuid = new Cuid2;+ $result = queue_application_deployment(+ application: $application,+ deployment_uuid: $deployment_uuid,+ force_rebuild: false,+ commit: data_get($payload, 'after', 'HEAD'),+ is_webhook: true,+ );+ if ($result['status'] === 'skipped') {+ $return_payloads->push([+ 'application' => $application->name,+ 'status' => 'skipped',+ 'message' => $result['message'],+ ]);+ } else {+ $return_payloads->push([+ 'application' => $application->name,+ 'status' => 'success',+ 'message' => 'Deployment queued.',+ 'application_uuid' => $application->uuid,+ 'application_name' => $application->name,+ 'deployment_uuid' => $result['deployment_uuid'],+ ]);+ }} else {+ $paths = str($application->watch_paths)->explode("\n");$return_payloads->push([- 'application' => $application->name,- 'status' => 'success',- 'message' => 'Deployment queued.',+ 'status' => 'failed',+ 'message' => 'Changed files do not match watch paths. Ignoring deployment.','application_uuid' => $application->uuid,'application_name' => $application->name,- 'deployment_uuid' => $result['deployment_uuid'],+ 'details' => [+ 'changed_files' => $changed_files,+ 'watch_paths' => $paths,+ ],]);}} else {- $paths = str($application->watch_paths)->explode("\n");$return_payloads->push(['status' => 'failed',- 'message' => 'Changed files do not match watch paths. Ignoring deployment.',+ 'message' => 'Deployments disabled.','application_uuid' => $application->uuid,'application_name' => $application->name,- 'details' => [- 'changed_files' => $changed_files,- 'watch_paths' => $paths,- ],]);}- } else {- $return_payloads->push([- 'status' => 'failed',- 'message' => 'Deployments disabled.',- 'application_uuid' => $application->uuid,- 'application_name' => $application->name,- ]);}- }- if ($x_github_event === 'pull_request') {- if ($action === 'opened' || $action === 'synchronize' || $action === 'reopened') {- if ($application->isPRDeployable()) {- // Check if PR deployments from public contributors are restricted- if (! $application->settings->is_pr_deployments_public_enabled) {- $trustedAssociations = ['OWNER', 'MEMBER', 'COLLABORATOR', 'CONTRIBUTOR'];- if (! in_array($author_association, $trustedAssociations)) {- $return_payloads->push([- 'application' => $application->name,- 'status' => 'failed',- 'message' => 'PR deployments are restricted to repository members and contributors. Author association: '.$author_association,- ]);+ if ($x_github_event === 'pull_request') {+ if ($action === 'opened' || $action === 'synchronize' || $action === 'reopened') {+ if ($application->isPRDeployable()) {+ // Check if PR deployments from public contributors are restricted+ if (! $application->settings->is_pr_deployments_public_enabled) {+ $trustedAssociations = ['OWNER', 'MEMBER', 'COLLABORATOR', 'CONTRIBUTOR'];+ if (! in_array($author_association, $trustedAssociations)) {+ $return_payloads->push([+ 'application' => $application->name,+ 'status' => 'failed',+ 'message' => 'PR deployments are restricted to repository members and contributors. Author association: '.$author_association,+ ]);- continue;+ continue;+ }}- }- $deployment_uuid = new Cuid2;- $found = ApplicationPreview::where('application_id', $application->id)->where('pull_request_id', $pull_request_id)->first();- if (! $found) {- if ($application->build_pack === 'dockercompose') {- $pr_app = ApplicationPreview::create([- 'git_type' => 'github',- 'application_id' => $application->id,- 'pull_request_id' => $pull_request_id,- 'pull_request_html_url' => $pull_request_html_url,- 'docker_compose_domains' => $application->docker_compose_domains,+ $deployment_uuid = new Cuid2;+ $found = ApplicationPreview::where('application_id', $application->id)->where('pull_request_id', $pull_request_id)->first();+ if (! $found) {+ if ($application->build_pack === 'dockercompose') {+ $pr_app = ApplicationPreview::create([+ 'git_type' => 'github',+ 'application_id' => $application->id,+ 'pull_request_id' => $pull_request_id,+ 'pull_request_html_url' => $pull_request_html_url,+ 'docker_compose_domains' => $application->docker_compose_domains,+ ]);+ $pr_app->generate_preview_fqdn_compose();+ } else {+ $pr_app = ApplicationPreview::create([+ 'git_type' => 'github',+ 'application_id' => $application->id,+ 'pull_request_id' => $pull_request_id,+ 'pull_request_html_url' => $pull_request_html_url,+ ]);+ $pr_app->generate_preview_fqdn();+ }+ }++ $result = queue_application_deployment(+ application: $application,+ pull_request_id: $pull_request_id,+ deployment_uuid: $deployment_uuid,+ force_rebuild: false,+ commit: data_get($payload, 'head.sha', 'HEAD'),+ is_webhook: true,+ git_type: 'github'+ );+ if ($result['status'] === 'skipped') {+ $return_payloads->push([+ 'application' => $application->name,+ 'status' => 'skipped',+ 'message' => $result['message'],]);- $pr_app->generate_preview_fqdn_compose();} else {- $pr_app = ApplicationPreview::create([- 'git_type' => 'github',- 'application_id' => $application->id,- 'pull_request_id' => $pull_request_id,- 'pull_request_html_url' => $pull_request_html_url,+ $return_payloads->push([+ 'application' => $application->name,+ 'status' => 'success',+ 'message' => 'Preview deployment queued.',]);- $pr_app->generate_preview_fqdn();}+ } else {+ $return_payloads->push([+ 'application' => $application->name,+ 'status' => 'failed',+ 'message' => 'Preview deployments disabled.',+ ]);}-- $result = queue_application_deployment(- application: $application,- pull_request_id: $pull_request_id,- deployment_uuid: $deployment_uuid,- force_rebuild: false,- commit: data_get($payload, 'head.sha', 'HEAD'),- is_webhook: true,- git_type: 'github'- );- if ($result['status'] === 'skipped') {+ }+ if ($action === 'closed') {+ $found = ApplicationPreview::where('application_id', $application->id)->where('pull_request_id', $pull_request_id)->first();+ if ($found) {+ DeleteResourceJob::dispatch($found);$return_payloads->push(['application' => $application->name,- 'status' => 'skipped',- 'message' => $result['message'],+ 'status' => 'success',+ 'message' => 'Preview deployment closed.',]);} else {$return_payloads->push(['application' => $application->name,- 'status' => 'success',- 'message' => 'Preview deployment queued.',+ 'status' => 'failed',+ 'message' => 'No preview deployment found.',]);}- } else {- $return_payloads->push([- 'application' => $application->name,- 'status' => 'failed',- 'message' => 'Preview deployments disabled.',- ]);- }- }- if ($action === 'closed') {- $found = ApplicationPreview::where('application_id', $application->id)->where('pull_request_id', $pull_request_id)->first();- if ($found) {- DeleteResourceJob::dispatch($found);- $return_payloads->push([- 'application' => $application->name,- 'status' => 'success',- 'message' => 'Preview deployment closed.',- ]);- } else {- $return_payloads->push([- 'application' => $application->name,- 'status' => 'failed',- 'message' => 'No preview deployment found.',- ]);}}}@@ -358,141 +364,147 @@ class Github extends Controllerreturn response("Nothing to do. No applications found with branch '$base_branch'.");}}- foreach ($applications as $application) {- $isFunctional = $application->destination->server->isFunctional();- if (! $isFunctional) {- $return_payloads->push([- 'status' => 'failed',- 'message' => 'Server is not functional.',- 'application_uuid' => $application->uuid,- 'application_name' => $application->name,- ]);+ $applicationsByServer = $applications->groupBy(function ($app) {+ return $app->destination->server_id;+ });- continue;- }- if ($x_github_event === 'push') {- if ($application->isDeployable()) {- $is_watch_path_triggered = $application->isWatchPathsTriggered($changed_files);- if ($is_watch_path_triggered || is_null($application->watch_paths)) {- $deployment_uuid = new Cuid2;- $result = queue_application_deployment(- application: $application,- deployment_uuid: $deployment_uuid,- commit: data_get($payload, 'after', 'HEAD'),- force_rebuild: false,- is_webhook: true,- );- $return_payloads->push([- 'status' => $result['status'],- 'message' => $result['message'],- 'application_uuid' => $application->uuid,- 'application_name' => $application->name,- 'deployment_uuid' => $result['deployment_uuid'],- ]);- } else {- $paths = str($application->watch_paths)->explode("\n");- $return_payloads->push([- 'status' => 'failed',- 'message' => 'Changed files do not match watch paths. Ignoring deployment.',- 'application_uuid' => $application->uuid,- 'application_name' => $application->name,- 'details' => [- 'changed_files' => $changed_files,- 'watch_paths' => $paths,- ],- ]);- }- } else {+ foreach ($applicationsByServer as $serverId => $serverApplications) {+ foreach ($serverApplications as $application) {+ $isFunctional = $application->destination->server->isFunctional();+ if (! $isFunctional) {$return_payloads->push(['status' => 'failed',- 'message' => 'Deployments disabled.',+ 'message' => 'Server is not functional.','application_uuid' => $application->uuid,'application_name' => $application->name,]);- }- }- if ($x_github_event === 'pull_request') {- if ($action === 'opened' || $action === 'synchronize' || $action === 'reopened') {- if ($application->isPRDeployable()) {- // Check if PR deployments from public contributors are restricted- if (! $application->settings->is_pr_deployments_public_enabled) {- $trustedAssociations = ['OWNER', 'MEMBER', 'COLLABORATOR', 'CONTRIBUTOR'];- if (! in_array($author_association, $trustedAssociations)) {- $return_payloads->push([- 'application' => $application->name,- 'status' => 'failed',- 'message' => 'PR deployments are restricted to repository members and contributors. Author association: '.$author_association,- ]);- continue;- }- }- $deployment_uuid = new Cuid2;- $found = ApplicationPreview::where('application_id', $application->id)->where('pull_request_id', $pull_request_id)->first();- if (! $found) {- ApplicationPreview::create([- 'git_type' => 'github',- 'application_id' => $application->id,- 'pull_request_id' => $pull_request_id,- 'pull_request_html_url' => $pull_request_html_url,- ]);- }- $result = queue_application_deployment(- application: $application,- pull_request_id: $pull_request_id,- deployment_uuid: $deployment_uuid,- force_rebuild: false,- commit: data_get($payload, 'head.sha', 'HEAD'),- is_webhook: true,- git_type: 'github'- );- if ($result['status'] === 'skipped') {+ continue;+ }+ if ($x_github_event === 'push') {+ if ($application->isDeployable()) {+ $is_watch_path_triggered = $application->isWatchPathsTriggered($changed_files);+ if ($is_watch_path_triggered || is_null($application->watch_paths)) {+ $deployment_uuid = new Cuid2;+ $result = queue_application_deployment(+ application: $application,+ deployment_uuid: $deployment_uuid,+ commit: data_get($payload, 'after', 'HEAD'),+ force_rebuild: false,+ is_webhook: true,+ );$return_payloads->push([- 'application' => $application->name,- 'status' => 'skipped',+ 'status' => $result['status'],'message' => $result['message'],+ 'application_uuid' => $application->uuid,+ 'application_name' => $application->name,+ 'deployment_uuid' => $result['deployment_uuid'],]);} else {+ $paths = str($application->watch_paths)->explode("\n");$return_payloads->push([- 'application' => $application->name,- 'status' => 'success',- 'message' => 'Preview deployment queued.',+ 'status' => 'failed',+ 'message' => 'Changed files do not match watch paths. Ignoring deployment.',+ 'application_uuid' => $application->uuid,+ 'application_name' => $application->name,+ 'details' => [+ 'changed_files' => $changed_files,+ 'watch_paths' => $paths,+ ],]);}} else {$return_payloads->push([- 'application' => $application->name,'status' => 'failed',- 'message' => 'Preview deployments disabled.',+ 'message' => 'Deployments disabled.',+ 'application_uuid' => $application->uuid,+ 'application_name' => $application->name,]);}}- if ($action === 'closed' || $action === 'close') {- $found = ApplicationPreview::where('application_id', $application->id)->where('pull_request_id', $pull_request_id)->first();- if ($found) {- $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);- });+ if ($x_github_event === 'pull_request') {+ if ($action === 'opened' || $action === 'synchronize' || $action === 'reopened') {+ if ($application->isPRDeployable()) {+ // Check if PR deployments from public contributors are restricted+ if (! $application->settings->is_pr_deployments_public_enabled) {+ $trustedAssociations = ['OWNER', 'MEMBER', 'COLLABORATOR', 'CONTRIBUTOR'];+ if (! in_array($author_association, $trustedAssociations)) {+ $return_payloads->push([+ 'application' => $application->name,+ 'status' => 'failed',+ 'message' => 'PR deployments are restricted to repository members and contributors. Author association: '.$author_association,+ ]);++ continue;+ }+ }+ $deployment_uuid = new Cuid2;+ $found = ApplicationPreview::where('application_id', $application->id)->where('pull_request_id', $pull_request_id)->first();+ if (! $found) {+ ApplicationPreview::create([+ 'git_type' => 'github',+ 'application_id' => $application->id,+ 'pull_request_id' => $pull_request_id,+ 'pull_request_html_url' => $pull_request_html_url,+ ]);+ }+ $result = queue_application_deployment(+ application: $application,+ pull_request_id: $pull_request_id,+ deployment_uuid: $deployment_uuid,+ force_rebuild: false,+ commit: data_get($payload, 'head.sha', 'HEAD'),+ is_webhook: true,+ git_type: 'github'+ );+ if ($result['status'] === 'skipped') {+ $return_payloads->push([+ 'application' => $application->name,+ 'status' => 'skipped',+ 'message' => $result['message'],+ ]);+ } else {+ $return_payloads->push([+ 'application' => $application->name,+ 'status' => 'success',+ 'message' => 'Preview deployment queued.',+ ]);+ }+ } else {+ $return_payloads->push([+ 'application' => $application->name,+ 'status' => 'failed',+ 'message' => 'Preview deployments disabled.',+ ]);}+ }+ if ($action === 'closed' || $action === 'close') {+ $found = ApplicationPreview::where('application_id', $application->id)->where('pull_request_id', $pull_request_id)->first();+ if ($found) {+ $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);+ });+ }- ApplicationPullRequestUpdateJob::dispatchSync(application: $application, preview: $found, status: ProcessStatus::CLOSED);+ ApplicationPullRequestUpdateJob::dispatchSync(application: $application, preview: $found, status: ProcessStatus::CLOSED);- DeleteResourceJob::dispatch($found);+ DeleteResourceJob::dispatch($found);- $return_payloads->push([- 'application' => $application->name,- 'status' => 'success',- 'message' => 'Preview deployment closed.',- ]);- } else {- $return_payloads->push([- 'application' => $application->name,- 'status' => 'failed',- 'message' => 'No preview deployment found.',- ]);+ $return_payloads->push([+ 'application' => $application->name,+ 'status' => 'success',+ 'message' => 'Preview deployment closed.',+ ]);+ } else {+ $return_payloads->push([+ 'application' => $application->name,+ 'status' => 'failed',+ 'message' => 'No preview deployment found.',+ ]);+ }}}}diff --git a/bootstrap/helpers/applications.php b/bootstrap/helpers/applications.phpindex 919b2bde5d..975c6fcf14 100644--- a/bootstrap/helpers/applications.php+++ b/bootstrap/helpers/applications.php@@ -147,7 +147,7 @@ function next_after_cancel(?Server $server = null)foreach ($next_found as $next) {$server = Server::find($next->server_id);$concurrent_builds = $server->settings->concurrent_builds;- $inprogress_deployments = ApplicationDeploymentQueue::where('server_id', $next->server_id)->whereIn('status', [ApplicationDeploymentStatus::QUEUED])->get()->sortByDesc('created_at');+ $inprogress_deployments = ApplicationDeploymentQueue::where('server_id', $next->server_id)->whereIn('status', [ApplicationDeploymentStatus::IN_PROGRESS])->get()->sortByDesc('created_at');if ($inprogress_deployments->count() < $concurrent_builds) {$next->update(['status' => ApplicationDeploymentStatus::IN_PROGRESS->value,