Deployment-Services implementiert: Basisklasse, Git-spezifische Implementierung und Resolver hinzugefügt
This commit is contained in:
110
src/Service/Git/GitDeployment.php
Normal file
110
src/Service/Git/GitDeployment.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
/*
|
||||
* GitDeployment.php 2026-03-25 thomas
|
||||
*
|
||||
* Copyright (c) 2026 Thomas Schneider <thomas@inter-mundos.de>
|
||||
* Alle Rechte vorbehalten.
|
||||
*/
|
||||
|
||||
namespace App\Service\Git;
|
||||
|
||||
use App\Entity\Project;
|
||||
use App\Service\Deployment\DeploymentService;
|
||||
use App\Service\ProjectConfigDirService;
|
||||
use App\Traits\LoggerTrait;
|
||||
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
|
||||
|
||||
#[AutoconfigureTag('app.deployment_service', ['git'])]
|
||||
class GitDeployment extends DeploymentService
|
||||
{
|
||||
use LoggerTrait;
|
||||
|
||||
const string GIT_DIR = 'repo.git';
|
||||
|
||||
|
||||
public function __construct(
|
||||
protected GitService $git, private readonly ProjectConfigDirService $projectConfigDirService,
|
||||
){}
|
||||
|
||||
|
||||
public function supports(): string
|
||||
{
|
||||
return 'git';
|
||||
}
|
||||
|
||||
|
||||
public function init(Project $project): void
|
||||
{
|
||||
$this->git->setWorkingDir($project->projectDir);
|
||||
}
|
||||
|
||||
|
||||
public function handle(Project $project, string $version = '', string $step = ''): void
|
||||
{
|
||||
$this->fetch($project);
|
||||
$this->deploy($project, $version);
|
||||
}
|
||||
|
||||
|
||||
public function fetch(Project $project): void
|
||||
{
|
||||
$this->projectConfigDirService->setDeploymentStep($project->projectDir, 'fetch');
|
||||
$options = [];
|
||||
|
||||
if(!empty($project->deployment['repository_url']) && str_starts_with($project->deployment['repository_url'], 'http'))
|
||||
{
|
||||
$options['auth_basic'] = [
|
||||
'username' => $project->deployment['username'] ?? '',
|
||||
'password' => $project->deployment['password'] ?? '',
|
||||
];
|
||||
}
|
||||
|
||||
if($this->git->isCloned(self::GIT_DIR))
|
||||
{
|
||||
$this->projectConfigDirService->setDeploymentStep($project->projectDir, 'fetch.git.clone');
|
||||
|
||||
$this->git->fetchRepo(
|
||||
dir: self::GIT_DIR,
|
||||
options: $options,
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->projectConfigDirService->setDeploymentStep($project->projectDir, 'fetch.git.fetch');
|
||||
|
||||
$this->git->cloneRepo(
|
||||
repoUrl: $project->deployment['repository_url'],
|
||||
targetDir: self::GIT_DIR,
|
||||
options: [...$options, '--mirror'],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function deploy(Project $project, string $version): void
|
||||
{
|
||||
# remove work tree
|
||||
$this->clearDeploymentFiles($project);
|
||||
|
||||
# create work tree
|
||||
$this->git->cloneRepo(self::GIT_DIR, $this->filesDir);
|
||||
|
||||
// default translates to current state of default branch
|
||||
if($version === 'default')
|
||||
{
|
||||
$version = $this->git->getRepoDefaultBranch(self::GIT_DIR);
|
||||
}
|
||||
|
||||
if(!$version || $version === 'latest')
|
||||
{
|
||||
# check out latest release
|
||||
$this->git->checkoutLatestRelease($this->filesDir);
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->git->checkoutRepo($this->filesDir, $version);
|
||||
}
|
||||
|
||||
parent::deploy($project, $version);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user