41 lines
1.6 KiB
PHP
41 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace Modules\CloudDevOps\Services;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
use Modules\CloudDevOps\Models\DeploymentEnvironment;
|
|
use Modules\CloudDevOps\Models\DeploymentPipeline;
|
|
use Modules\CloudDevOps\Models\DeploymentRelease;
|
|
|
|
final class DeploymentManager
|
|
{
|
|
public function createRelease(DeploymentEnvironment $environment, DeploymentPipeline $pipeline, array $attributes = []): DeploymentRelease
|
|
{
|
|
return DB::transaction(function () use ($environment, $pipeline, $attributes): DeploymentRelease {
|
|
$release = $environment->releases()->create([
|
|
'pipeline_id' => $pipeline->getKey(),
|
|
'version' => $attributes['version'] ?? now()->format('YmdHis').'-'.Str::lower(Str::random(6)),
|
|
'commit_hash' => $attributes['commit_hash'] ?? null,
|
|
'status' => $pipeline->requires_approval ? 'awaiting_approval' : 'pending',
|
|
'requested_by' => $attributes['requested_by'] ?? null,
|
|
'metadata' => $attributes['metadata'] ?? [],
|
|
]);
|
|
|
|
foreach ($pipeline->steps as $position => $step) {
|
|
$release->steps()->create(['name' => $step['name'] ?? 'Step '.($position + 1), 'position' => $position]);
|
|
}
|
|
|
|
return $release->load('steps');
|
|
});
|
|
}
|
|
|
|
public function execute(DeploymentRelease $release): DeploymentRelease
|
|
{
|
|
abort_unless(config('clouddevops.allow_execution'), 403, 'Deployment execution is disabled.');
|
|
$release->update(['status' => 'running', 'started_at' => now()]);
|
|
|
|
return $release->refresh();
|
|
}
|
|
}
|