Deployment-Services implementiert: Basisklasse, Git-spezifische Implementierung und Resolver hinzugefügt

This commit is contained in:
2026-03-27 14:25:30 +01:00
parent 804bcf9e7a
commit 90d61cdfb8
5 changed files with 386 additions and 0 deletions

View File

@@ -0,0 +1,138 @@
<?php
/*
* GitService.php 2026-03-25 thomas
*
* Copyright (c) 2026 Thomas Schneider <thomas@inter-mundos.de>
* Alle Rechte vorbehalten.
*/
namespace App\Service\Git;
use App\Traits\LoggerTrait;
use League\Uri\Uri;
use Symfony\Component\Process\Process;
class GitService
{
use LoggerTrait;
protected string $workingDir;
public function __construct(
){
$this->workingDir = sys_get_temp_dir();
}
public function setWorkingDir(string $dir): self
{
$this->workingDir = $dir;
return $this;
}
public function isCloned(string $dir = '.git'): bool
{
return is_dir($this->workingDir . DIRECTORY_SEPARATOR . $dir);
}
public function cloneRepo(string $repoUrl, string $targetDir = '', array $options = []): void
{
if(!empty($options['auth_basic']))
{
$repoUrl = Uri::new($repoUrl)
->withUsername($options['auth_basic']['username'] ?? '')
->withPassword($options['auth_basic']['password'] ?? '')
->toString()
;
unset ($options['auth_basic']);
}
new Process(
command: ['git', 'clone', ...$options, $repoUrl, $targetDir],
cwd: $this->workingDir,
)->mustRun();
}
public function checkoutRepo(string $dir, string $branch = 'master'): void
{
$process = new Process(['git', 'checkout', '-f', $branch]);
$process->setWorkingDirectory($this->workingDir . DIRECTORY_SEPARATOR . $dir);
$process->mustRun();
}
public function fetchRepo(string $dir, array $options = []): void
{
$process = Process::fromShellCommandline('git --all --tags');
$process->setWorkingDirectory($this->workingDir . DIRECTORY_SEPARATOR . $dir);
$process->mustRun();
}
public function updateRef(string $dir, array $options = []): void
{
Process::fromShellCommandline(
command: 'git update-ref',
cwd: $this->workingDir . DIRECTORY_SEPARATOR . $dir
)->mustRun();
}
public function pullRepo(string $targetDir, array $options = []): void
{
Process::fromShellCommandline(
command: 'git pull --all --tags --force',
cwd: $this->workingDir . DIRECTORY_SEPARATOR . $targetDir
)->mustRun();
}
public function getRepoDefaultBranch(string $dir): string
{
$process = Process::fromShellCommandline('git symbolic-ref --short HEAD');
$process->setWorkingDirectory($this->workingDir . DIRECTORY_SEPARATOR . $dir);
$process->mustRun();
return trim($process->getOutput()) ?: 'master';
}
public function getCurrentRelease(string $dir): string
{
$process = Process::fromShellCommandline('git describe --tags');
$process->setWorkingDirectory($this->workingDir . DIRECTORY_SEPARATOR . $dir);
$process->mustRun();
return trim($process->getOutput());
}
public function getLatestRelease(string $dir): string
{
$process = Process::fromShellCommandline('git tag --sort=committerdate --list "v[0-9]*" "[0-9]*.[0-9]*.[0-9]*" | tail -1');
$process->setWorkingDirectory($this->workingDir . DIRECTORY_SEPARATOR . $dir);
$process->mustRun();
return trim($process->getOutput());
}
public function checkoutLatestRelease(string $dir): void
{
$currentRelease = $this->getCurrentRelease($dir);
$latestRelease = $this->getLatestRelease($dir);
// Update abbrechen, wenn Version bereits aktuell
if($currentRelease === $latestRelease)
{
return;
}
// neueste Version herunterladen und auschecken
$this->checkoutRepo($dir, $latestRelease);
}
}