From 2f4d7c0e430fea4ee6eaea580a986d71fb61dbd5 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 6 Oct 2023 14:39:30 +0200 Subject: [PATCH] feat: deploy private repo with ssh key --- .../New/GithubPrivateRepositoryDeployKey.php | 70 ++++++++++++------- app/Jobs/ApplicationDeploymentJob.php | 13 ++-- app/Models/Application.php | 4 +- .../livewire/project/new/select.blade.php | 2 +- 4 files changed, 53 insertions(+), 36 deletions(-) diff --git a/app/Http/Livewire/Project/New/GithubPrivateRepositoryDeployKey.php b/app/Http/Livewire/Project/New/GithubPrivateRepositoryDeployKey.php index cf8f56642..0397b822f 100644 --- a/app/Http/Livewire/Project/New/GithubPrivateRepositoryDeployKey.php +++ b/app/Http/Livewire/Project/New/GithubPrivateRepositoryDeployKey.php @@ -11,6 +11,7 @@ use App\Models\SwarmDocker; use Livewire\Component; use Spatie\Url\Url; +use Illuminate\Support\Str; class GithubPrivateRepositoryDeployKey extends Component { @@ -29,7 +30,7 @@ class GithubPrivateRepositoryDeployKey extends Component public string $repository_url; public string $branch; protected $rules = [ - 'repository_url' => 'required|url', + 'repository_url' => 'required', 'branch' => 'required|string', 'port' => 'required|numeric', 'is_static' => 'required|boolean', @@ -43,8 +44,8 @@ class GithubPrivateRepositoryDeployKey extends Component 'publish_directory' => 'Publish directory', ]; private object $repository_url_parsed; - private GithubApp|GitlabApp|null $git_source = null; - private string $git_host; + private GithubApp|GitlabApp|string $git_source = 'other'; + private ?string $git_host = null; private string $git_repository; public function mount() @@ -92,21 +93,38 @@ public function submit() $project = Project::where('uuid', $this->parameters['project_uuid'])->first(); $environment = $project->load(['environments'])->environments->where('name', $this->parameters['environment_name'])->first(); - $application_init = [ - 'name' => generate_random_name(), - 'git_repository' => $this->git_repository, - 'git_branch' => $this->branch, - 'git_full_url' => "git@$this->git_host:$this->git_repository.git", - 'build_pack' => 'nixpacks', - 'ports_exposes' => $this->port, - 'publish_directory' => $this->publish_directory, - 'environment_id' => $environment->id, - 'destination_id' => $destination->id, - 'destination_type' => $destination_class, - 'private_key_id' => $this->private_key_id, - 'source_id' => $this->git_source->id, - 'source_type' => $this->git_source->getMorphClass() - ]; + if ($this->git_source === 'other') { + $application_init = [ + 'name' => generate_random_name(), + 'git_repository' => $this->git_repository, + 'git_branch' => $this->branch, + 'git_full_url' => $this->git_repository, + 'build_pack' => 'nixpacks', + 'ports_exposes' => $this->port, + 'publish_directory' => $this->publish_directory, + 'environment_id' => $environment->id, + 'destination_id' => $destination->id, + 'destination_type' => $destination_class, + 'private_key_id' => $this->private_key_id, + ]; + } else { + $application_init = [ + 'name' => generate_random_name(), + 'git_repository' => $this->git_repository, + 'git_branch' => $this->branch, + 'git_full_url' => "git@$this->git_host:$this->git_repository.git", + 'build_pack' => 'nixpacks', + 'ports_exposes' => $this->port, + 'publish_directory' => $this->publish_directory, + 'environment_id' => $environment->id, + 'destination_id' => $destination->id, + 'destination_type' => $destination_class, + 'private_key_id' => $this->private_key_id, + 'source_id' => $this->git_source->id, + 'source_type' => $this->git_source->getMorphClass() + ]; + } + $application = Application::create($application_init); $application->settings->is_static = $this->is_static; $application->settings->save(); @@ -128,16 +146,18 @@ public function submit() private function get_git_source() { - $this->repository_url_parsed = Url::fromString($this->repository_url); - $this->git_host = $this->repository_url_parsed->getHost(); - $this->git_repository = $this->repository_url_parsed->getSegment(1) . '/' . $this->repository_url_parsed->getSegment(2); + if (Str::of($this->repository_url)->startsWith('http')) { + $this->repository_url_parsed = Url::fromString($this->repository_url); + $this->git_host = $this->repository_url_parsed->getHost(); + $this->git_repository = $this->repository_url_parsed->getSegment(1) . '/' . $this->repository_url_parsed->getSegment(2); + } else { + $this->git_repository = $this->repository_url; + } if ($this->git_host == 'github.com') { $this->git_source = GithubApp::where('name', 'Public GitHub')->first(); - } elseif ($this->git_host == 'gitlab.com') { - $this->git_source = GitlabApp::where('name', 'Public GitLab')->first(); - } elseif ($this->git_host == 'bitbucket.org') { - // Not supported yet + return; } + $this->git_source = 'other'; } } diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index 9988c9728..fc0387357 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -165,12 +165,12 @@ public function handle(): void ] ); } - // $this->execute_remote_command( - // [ - // "docker rm -f {$this->deployment_uuid} >/dev/null 2>&1", - // "hidden" => true, - // ] - // ); + $this->execute_remote_command( + [ + "docker rm -f {$this->deployment_uuid} >/dev/null 2>&1", + "hidden" => true, + ] + ); } } private function deploy_docker_compose() @@ -428,7 +428,6 @@ private function importing_git_repository() $pr_branch_name = "pr-{$this->pull_request_id}-coolify"; } - 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)); diff --git a/app/Models/Application.php b/app/Models/Application.php index 3d23a5709..3f5eb54d5 100644 --- a/app/Models/Application.php +++ b/app/Models/Application.php @@ -243,9 +243,7 @@ public function git_based(): bool if ($this->dockerfile) { return false; } - if (is_null($this->source)) { - return false; - } + return true; } public function isHealthcheckDisabled(): bool diff --git a/resources/views/livewire/project/new/select.blade.php b/resources/views/livewire/project/new/select.blade.php index 8e4b9d54c..3bda859b3 100644 --- a/resources/views/livewire/project/new/select.blade.php +++ b/resources/views/livewire/project/new/select.blade.php @@ -23,7 +23,7 @@
- Private Repository + Private Repository (with GitHub App)
You can deploy public & private repositories through your GitHub Apps.