Implement Keystone environment deployments
This commit is contained in:
51
app/Actions/Applications/VerifyRepositoryAccess.php
Normal file
51
app/Actions/Applications/VerifyRepositoryAccess.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Applications;
|
||||
|
||||
use App\Models\Application;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\Process;
|
||||
use RuntimeException;
|
||||
|
||||
class VerifyRepositoryAccess
|
||||
{
|
||||
public function execute(Application $application): bool
|
||||
{
|
||||
if (! $application->deploy_key_private) {
|
||||
throw new RuntimeException('Application does not have a deploy key.');
|
||||
}
|
||||
|
||||
$directory = storage_path('app/private/operations/repository-access-'.$application->id.'-'.str()->random(8));
|
||||
$keyPath = $directory.'/deploy_key';
|
||||
|
||||
File::ensureDirectoryExists($directory, 0700);
|
||||
File::put($keyPath, $application->deploy_key_private);
|
||||
File::chmod($keyPath, 0600);
|
||||
|
||||
try {
|
||||
$result = Process::path($directory)
|
||||
->env([
|
||||
'GIT_SSH_COMMAND' => 'ssh -i '.$keyPath.' -o IdentitiesOnly=yes -o StrictHostKeyChecking=no',
|
||||
])
|
||||
->run([
|
||||
'git',
|
||||
'ls-remote',
|
||||
'--heads',
|
||||
$application->repository_url,
|
||||
$application->default_branch,
|
||||
]);
|
||||
|
||||
if ($result->successful()) {
|
||||
$application->forceFill([
|
||||
'deploy_key_installed_at' => now(),
|
||||
])->save();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
} finally {
|
||||
rescue(fn () => File::deleteDirectory($directory), report: false);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user