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
feat(github-runners): support deployment and build servers (#12109)
Andras Bacsai38861e139407
diff --git a/app/Http/Controllers/Api/ServersController.php b/app/Http/Controllers/Api/ServersController.phpindex c55eb6c8f8..5fa1801a5e 100644--- a/app/Http/Controllers/Api/ServersController.php+++ b/app/Http/Controllers/Api/ServersController.php@@ -816,10 +816,10 @@ class ServersController extends Controller], 422);}- if ($serverRole !== null && $serverRole !== ServerRole::BUILD && $server->hasEnabledGithubRunners()) {+ if ($serverRole === ServerRole::DEPLOYMENT && $server->hasEnabledGithubRunners()) {return response()->json(['message' => 'Validation failed.',- 'errors' => ['server_role' => ['Disable the GitHub runners before you change the role of this server.']],+ 'errors' => ['server_role' => ['Disable the GitHub runners before you set this server to deployments only.']],], 422);}diff --git a/app/Jobs/ProvisionGithubRunnerJob.php b/app/Jobs/ProvisionGithubRunnerJob.phpindex f170c34c3b..c5b1148448 100644--- a/app/Jobs/ProvisionGithubRunnerJob.php+++ b/app/Jobs/ProvisionGithubRunnerJob.php@@ -19,7 +19,7 @@ use Illuminate\Support\Sleep;use RuntimeException;/**- * Starts one ephemeral runner for a queued execution on the least busy matching build server.+ * Starts one ephemeral runner for a queued execution on the least busy matching server.* When no server has free capacity, the execution stays queued and is retried by* CleanupGithubRunnerJob or ReconcileGithubRunnersJob. A runner that fails to start is removed and* started again, possibly on another server, up to MAX_ATTEMPTS times.@@ -191,7 +191,7 @@ class ProvisionGithubRunnerJob implements ShouldBeEncrypted, ShouldQueue->filter(fn (GithubRunnerConfig $config) => $config->server !== null&& $config->matchesLabels($execution->labels ?? [])&& ($config->allow_pull_requests || ! $execution->is_pull_request)- && $config->server->isBuildServer()+ && $config->server->canBuildApplications()&& ! $config->server->isLocalhost()&& $config->server->isFunctional())->map(fn (GithubRunnerConfig $config) => ['config' => $config, 'occupied' => $config->occupiedRunnerCount()])diff --git a/app/Jobs/ReconcileGithubRunnersJob.php b/app/Jobs/ReconcileGithubRunnersJob.phpindex 6ec70a26b2..f3adb513c4 100644--- a/app/Jobs/ReconcileGithubRunnersJob.php+++ b/app/Jobs/ReconcileGithubRunnersJob.php@@ -17,7 +17,7 @@ use Illuminate\Queue\SerializesModels;use Illuminate\Support\Collection;/**- * Compares runner executions with the runner containers on each build server. It cleans up+ * Compares runner executions with the runner containers on each runner server. It cleans up* stopped and orphaned runners, applies the wait, idle, and job timeouts, and restarts* provisioning for queued executions whose dispatch was lost.*/@@ -161,7 +161,7 @@ class ReconcileGithubRunnersJob implements ShouldBeUnique, ShouldQueueforeach ($executions as $execution) {if (($execution->queued_at ?? $execution->created_at)->lt(now()->subMinutes($timeout))) {- $execution->finish(GithubRunnerStatus::TimedOut, "No build server had free capacity within {$timeout} minutes.");+ $execution->finish(GithubRunnerStatus::TimedOut, "No runner server had free capacity within {$timeout} minutes.");}}diff --git a/app/Livewire/Server/GithubRunners.php b/app/Livewire/Server/GithubRunners.phpindex 3613339f28..598a8485fc 100644--- a/app/Livewire/Server/GithubRunners.php+++ b/app/Livewire/Server/GithubRunners.php@@ -183,8 +183,8 @@ class GithubRunners extends Component}try {- if (! $this->server->isBuildServer()) {- throw new \RuntimeException('GitHub Actions runners can only run on servers with the Build role.');+ if (! $this->server->canBuildApplications()) {+ throw new \RuntimeException('GitHub Actions runners need a server with the Builds only or Deployments and builds role.');}if ($this->config?->is_enabled ?? true) {@@ -242,8 +242,8 @@ class GithubRunners extends Component});$this->dispatch('success', 'GitHub runners disabled. Running jobs continue until they finish.');} else {- if (! $this->server->isBuildServer()) {- throw new \RuntimeException('GitHub Actions runners can only run on servers with the Build role.');+ if (! $this->server->canBuildApplications()) {+ throw new \RuntimeException('GitHub Actions runners need a server with the Builds only or Deployments and builds role.');}$this->authorize('update', $config->githubApp);$this->prepareRunnerGroup($config->githubApp);diff --git a/app/Livewire/Server/Show.php b/app/Livewire/Server/Show.phpindex cea2c3f465..8357e89257 100644--- a/app/Livewire/Server/Show.php+++ b/app/Livewire/Server/Show.php@@ -476,9 +476,9 @@ class Show extends Component$newRole = ServerRole::from($this->serverRole);$currentRole = $this->server->settings()->firstOrFail()->effectiveServerRole();- if ($newRole !== ServerRole::BUILD && $this->server->hasEnabledGithubRunners()) {+ if ($newRole === ServerRole::DEPLOYMENT && $this->server->hasEnabledGithubRunners()) {$this->serverRole = $currentRole->value;- $this->dispatch('error', 'Disable the GitHub runners before you change the role of this server.');+ $this->dispatch('error', 'Disable the GitHub runners before you set this server to deployments only.');return;}diff --git a/app/Services/GithubRunner/GithubRunnerContainer.php b/app/Services/GithubRunner/GithubRunnerContainer.phpindex c970490be7..09839249a5 100644--- a/app/Services/GithubRunner/GithubRunnerContainer.php+++ b/app/Services/GithubRunner/GithubRunnerContainer.php@@ -162,7 +162,7 @@ class GithubRunnerContainer#!/bin/bashcase "\${GITHUB_EVENT_NAME:-}" in{$events}|"")- echo "::error::This Coolify runner does not run pull request jobs. Allow pull request jobs in the GitHub runner settings of the build server to run them."+ echo "::error::This Coolify runner does not run pull request jobs. Allow pull request jobs in the GitHub runner settings of the server to run them."exit 1;;esacdiff --git a/resources/views/livewire/server/github-runners.blade.php b/resources/views/livewire/server/github-runners.blade.phpindex b91886484e..96b918b28a 100644--- a/resources/views/livewire/server/github-runners.blade.php+++ b/resources/views/livewire/server/github-runners.blade.php@@ -10,7 +10,7 @@<x-server.sidebar :server="$server" activeMenu="github-runners" /><div class="application-settings-form flex w-full flex-col gap-6">- @if (!$server->isBuildServer())+ @if (! $server->canBuildApplications())<x-application.settings-section id="github-runners-section" title="GitHub Actions runners"helper="Each workflow job runs in a new container that is deleted after the job."><x-slot:actions>@@ -22,13 +22,13 @@</a></x-slot:actions><x-empty size="sm" title="This server is not enabled for builds"- description="GitHub Actions runners only run on servers with the Builds only role. Change the server role in the General settings."+ description="GitHub Actions runners need the Builds only or the Deployments and builds role. Change the server role in the General settings."icon-name="play-circle" /></x-application.settings-section>@elseif ($this->githubApps->isEmpty())<x-callout type="info" title="No organization GitHub App">Runners are registered at organization level. Add a GitHub App that belongs to an organization- under Sources, and select "Run workflow jobs on build servers" when you register it.+ under Sources, and select "Run workflow jobs on your servers" when you register it.</x-callout>@elseif (! $this->config?->is_enabled)<x-application.settings-section id="github-runners-section" title="GitHub Actions runners"@@ -40,10 +40,24 @@description="Enable runners to take GitHub Actions workflow jobs on this server."icon-name="play-circle"><x-slot:contents>- <x-forms.button canGate="update" :canResource="$server" isHighlighted- wire:click="toggleEnabled" wire:loading.attr="disabled" wire:target="toggleEnabled">- Enable runners- </x-forms.button>+ @if ($server->isBuildServer())+ <x-forms.button canGate="update" :canResource="$server" isHighlighted+ wire:click="toggleEnabled" wire:loading.attr="disabled" wire:target="toggleEnabled">+ Enable runners+ </x-forms.button>+ @else+ <x-modal-confirmation title="Enable runners on this server?"+ submitAction="toggleEnabled" :confirmWithText="false"+ :confirmWithPassword="false" step2ButtonText="Enable runners"+ warningMessage="This server also runs your resources. We do not recommend GitHub Actions runners here: workflow jobs use the same CPU, memory, and disk, and a heavy job can slow down or stop your resources. Use a server with the Builds only role for runners when you can."+ :actions="['Workflow jobs of the GitHub organization will run on this server next to your resources.']">+ <x-slot:trigger>+ <x-forms.button type="button" canGate="update" :canResource="$server" isHighlighted>+ Enable runners+ </x-forms.button>+ </x-slot:trigger>+ </x-modal-confirmation>+ @endif</x-slot:contents></x-empty></x-application.settings-section>@@ -63,6 +77,13 @@@endcan</x-slot:actions>+ @if (! $server->isBuildServer())+ <x-callout type="warning" title="Runners share this server with your resources" class="mb-4">+ Workflow jobs use the same CPU, memory, and disk as the resources on this server. A heavy+ job can slow down or stop them. Use a server with the Builds only role for runners when+ you can.+ </x-callout>+ @endif<div x-cloak x-show="$wire.dockerMode === 'dind'"><x-callout type="warning" title="Use only for trusted repositories">In the privileged Docker mode, a malicious workflow can take control of this server.@@ -99,12 +120,14 @@<x-forms.input id="labels" label="Labels" required canGate="update" :canResource="$server"placeholder="coolify"helper="Comma-separated custom labels. Coolify also registers self-hosted and linux. Jobs must ask for at least one custom label." />- <x-forms.listbox id="isDedicated" label="Application builds" canGate="update"- :canResource="$server" :options="[- ['value' => false, 'label' => 'Also build applications'],- ['value' => true, 'label' => 'Dedicated to runners'],- ]"- helper="Dedicated servers are not used for application builds while runners are enabled." />+ @if ($server->isBuildServer())+ <x-forms.listbox id="isDedicated" label="Application builds" canGate="update"+ :canResource="$server" :options="[+ ['value' => false, 'label' => 'Also build applications'],+ ['value' => true, 'label' => 'Dedicated to runners'],+ ]"+ helper="Dedicated servers are not used for application builds while runners are enabled." />+ @endif<x-forms.listbox id="allowPullRequests" label="Pull request jobs" canGate="update":canResource="$server" :options="[['value' => false, 'label' => 'Refuse pull request jobs'],@@ -189,7 +212,7 @@@endcan@endif- @if ($server->isBuildServer() && $this->config?->is_enabled)+ @if ($server->canBuildApplications() && $this->config?->is_enabled)<x-application.settings-section id="github-runners-executions-section" title="Recent runners"helper="Queued jobs, active runners, and their results. The list updates every 10 seconds." flush><livewire:server.github-runner-executions :server="$server" />diff --git a/resources/views/livewire/server/new/by-ip.blade.php b/resources/views/livewire/server/new/by-ip.blade.phpindex 772ede2ac5..628f6656b3 100644--- a/resources/views/livewire/server/new/by-ip.blade.php+++ b/resources/views/livewire/server/new/by-ip.blade.php@@ -84,11 +84,11 @@<x-forms.input type="number" id="port" label="Port" required /></div><x-forms.listbox id="server_role"- helper="Choose whether this server runs deployments, application builds, or both. GitHub Actions runners need the Builds only role."+ helper="Choose whether this server runs deployments, application builds, or both. GitHub Actions runners need the Builds only or the Deployments and builds role."label="Server role" :options="[['value' => 'deployment', 'label' => 'Deployments only', 'description' => 'Runs your resources. Images are built on a build server.'],- ['value' => 'build', 'label' => 'Builds only', 'description' => 'Builds images for other servers. Required for GitHub Actions runners.'],- ['value' => 'both', 'label' => 'Deployments and builds', 'description' => 'Builds and runs your resources on this server.'],+ ['value' => 'build', 'label' => 'Builds only', 'description' => 'Builds images for other servers and runs GitHub Actions runners.'],+ ['value' => 'both', 'label' => 'Deployments and builds', 'description' => 'Builds and runs your resources on this server. Can also run GitHub Actions runners.'],]" /></x-forms.collapsible></x-application.settings-section>diff --git a/resources/views/livewire/server/show.blade.php b/resources/views/livewire/server/show.blade.phpindex ff159bdf1a..565fda89a3 100644--- a/resources/views/livewire/server/show.blade.php+++ b/resources/views/livewire/server/show.blade.php@@ -238,11 +238,11 @@<div class="mt-4 border-t border-neutral-200 pt-4 dark:border-white/[0.08]"><x-forms.listbox canGate="update" :canResource="$server" id="serverRole"label="Server role" onChange="requestServerRoleChange"- helper="Builds can use large amounts of CPU and memory. Deployments on the same server can become slow or unreachable during a build. GitHub Actions runners need the Builds only role."+ helper="Builds can use large amounts of CPU and memory. Deployments on the same server can become slow or unreachable during a build. GitHub Actions runners need the Builds only or the Deployments and builds role.":disabled="$isValidating" :options="[['value' => 'deployment', 'label' => 'Deployments only', 'description' => 'Runs your resources. Images are built on a build server.'],- ['value' => 'build', 'label' => 'Builds only', 'description' => 'Builds images for other servers. Required for GitHub Actions runners.'],- ['value' => 'both', 'label' => 'Deployments and builds', 'description' => 'Builds and runs your resources on this server.'],+ ['value' => 'build', 'label' => 'Builds only', 'description' => 'Builds images for other servers and runs GitHub Actions runners.'],+ ['value' => 'both', 'label' => 'Deployments and builds', 'description' => 'Builds and runs your resources on this server. Can also run GitHub Actions runners.'],]" /></div>@endifdiff --git a/resources/views/livewire/source/github/change.blade.php b/resources/views/livewire/source/github/change.blade.phpindex 38cef644d9..383d54bc6a 100644--- a/resources/views/livewire/source/github/change.blade.php+++ b/resources/views/livewire/source/github/change.blade.php@@ -314,7 +314,7 @@:disabled="blank($github_app->organization)":options="[['value' => false, 'label' => 'Do not run workflow jobs'],- ['value' => true, 'label' => 'Run workflow jobs on build servers'],+ ['value' => true, 'label' => 'Run workflow jobs on your servers'],]":helper="filled($github_app->organization)? 'Adds the organization Self-hosted runners (write) and Actions (read) permissions and the Workflow job webhook event.'diff --git a/resources/views/livewire/source/github/create.blade.php b/resources/views/livewire/source/github/create.blade.phpindex e61a029f37..d60517d4c7 100644--- a/resources/views/livewire/source/github/create.blade.php+++ b/resources/views/livewire/source/github/create.blade.php@@ -37,7 +37,7 @@<x-forms.checkbox id="use_for_pull_request_previews" label="Pull request previews"helper="Deploys pull requests and posts deployment status on them." /><x-forms.checkbox id="use_for_github_runners" live label="GitHub Actions runners"- helper="Runs workflow jobs on your build servers. Needs an organization." />+ helper="Runs workflow jobs on your servers. Needs an organization." /></div></div>diff --git a/resources/views/livewire/source/github/permissions.blade.php b/resources/views/livewire/source/github/permissions.blade.phpindex 6f6884ceed..ef2162e902 100644--- a/resources/views/livewire/source/github/permissions.blade.php+++ b/resources/views/livewire/source/github/permissions.blade.php@@ -27,7 +27,7 @@@php($missingRunnerRequirements = $github_app->missingRunnerRequirements())<x-application.settings-section title="GitHub Actions runners"- description="Build servers can run the organization's workflow jobs in Docker containers. Configure them in the GitHub Runners menu of a build server.">+ description="Your servers can run the organization's workflow jobs in Docker containers. Configure them in the GitHub Runners menu of a server."><div class="grid gap-4 lg:grid-cols-3"><x-forms.input canGate="view" :canResource="$github_app"label="Self-hosted runners" readonly placeholder="N/A"diff --git a/tests/Feature/GithubRunnerFlowTest.php b/tests/Feature/GithubRunnerFlowTest.phpindex 37663a26a9..d6d0ea49bd 100644--- a/tests/Feature/GithubRunnerFlowTest.php+++ b/tests/Feature/GithubRunnerFlowTest.php@@ -480,14 +480,14 @@ describe('workflow_job webhook', function () {});describe('provisioning', function () {- it('starts a runner on the least busy matching build server', function () {+ it('starts a runner on the least busy matching server that can build', function () {Queue::fake();fakeRunnerGithubApi($this->githubApp);Process::fake(['*' => Process::result(output: '29.8.0')]);$busyServer = runnerTestServer($this->team);$busyConfig = runnerTestConfig($busyServer, $this->githubApp);runnerTestExecution($this->githubApp, ['status' => GithubRunnerStatus::Running, 'server_id' => $busyServer->id, 'github_runner_config_id' => $busyConfig->id]);- $freeServer = runnerTestServer($this->team);+ $freeServer = runnerTestServer($this->team, 'both');runnerTestConfig($freeServer, $this->githubApp);$execution = runnerTestExecution($this->githubApp);@@ -615,7 +615,7 @@ describe('provisioning', function () {Process::assertNothingRan();});- it('does not use servers without the build role, disabled configs, or other labels', function (string $role, array $configAttributes) {+ it('does not use deployments only servers, disabled configs, or other labels', function (string $role, array $configAttributes) {Queue::fake();Process::fake();$server = runnerTestServer($this->team, $role);@@ -627,7 +627,7 @@ describe('provisioning', function () {expect($execution->fresh()->status)->toBe(GithubRunnerStatus::Queued);Process::assertNothingRan();})->with([- 'combined role' => ['both', []],+ 'deployments only role' => ['deployment', []],'disabled config' => ['build', ['is_enabled' => false]],'other labels' => ['build', ['labels' => ['gpu']]],]);diff --git a/tests/Feature/GithubRunnerSettingsTest.php b/tests/Feature/GithubRunnerSettingsTest.phpindex 65052a0fcb..e06a072e8e 100644--- a/tests/Feature/GithubRunnerSettingsTest.php+++ b/tests/Feature/GithubRunnerSettingsTest.php@@ -268,21 +268,47 @@ describe('runner settings page', function () {->assertRedirect(route('server.index'));});- it('refuses to save runners on a server without the build role', function () {+ it('asks for confirmation before it enables runners on a server that also runs resources', function () {$server = settingsTestServer($this->team, 'both');+ fakeRunnerGroupApi($this->githubApp);++ Livewire::actingAs($this->owner)+ ->test(GithubRunners::class, ['server_uuid' => $server->uuid])+ ->assertSee('Enable runners on this server?')+ ->assertDontSee('Application builds')+ ->call('toggleEnabled')+ ->assertDispatched('success')+ ->assertSee('Runners share this server with your resources');++ expect(GithubRunnerConfig::sole())->server_id->toBe($server->id)->is_enabled->toBeTrue();+ });++ it('refuses runners on a deployments only server', function () {+ $server = settingsTestServer($this->team, 'deployment');Http::fake();Livewire::actingAs($this->owner)->test(GithubRunners::class, ['server_uuid' => $server->uuid])->assertSee('This server is not enabled for builds')- ->assertSee(route('server.show', ['server_uuid' => $server->uuid]))- ->assertSee(route('server.github-runners', ['server_uuid' => $server->uuid]))->assertDontSee('Recent runners')->set('githubAppId', $this->githubApp->id)- ->call('submit')+ ->call('toggleEnabled')->assertDispatched('error');expect(GithubRunnerConfig::count())->toBe(0);+ Http::assertNothingSent();+ });++ it('enables runners on a build server without a confirmation', function () {+ fakeRunnerGroupApi($this->githubApp);++ Livewire::actingAs($this->owner)+ ->test(GithubRunners::class, ['server_uuid' => $this->server->uuid])+ ->assertDontSee('Enable runners on this server?')+ ->call('toggleEnabled')+ ->assertDispatched('success')+ ->assertSee('Application builds')+ ->assertDontSee('Runners share this server with your resources');});it('disables runners and removes only the idle ones', function () {@@ -539,25 +565,49 @@ describe('build server role', function () {GithubRunnerConfig::create(['server_id' => $this->server->id, 'github_app_id' => $this->githubApp->id, 'labels' => ['coolify']]);});- it('blocks a role change in the UI while runners are enabled', function () {+ it('changes the role in the UI while runners are enabled', function () {Livewire::actingAs($this->owner)->test(Show::class, ['server_uuid' => $this->server->uuid])->set('serverRole', 'both')->call('requestServerRoleChange')+ ->assertNotDispatched('error')+ ->assertDispatched('open-server-role-confirmation')+ ->call('confirmServerRoleChange');++ expect($this->server->settings->fresh()->server_role->value)->toBe('both');+ });++ it('blocks a change to deployments only in the UI while runners are enabled', function () {+ Livewire::actingAs($this->owner)+ ->test(Show::class, ['server_uuid' => $this->server->uuid])+ ->set('serverRole', 'deployment')+ ->call('requestServerRoleChange')->assertSet('serverRole', 'build')->assertDispatched('error');expect($this->server->settings->fresh()->server_role->value)->toBe('build');});- it('blocks a role change through the API while runners are enabled', function () {+ it('blocks a change to deployments only through the API while runners are enabled', function () {$token = $this->owner->createToken('runner-test', ['*']);$token->accessToken->forceFill(['team_id' => $this->team->id])->save();+ settingsTestServer($this->team, 'build');$this->withHeaders(['Authorization' => 'Bearer '.$token->plainTextToken])- ->patchJson('/api/v1/servers/'.$this->server->uuid, ['server_role' => 'both'])+ ->patchJson('/api/v1/servers/'.$this->server->uuid, ['server_role' => 'deployment'])->assertStatus(422)- ->assertJsonPath('errors.server_role.0', 'Disable the GitHub runners before you change the role of this server.');+ ->assertJsonPath('errors.server_role.0', 'Disable the GitHub runners before you set this server to deployments only.');+ });++ it('changes the role through the API while runners are enabled', function () {+ $token = $this->owner->createToken('runner-test', ['*']);+ $token->accessToken->forceFill(['team_id' => $this->team->id])->save();++ $this->withHeaders(['Authorization' => 'Bearer '.$token->plainTextToken])+ ->patchJson('/api/v1/servers/'.$this->server->uuid, ['server_role' => 'both'])+ ->assertSuccessful();++ expect($this->server->settings->fresh()->server_role->value)->toBe('both');});it('leaves dedicated runner servers out of application builds', function () {