feat: deploy private repo with ssh key

This commit is contained in:
Andras Bacsai 2023-10-06 14:39:30 +02:00
parent e443fc394a
commit 2f4d7c0e43
4 changed files with 53 additions and 36 deletions

View File

@ -11,6 +11,7 @@ use App\Models\StandaloneDocker;
use App\Models\SwarmDocker; use App\Models\SwarmDocker;
use Livewire\Component; use Livewire\Component;
use Spatie\Url\Url; use Spatie\Url\Url;
use Illuminate\Support\Str;
class GithubPrivateRepositoryDeployKey extends Component class GithubPrivateRepositoryDeployKey extends Component
{ {
@ -29,7 +30,7 @@ class GithubPrivateRepositoryDeployKey extends Component
public string $repository_url; public string $repository_url;
public string $branch; public string $branch;
protected $rules = [ protected $rules = [
'repository_url' => 'required|url', 'repository_url' => 'required',
'branch' => 'required|string', 'branch' => 'required|string',
'port' => 'required|numeric', 'port' => 'required|numeric',
'is_static' => 'required|boolean', 'is_static' => 'required|boolean',
@ -43,8 +44,8 @@ class GithubPrivateRepositoryDeployKey extends Component
'publish_directory' => 'Publish directory', 'publish_directory' => 'Publish directory',
]; ];
private object $repository_url_parsed; private object $repository_url_parsed;
private GithubApp|GitlabApp|null $git_source = null; private GithubApp|GitlabApp|string $git_source = 'other';
private string $git_host; private ?string $git_host = null;
private string $git_repository; private string $git_repository;
public function mount() public function mount()
@ -92,21 +93,38 @@ class GithubPrivateRepositoryDeployKey extends Component
$project = Project::where('uuid', $this->parameters['project_uuid'])->first(); $project = Project::where('uuid', $this->parameters['project_uuid'])->first();
$environment = $project->load(['environments'])->environments->where('name', $this->parameters['environment_name'])->first(); $environment = $project->load(['environments'])->environments->where('name', $this->parameters['environment_name'])->first();
$application_init = [ if ($this->git_source === 'other') {
'name' => generate_random_name(), $application_init = [
'git_repository' => $this->git_repository, 'name' => generate_random_name(),
'git_branch' => $this->branch, 'git_repository' => $this->git_repository,
'git_full_url' => "git@$this->git_host:$this->git_repository.git", 'git_branch' => $this->branch,
'build_pack' => 'nixpacks', 'git_full_url' => $this->git_repository,
'ports_exposes' => $this->port, 'build_pack' => 'nixpacks',
'publish_directory' => $this->publish_directory, 'ports_exposes' => $this->port,
'environment_id' => $environment->id, 'publish_directory' => $this->publish_directory,
'destination_id' => $destination->id, 'environment_id' => $environment->id,
'destination_type' => $destination_class, 'destination_id' => $destination->id,
'private_key_id' => $this->private_key_id, 'destination_type' => $destination_class,
'source_id' => $this->git_source->id, 'private_key_id' => $this->private_key_id,
'source_type' => $this->git_source->getMorphClass() ];
]; } 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 = Application::create($application_init);
$application->settings->is_static = $this->is_static; $application->settings->is_static = $this->is_static;
$application->settings->save(); $application->settings->save();
@ -128,16 +146,18 @@ class GithubPrivateRepositoryDeployKey extends Component
private function get_git_source() private function get_git_source()
{ {
$this->repository_url_parsed = Url::fromString($this->repository_url); if (Str::of($this->repository_url)->startsWith('http')) {
$this->git_host = $this->repository_url_parsed->getHost(); $this->repository_url_parsed = Url::fromString($this->repository_url);
$this->git_repository = $this->repository_url_parsed->getSegment(1) . '/' . $this->repository_url_parsed->getSegment(2); $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') { if ($this->git_host == 'github.com') {
$this->git_source = GithubApp::where('name', 'Public GitHub')->first(); $this->git_source = GithubApp::where('name', 'Public GitHub')->first();
} elseif ($this->git_host == 'gitlab.com') { return;
$this->git_source = GitlabApp::where('name', 'Public GitLab')->first();
} elseif ($this->git_host == 'bitbucket.org') {
// Not supported yet
} }
$this->git_source = 'other';
} }
} }

View File

@ -165,12 +165,12 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted
] ]
); );
} }
// $this->execute_remote_command( $this->execute_remote_command(
// [ [
// "docker rm -f {$this->deployment_uuid} >/dev/null 2>&1", "docker rm -f {$this->deployment_uuid} >/dev/null 2>&1",
// "hidden" => true, "hidden" => true,
// ] ]
// ); );
} }
} }
private function deploy_docker_compose() private function deploy_docker_compose()
@ -428,7 +428,6 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted
$pr_branch_name = "pr-{$this->pull_request_id}-coolify"; $pr_branch_name = "pr-{$this->pull_request_id}-coolify";
} }
if ($this->application->deploymentType() === 'source') { if ($this->application->deploymentType() === 'source') {
$source_html_url = data_get($this->application, 'source.html_url'); $source_html_url = data_get($this->application, 'source.html_url');
$url = parse_url(filter_var($source_html_url, FILTER_SANITIZE_URL)); $url = parse_url(filter_var($source_html_url, FILTER_SANITIZE_URL));

View File

@ -243,9 +243,7 @@ class Application extends BaseModel
if ($this->dockerfile) { if ($this->dockerfile) {
return false; return false;
} }
if (is_null($this->source)) {
return false;
}
return true; return true;
} }
public function isHealthcheckDisabled(): bool public function isHealthcheckDisabled(): bool

View File

@ -23,7 +23,7 @@
<div class="box group" wire:click="setType('private-gh-app')"> <div class="box group" wire:click="setType('private-gh-app')">
<div class="flex flex-col mx-6"> <div class="flex flex-col mx-6">
<div class="font-bold text-white group-hover:text-white"> <div class="font-bold text-white group-hover:text-white">
Private Repository Private Repository (with GitHub App)
</div> </div>
<div class="text-xs group-hover:text-white"> <div class="text-xs group-hover:text-white">
You can deploy public & private repositories through your GitHub Apps. You can deploy public & private repositories through your GitHub Apps.