From aba47d58a41249264fa193100fa2251ac09e320e Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Wed, 8 Nov 2023 11:30:54 +0100 Subject: [PATCH] Add customRepository property to ApplicationDeploymentJob class Fix weird image names in case of custom git --- app/Jobs/ApplicationDeploymentJob.php | 37 +++++++++++++++------------ 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index 725b8cbc0..798c46b2b 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -78,6 +78,7 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted private string $dockerConfigFileExists = 'NOK'; private int $customPort = 22; + private ?string $customRepository = null; private ?string $fullRepoUrl = null; private ?string $branch = null; @@ -193,7 +194,9 @@ public function handle(): void $this->customPort = $matches[0]; $gitHost = str($this->application->git_repository)->before(':'); $gitRepo = str($this->application->git_repository)->after('/'); - $this->application->git_repository = "$gitHost:$gitRepo"; + $this->customRepository = "$gitHost:$gitRepo"; + } else { + $this->customRepository = $this->application->git_repository; } try { if ($this->restart_only) { @@ -258,7 +261,7 @@ public function handle(): void // executeInDocker($this->deployment_uuid, "echo '$dockercompose_base64' | base64 -d > $this->workdir/docker-compose.yaml") // ], // ); - // $this->build_image_name = Str::lower("{$this->application->git_repository}:build"); + // $this->build_image_name = Str::lower("{$this->customRepository}:build"); // $this->production_image_name = Str::lower("{$this->application->uuid}:latest"); // $this->save_environment_variables(); // $containers = getCurrentApplicationContainerStatus($this->application->destination->server, $this->application->id); @@ -283,7 +286,7 @@ public function handle(): void private function generate_image_names() { if ($this->application->dockerfile) { - $this->build_image_name = Str::lower("{$this->application->git_repository}:build"); + $this->build_image_name = Str::lower("{$this->application->uuid}:build"); $this->production_image_name = Str::lower("{$this->application->uuid}:latest"); } else if ($this->application->build_pack === 'dockerimage') { $this->production_image_name = Str::lower("{$this->dockerImage}:{$this->dockerImageTag}"); @@ -295,7 +298,7 @@ private function generate_image_names() if (strlen($tag) > 128) { $tag = $tag->substr(0, 128); } - $this->build_image_name = Str::lower("{$this->application->git_repository}:{$tag}-build"); + $this->build_image_name = Str::lower("{$this->application->uuid}:{$tag}-build"); $this->production_image_name = Str::lower("{$this->application->uuid}:{$tag}"); } } @@ -303,7 +306,7 @@ private function just_restart() { $this->execute_remote_command( [ - "echo 'Starting deployment of {$this->application->git_repository}:{$this->application->git_branch}.'" + "echo 'Starting deployment of {$this->customRepository}:{$this->application->git_branch}.'" ], ); $this->prepare_builder_image(); @@ -380,7 +383,7 @@ private function deploy_dockerfile_buildpack() } $this->execute_remote_command( [ - "echo 'Starting deployment of {$this->application->git_repository}:{$this->application->git_branch}.'" + "echo 'Starting deployment of {$this->customRepository}:{$this->application->git_branch}.'" ], ); $this->prepare_builder_image(); @@ -399,7 +402,7 @@ private function deploy_nixpacks_buildpack() { $this->execute_remote_command( [ - "echo 'Starting deployment of {$this->application->git_repository}:{$this->application->git_branch}.'" + "echo 'Starting deployment of {$this->customRepository}:{$this->application->git_branch}.'" ], ); $this->prepare_builder_image(); @@ -501,7 +504,7 @@ private function deploy_pull_request() { $this->generate_image_names(); $this->execute_remote_command([ - "echo 'Starting pull request (#{$this->pull_request_id}) deployment of {$this->application->git_repository}:{$this->application->git_branch}.'", + "echo 'Starting pull request (#{$this->pull_request_id}) deployment of {$this->customRepository}:{$this->application->git_branch}.'", ]); $this->prepare_builder_image(); $this->clone_repository(); @@ -599,7 +602,7 @@ private function clone_repository() $importCommands = $this->generate_git_import_commands(); $this->execute_remote_command( [ - "echo -n 'Importing {$this->application->git_repository}:{$this->application->git_branch} (commit sha {$this->application->git_commit_sha}) to {$this->basedir}. '" + "echo -n 'Importing {$this->customRepository}:{$this->application->git_branch} (commit sha {$this->application->git_commit_sha}) to {$this->basedir}. '" ], [ $importCommands, "hidden" => true @@ -624,15 +627,15 @@ private function generate_git_import_commands() if ($this->source->getMorphClass() == 'App\Models\GithubApp') { if ($this->source->is_public) { - $this->fullRepoUrl = "{$this->source->html_url}/{$this->application->git_repository}"; - $git_clone_command = "{$git_clone_command} {$this->source->html_url}/{$this->application->git_repository} {$this->basedir}"; + $this->fullRepoUrl = "{$this->source->html_url}/{$this->customRepository}"; + $git_clone_command = "{$git_clone_command} {$this->source->html_url}/{$this->customRepository} {$this->basedir}"; $git_clone_command = $this->set_git_import_settings($git_clone_command); $commands->push(executeInDocker($this->deployment_uuid, $git_clone_command)); } else { $github_access_token = generate_github_installation_token($this->source); - $commands->push(executeInDocker($this->deployment_uuid, "git clone -q -b {$this->application->git_branch} $source_html_url_scheme://x-access-token:$github_access_token@$source_html_url_host/{$this->application->git_repository}.git {$this->basedir}")); - $this->fullRepoUrl = "$source_html_url_scheme://x-access-token:$github_access_token@$source_html_url_host/{$this->application->git_repository}.git"; + $commands->push(executeInDocker($this->deployment_uuid, "git clone -q -b {$this->application->git_branch} $source_html_url_scheme://x-access-token:$github_access_token@$source_html_url_host/{$this->customRepository}.git {$this->basedir}")); + $this->fullRepoUrl = "$source_html_url_scheme://x-access-token:$github_access_token@$source_html_url_host/{$this->customRepository}.git"; } if ($this->pull_request_id !== 0) { $this->branch = "pull/{$this->pull_request_id}/head:$pr_branch_name"; @@ -642,13 +645,13 @@ private function generate_git_import_commands() } } if ($this->application->deploymentType() === 'deploy_key') { - $this->fullRepoUrl = $this->application->git_repository; + $this->fullRepoUrl = $this->customRepository; $private_key = data_get($this->application, 'private_key.private_key'); if (is_null($private_key)) { throw new Exception('Private key not found. Please add a private key to the application and try again.'); } $private_key = base64_encode($private_key); - $git_clone_command = "GIT_SSH_COMMAND=\"ssh -o ConnectTimeout=30 -p {$this->customPort} -o Port={$this->customPort} -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /root/.ssh/id_rsa\" {$git_clone_command} {$this->application->git_repository} {$this->basedir}"; + $git_clone_command = "GIT_SSH_COMMAND=\"ssh -o ConnectTimeout=30 -p {$this->customPort} -o Port={$this->customPort} -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /root/.ssh/id_rsa\" {$git_clone_command} {$this->customRepository} {$this->basedir}"; $git_clone_command = $this->set_git_import_settings($git_clone_command); $commands = collect([ executeInDocker($this->deployment_uuid, "mkdir -p /root/.ssh"), @@ -659,8 +662,8 @@ private function generate_git_import_commands() return $commands->implode(' && '); } if ($this->application->deploymentType() === 'other') { - $this->fullRepoUrl = $this->application->git_repository; - $git_clone_command = "{$git_clone_command} {$this->application->git_repository} {$this->basedir}"; + $this->fullRepoUrl = $this->customRepository; + $git_clone_command = "{$git_clone_command} {$this->customRepository} {$this->basedir}"; $git_clone_command = $this->set_git_import_settings($git_clone_command); $commands->push(executeInDocker($this->deployment_uuid, $git_clone_command)); return $commands->implode(' && ');