Files
michaelschiemer/tests/debug/test-deployment-pipeline.php
Michael Schiemer 3b623e7afb feat(Deployment): Integrate Ansible deployment via PHP deployment pipeline
- Create AnsibleDeployStage using framework's Process module for secure command execution
- Integrate AnsibleDeployStage into DeploymentPipelineCommands for production deployments
- Add force_deploy flag support in Ansible playbook to override stale locks
- Use PHP deployment module as orchestrator (php console.php deploy:production)
- Fix ErrorAggregationInitializer to use Environment class instead of $_ENV superglobal

Architecture:
- BuildStage → AnsibleDeployStage → HealthCheckStage for production
- Process module provides timeout, error handling, and output capture
- Ansible playbook supports rollback via rollback-git-based.yml
- Zero-downtime deployments with health checks
2025-10-26 14:08:07 +01:00

139 lines
4.2 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/../../vendor/autoload.php';
use App\Framework\Deployment\Pipeline\Services\DeploymentPipelineService;
use App\Framework\Deployment\Pipeline\Services\PipelineStatusStore;
use App\Framework\Deployment\Pipeline\Services\PipelineHistoryService;
use App\Framework\Deployment\Pipeline\Stages\BuildStage;
use App\Framework\Deployment\Pipeline\Stages\TestStage;
use App\Framework\Deployment\Pipeline\Stages\DeployStage;
use App\Framework\Deployment\Pipeline\Stages\HealthCheckStage;
use App\Framework\Deployment\Pipeline\ValueObjects\DeploymentEnvironment;
use App\Framework\EventBus\DefaultEventBus;
use App\Framework\DI\DefaultContainer;
use App\Framework\Logging\DefaultLogger;
use App\Framework\Logging\Handlers\ConsoleHandler;
echo "=== Testing Deployment Pipeline ===\n\n";
// Setup dependencies
$logger = new DefaultLogger(handlers: [new ConsoleHandler()]);
$container = new DefaultContainer();
$eventBus = new DefaultEventBus(
eventHandlers: [],
container: $container,
logger: $logger
);
$statusStore = new PipelineStatusStore();
$historyService = new PipelineHistoryService();
// Create pipeline stages
$stages = [
new BuildStage(),
new TestStage(),
new DeployStage(),
new HealthCheckStage()
];
// Create pipeline service
$pipelineService = new DeploymentPipelineService(
stages: $stages,
eventBus: $eventBus,
logger: $logger,
statusStore: $statusStore,
historyService: $historyService
);
// Test 1: Execute pipeline for staging environment
echo "Test 1: Execute deployment pipeline for STAGING\n";
echo "------------------------------------------------\n";
try {
$environment = DeploymentEnvironment::STAGING;
$result = $pipelineService->execute($environment);
echo "\nPipeline Result:\n";
echo " Pipeline ID: {$result->pipelineId->value}\n";
echo " Environment: {$result->environment->value}\n";
echo " Status: {$result->status->value}\n";
echo " Total Duration: {$result->totalDuration->toMilliseconds()}ms\n";
echo " Stages Executed: " . count($result->stageResults) . "\n\n";
echo "Stage Results:\n";
foreach ($result->stageResults as $stageResult) {
$statusIcon = $stageResult->isSuccess() ? '✅' : '❌';
echo " {$statusIcon} {$stageResult->stage->value}: {$stageResult->duration->toMilliseconds()}ms\n";
if ($stageResult->output) {
echo " Output: {$stageResult->output}\n";
}
if ($stageResult->error) {
echo " Error: {$stageResult->error}\n";
}
}
echo "\n";
if ($result->isSuccess()) {
echo "✅ Pipeline completed successfully!\n";
} elseif ($result->isRolledBack()) {
echo "⚠️ Pipeline was rolled back due to failure\n";
} else {
echo "❌ Pipeline failed!\n";
}
} catch (\Throwable $e) {
echo "❌ Pipeline execution failed with exception:\n";
echo " {$e->getMessage()}\n";
echo " {$e->getFile()}:{$e->getLine()}\n";
}
echo "\n";
// Test 2: Check status store
echo "Test 2: Check Pipeline Status Store\n";
echo "------------------------------------\n";
try {
if (isset($result)) {
$status = $statusStore->getStatus($result->pipelineId);
echo "Pipeline Status from Store:\n";
echo " Pipeline ID: {$status['pipeline_id']}\n";
echo " Environment: {$status['environment']}\n";
echo " Status: {$status['status']}\n";
echo " Stages:\n";
foreach ($status['stages'] as $stageName => $stageData) {
echo " - {$stageName}: {$stageData['status']}\n";
}
echo "\n";
}
} catch (\Throwable $e) {
echo "⚠️ Status store check failed: {$e->getMessage()}\n\n";
}
// Test 3: Check pipeline history
echo "Test 3: Check Pipeline History\n";
echo "-------------------------------\n";
try {
$history = $historyService->getRecentPipelines(limit: 5);
echo "Recent Pipelines: " . count($history) . "\n";
foreach ($history as $entry) {
echo " - {$entry->pipelineId->value}: {$entry->status->value} ({$entry->environment->value})\n";
}
echo "\n";
} catch (\Throwable $e) {
echo "⚠️ History check failed: {$e->getMessage()}\n\n";
}
echo "=== Test completed ===\n";