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: deploy key based deployments
Andras Bacsai946758e219f5
diff --git a/app/Http/Livewire/Project/New/GithubPrivateRepositoryDeployKey.php b/app/Http/Livewire/Project/New/GithubPrivateRepositoryDeployKey.phpnew file mode 100644index 0000000000..de470d39e2--- /dev/null+++ b/app/Http/Livewire/Project/New/GithubPrivateRepositoryDeployKey.php@@ -0,0 +1,113 @@+<?php++namespace App\Http\Livewire\Project\New;++use App\Models\Application;+use App\Models\PrivateKey;+use App\Models\Project;+use App\Models\StandaloneDocker;+use App\Models\SwarmDocker;+use Livewire\Component;+use Spatie\Url\Url;++class GithubPrivateRepositoryDeployKey extends Component+{+ public $parameters;+ public $private_keys;+ public int $private_key_id;+ public string $repository_url;++ public $servers;+ public $standalone_docker;+ public $swarm_docker;+ public $chosenServer;+ public $chosenDestination;++ public int $port = 3000;+ public string $type;++ public bool $is_static = false;+ public null|string $publish_directory = null;+ protected $rules = [+ 'repository_url' => 'required|url',+ 'port' => 'required|numeric',+ 'is_static' => 'required|boolean',+ 'publish_directory' => 'nullable|string',+ ];+ public function mount()+ {+ if (config('app.env') === 'local') {+ $this->repository_url = 'https://github.com/coollabsio/coolify-examples/tree/nodejs-fastify';+ }+ $this->parameters = getParameters();+ $this->private_keys = PrivateKey::where('team_id', session('currentTeam')->id)->get();+ $this->servers = session('currentTeam')->load(['servers'])->servers;+ }+ public function chooseServer($server)+ {+ $this->chosenServer = $server;+ $this->standalone_docker = StandaloneDocker::where('server_id', $server['id'])->get();+ $this->swarm_docker = SwarmDocker::where('server_id', $server['id'])->get();+ }+ public function setDestination($destination_uuid, $destination_type)+ {+ $class = "App\Models\\{$destination_type}";+ $instance = new $class;+ $this->chosenDestination = $instance::where('uuid', $destination_uuid)->first();+ }+ public function instantSave()+ {+ if ($this->is_static) {+ $this->port = 80;+ $this->publish_directory = '/dist';+ } else {+ $this->port = 3000;+ $this->publish_directory = null;+ }+ }+ public function setPrivateKey($private_key_id)+ {+ $this->private_key_id = $private_key_id;+ }+ public function submit()+ {+ $this->validate();+ $url = Url::fromString($this->repository_url);+ $git_host = $url->getHost();+ $git_repository = $url->getSegment(1) . '/' . $url->getSegment(2);+ $git_branch = $url->getSegment(4) ?? 'main';++ if ($this->type === 'project') {+ $project = Project::create([+ 'name' => generateRandomName(),+ 'team_id' => session('currentTeam')->id,+ ]);+ $environment = $project->environments->first();+ } else {+ $project = Project::where('uuid', $this->parameters['project_uuid'])->firstOrFail();+ $environment = $project->environments->where('name', $this->parameters['environment_name'])->firstOrFail();+ }+ $application_init = [+ 'name' => generateRandomName(),+ 'git_repository' => $git_repository,+ 'git_branch' => $git_branch,+ 'git_full_url' => "git@$git_host:$git_repository.git",+ 'build_pack' => 'nixpacks',+ 'ports_exposes' => $this->port,+ 'publish_directory' => $this->publish_directory,+ 'environment_id' => $environment->id,+ 'destination_id' => $this->chosenDestination->id,+ 'destination_type' => $this->chosenDestination->getMorphClass(),+ 'private_key_id' => $this->private_key_id,+ ];+ $application = Application::create($application_init);+ $application->settings->is_static = $this->is_static;+ $application->settings->save();++ return redirect()->route('project.application.configuration', [+ 'project_uuid' => $project->uuid,+ 'environment_name' => $environment->name,+ 'application_uuid' => $application->uuid,+ ]);+ }+}diff --git a/app/Http/Livewire/Project/New/PublicGitRepository.php b/app/Http/Livewire/Project/New/PublicGitRepository.phpindex 22a0bca964..a8ce83b71b 100644--- a/app/Http/Livewire/Project/New/PublicGitRepository.php+++ b/app/Http/Livewire/Project/New/PublicGitRepository.php@@ -14,8 +14,8 @@ use Spatie\Url\Url;class PublicGitRepository extends Component{- public string $public_repository_url;- public int $port;+ public string $repository_url;+ public int $port = 3000;public string $type;public $parameters;@@ -31,7 +31,7 @@ class PublicGitRepository extends Componentpublic null|string $publish_directory = null;protected $rules = [- 'public_repository_url' => 'required|url',+ 'repository_url' => 'required|url','port' => 'required|numeric','is_static' => 'required|boolean','publish_directory' => 'nullable|string',@@ -39,7 +39,7 @@ class PublicGitRepository extends Componentpublic function mount(){if (config('app.env') === 'local') {- $this->public_repository_url = 'https://github.com/coollabsio/coolify-examples/tree/nodejs-fastify';+ $this->repository_url = 'https://github.com/coollabsio/coolify-examples/tree/nodejs-fastify';$this->port = 3000;}$this->parameters = getParameters();@@ -73,7 +73,7 @@ class PublicGitRepository extends Componentpublic function submit(){$this->validate();- $url = Url::fromString($this->public_repository_url);+ $url = Url::fromString($this->repository_url);$git_host = $url->getHost();$git_repository = $url->getSegment(1) . '/' . $url->getSegment(2);$git_branch = $url->getSegment(4) ?? 'main';diff --git a/app/Jobs/DeployApplicationJob.php b/app/Jobs/DeployApplicationJob.phpindex f4a9f27d93..9d0ce017cd 100644--- a/app/Jobs/DeployApplicationJob.php+++ b/app/Jobs/DeployApplicationJob.php@@ -107,7 +107,10 @@ class DeployApplicationJob implements ShouldQueue{try {$coolify_instance_settings = InstanceSettings::find(0);- $this->source = $this->application->source->getMorphClass()::where('id', $this->application->source->id)->first();+ $deployment_type = $this->application->deploymentType();+ if ($this->application->deploymentType() === 'source') {+ $this->source = $this->application->source->getMorphClass()::where('id', $this->application->source->id)->first();+ }// Get Wildcard Domain$project_wildcard_domain = data_get($this->application, 'environment.project.settings.wildcard_domain');@@ -443,29 +446,19 @@ COPY --from={$this->application->uuid}:{$this->git_commit}-build /app/{$this->ap}private function gitImport(){- $source_html_url = data_get($this->application, 'source.html_url');- $url = parse_url(filter_var($source_html_url, FILTER_SANITIZE_URL));- $source_html_url_host = $url['host'];- $source_html_url_scheme = $url['scheme'];-$git_clone_command = "git clone -q -b {$this->application->git_branch}";- if ($this->source->getMorphClass() == 'App\Models\GithubApp') {- if ($this->source->is_public) {- $git_clone_command = "{$git_clone_command} {$this->source->html_url}/{$this->application->git_repository} {$this->workdir}";- $git_clone_command = $this->setGitImportSettings($git_clone_command);- return [- $this->execute_in_builder($git_clone_command)- ];- } else {- if (!$this->source->app_id) {- $private_key = base64_encode($this->source->privateKey->private_key);- $git_clone_command = "GIT_SSH_COMMAND=\"ssh -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /root/.ssh/id_rsa\" {$git_clone_command} git@$source_html_url_host:{$this->application->git_repository}.git {$this->workdir}";+ if ($this->application->deploymentType() === 'source') {+ $source_html_url = data_get($this->application, 'source.html_url');+ $url = parse_url(filter_var($source_html_url, FILTER_SANITIZE_URL));+ $source_html_url_host = $url['host'];+ $source_html_url_scheme = $url['scheme'];++ if ($this->source->getMorphClass() == 'App\Models\GithubApp') {+ if ($this->source->is_public) {+ $git_clone_command = "{$git_clone_command} {$this->source->html_url}/{$this->application->git_repository} {$this->workdir}";$git_clone_command = $this->setGitImportSettings($git_clone_command);return [- $this->execute_in_builder("mkdir -p /root/.ssh"),- $this->execute_in_builder("echo '{$private_key}' | base64 -d > /root/.ssh/id_rsa"),- $this->execute_in_builder("chmod 600 /root/.ssh/id_rsa"),$this->execute_in_builder($git_clone_command)];} else {@@ -476,6 +469,17 @@ COPY --from={$this->application->uuid}:{$this->git_commit}-build /app/{$this->ap}}}+ if ($this->application->deploymentType() === 'deploy_key') {+ $private_key = base64_encode($this->application->private_key->private_key);+ $git_clone_command = "GIT_SSH_COMMAND=\"ssh -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /root/.ssh/id_rsa\" {$git_clone_command} {$this->application->git_full_url} {$this->workdir}";+ $git_clone_command = $this->setGitImportSettings($git_clone_command);+ return [+ $this->execute_in_builder("mkdir -p /root/.ssh"),+ $this->execute_in_builder("echo '{$private_key}' | base64 -d > /root/.ssh/id_rsa"),+ $this->execute_in_builder("chmod 600 /root/.ssh/id_rsa"),+ $this->execute_in_builder($git_clone_command)+ ];+ }}private function nixpacks_build_cmd(){diff --git a/app/Models/Application.php b/app/Models/Application.phpindex c1a0576edb..2da683512d 100644--- a/app/Models/Application.php+++ b/app/Models/Application.php@@ -28,6 +28,7 @@ class Application extends BaseModel'description','git_repository','git_branch',+ 'git_full_url','build_pack','environment_id','destination_id',@@ -37,6 +38,7 @@ class Application extends BaseModel'ports_mappings','ports_exposes','publish_directory',+ 'private_key_id'];public function publishDirectory(): Attribute{@@ -113,6 +115,10 @@ class Application extends BaseModel{return $this->hasMany(EnvironmentVariable::class)->where('key', 'like', 'NIXPACKS_%');}+ public function private_key()+ {+ return $this->belongsTo(PrivateKey::class);+ }public function environment(){return $this->belongsTo(Environment::class);@@ -149,4 +155,14 @@ class Application extends BaseModel}return false;}+ public function deploymentType()+ {+ if (data_get($this, 'source')) {+ return 'source';+ }+ if (data_get($this, 'private_key_id')) {+ return 'deploy_key';+ }+ throw new \Exception('No deployment type found');+ }}diff --git a/database/migrations/2023_03_27_081716_create_applications_table.php b/database/migrations/2023_03_27_081716_create_applications_table.phpindex 3e5e4ecb09..f79dd6f4e8 100644--- a/database/migrations/2023_03_27_081716_create_applications_table.php+++ b/database/migrations/2023_03_27_081716_create_applications_table.php@@ -23,6 +23,7 @@ return new class extends Migration$table->string('git_repository');$table->string('git_branch');$table->string('git_commit_sha')->default('HEAD');+ $table->string('git_full_url')->nullable();$table->string('docker_registry_image_name')->nullable();$table->string('docker_registry_image_tag')->nullable();@@ -55,8 +56,9 @@ return new class extends Migration$table->string('status')->default('exited');$table->nullableMorphs('destination');- $table->morphs('source');+ $table->nullableMorphs('source');+ $table->foreignId('private_key_id')->nullable();$table->foreignId('environment_id');$table->timestamps();});diff --git a/resources/views/livewire/project/new/github-private-repository-deploy-key.blade.php b/resources/views/livewire/project/new/github-private-repository-deploy-key.blade.phpnew file mode 100644index 0000000000..c2a795f053--- /dev/null+++ b/resources/views/livewire/project/new/github-private-repository-deploy-key.blade.php@@ -0,0 +1,84 @@+<div>+ @if ($servers->count() > 0)+ <h1>Choose a server</h1>+ @endif+ @forelse ($servers as $server)+ @if ($chosenServer && $chosenServer['id'] === $server->id)+ <x-inputs.button class="bg-blue-500" wire:click="chooseServer({{ $server }})">{{ $server->name }}+ </x-inputs.button>+ @else+ <x-inputs.button wire:click="chooseServer({{ $server }})">{{ $server->name }}</x-inputs.button>+ @endif+ @empty+ No servers found.+ <p>Did you forget to add a destination on the server?</p>+ @endforelse++ @isset($chosenServer)+ @if ($standalone_docker->count() > 0 || $swarm_docker->count() > 0)+ <h1>Choose a destination</h1>+ <div>+ @foreach ($standalone_docker as $standalone)+ @if ($chosenDestination?->uuid == $standalone->uuid)+ <x-inputs.button class="bg-blue-500"+ wire:click="setDestination('{{ $standalone->uuid }}','StandaloneDocker')">+ {{ $standalone->network }}</x-inputs.button>+ @else+ <x-inputs.button wire:click="setDestination('{{ $standalone->uuid }}','StandaloneDocker')">+ {{ $standalone->network }}</x-inputs.button>+ @endif+ @endforeach+ @foreach ($swarm_docker as $standalone)+ @if ($chosenDestination?->uuid == $standalone->uuid)+ <x-inputs.button class="bg-blue-500"+ wire:click="setDestination('{{ $standalone->uuid }}','SwarmDocker')">+ {{ $standalone->network }}</x-inputs.button>+ @else+ <x-inputs.button wire:click="setDestination('{{ $standalone->uuid }}','SwarmDocker')">+ {{ $standalone->uuid }}</x-inputs.button>+ @endif+ @endforeach+ </div>+ <div>+ <a href="{{ route('destination.new', ['server_id' => $chosenServer['id']]) }}">Add+ a new+ destination</a>+ </div>+ @else+ <h1>No destinations found on this server.</h1>+ <a href="{{ route('destination.new', ['server_id' => $chosenServer['id']]) }}">Add+ a+ destination</a>+ @endif++ @endisset+ @isset($chosenDestination)+ <h1>Choose a repository</h1>+ <form wire:submit.prevent='submit'>+ <div class="flex items-end gap-2 pb-2">+ <x-inputs.input class="w-96" id="repository_url" label="Repository URL" />+ @if ($is_static)+ <x-inputs.input id="publish_directory" label="Publish Directory" />+ @else+ <x-inputs.input type="number" id="port" label="Port" :readonly="$is_static" />+ @endif+ <x-inputs.input instantSave type="checkbox" id="is_static" label="Static Site?" />+ </div>+ <x-inputs.button type="submit">+ Submit+ </x-inputs.button>+ </form>+ <div>+ <h1>Select a private key</h1>+ @foreach ($private_keys as $key)+ @if ($private_key_id == $key->id)+ <x-inputs.button class="bg-blue-500" wire:click.defer="setPrivateKey('{{ $key->id }}')">+ {{ $key->name }}</x-inputs.button>+ @else+ <x-inputs.button wire:click.defer="setPrivateKey('{{ $key->id }}')">{{ $key->name }}+ </x-inputs.button>+ @endif+ @endforeach+ </div>+ @endisset+</div>diff --git a/resources/views/livewire/project/new/public-git-repository.blade.php b/resources/views/livewire/project/new/public-git-repository.blade.phpindex bd9cb6bcd9..1d0377dfb3 100644--- a/resources/views/livewire/project/new/public-git-repository.blade.php+++ b/resources/views/livewire/project/new/public-git-repository.blade.php@@ -56,7 +56,7 @@@isset($chosenDestination)<h1>Choose a repository</h1><form class="flex flex-col gap-2 w-96" wire:submit.prevent='submit'>- <x-inputs.input class="w-96" id="public_repository_url" label="Repository URL" />+ <x-inputs.input class="w-96" id="repository_url" label="Repository URL" /><x-inputs.input instantSave type="checkbox" id="is_static" label="Static Site?" />@if ($is_static)<x-inputs.input id="publish_directory" label="Publish Directory" />diff --git a/resources/views/project/new.blade.php b/resources/views/project/new.blade.phpindex 14ab4b1663..339be40266 100644--- a/resources/views/project/new.blade.php+++ b/resources/views/project/new.blade.php@@ -7,7 +7,10 @@<div x-data="{ activeTab: 'choose' }"><div class="flex flex-col w-64 gap-2 mb-10"><x-inputs.button @click.prevent="activeTab = 'public-repo'">Public Repository</x-inputs.button>- <x-inputs.button @click.prevent="activeTab = 'github-private-repo'">Private Repository (GitHub App)+ <x-inputs.button @click.prevent="activeTab = 'github-private-repo'">Private Repository (with GitHub App)+ </x-inputs.button>+ <x-inputs.button @click.prevent="activeTab = 'github-private-repo-deploy-key'">Private Repository (with+ Deploy Key)</x-inputs.button>@if ($type === 'project')<livewire:project.new.empty-project />@@ -20,5 +23,8 @@<div x-cloak x-show="activeTab === 'github-private-repo'"><livewire:project.new.github-private-repository :type="$type" /></div>+ <div x-cloak x-show="activeTab === 'github-private-repo-deploy-key'">+ <livewire:project.new.github-private-repository-deploy-key :type="$type" />+ </div></div></x-layout>