PHP自动化构建与持续交付流水线
PHP自动化构建与持续交付流水线自动化构建和持续交付是现代软件开发的基础设施。今天说说PHP项目的构建、测试、部署流水线的各个阶段。构建阶段包括代码检查、依赖安装、静态分析和单元测试。php// 构建状态追踪class BuildPipeline{private string $buildId;private string $branch;private string $commit;private array $stages [];private string $status pending;private ?string $error null;public function __construct(string $branch, string $commit){$this-buildId date(Ymd) . _ . uniqid();$this-branch $branch;$this-commit $commit;}public function runStage(string $name, callable $stage): bool{echo \n {$name} \n;$startTime microtime(true);try {$stage();$duration round((microtime(true) - $startTime) * 1000);$this-stages[] [name $name, status success, duration $duration];echo 完成 ({$duration}ms)\n;return true;} catch (\Exception $e) {$duration round((microtime(true) - $startTime) * 1000);$this-stages[] [name $name, status failed, duration $duration];$this-error $e-getMessage();$this-status failed;echo 失败: {$e-getMessage()}\n;return false;}}public function execute(array $stages): void{echo 构建 #{$this-buildId} 开始\n;echo 分支: {$this-branch}, 提交: {$this-commit}\n;foreach ($stages as $name $stage) {if (!$this-runStage($name, $stage)) {echo \n构建失败\n;$this-status failed;return;}}$this-status success;echo \n构建成功\n;}public function getSummary(): array{$totalDuration array_sum(array_column($this-stages, duration));return [build_id $this-buildId,branch $this-branch,commit $this-commit,status $this-status,stages $this-stages,total_duration_ms $totalDuration,error $this-error,];}}$build new BuildPipeline(main, abc1234);$build-execute([语法检查 function () {exec(php -l src/index.php 21, $output, $code);if ($code ! 0) throw new \RuntimeException(implode(\n, $output));},依赖安装 function () {exec(composer install --no-dev --no-progress 21, $output, $code);if ($code ! 0) throw new \RuntimeException(Composer安装失败);},单元测试 function () {exec(vendor/bin/phpunit --no-coverage 21, $output, $code);if ($code ! 0) throw new \RuntimeException(单元测试失败);},]);print_r($build-getSummary());?版本号管理和自动发布phpclass VersionManager{private string $versionFile;private array $version;public function __construct(string $versionFile VERSION){$this-versionFile $versionFile;$this-version $this-load();}private function load(): array{if (file_exists($this-versionFile)) {$content trim(file_get_contents($this-versionFile));$parts explode(., $content);return [major (int)($parts[0] ?? 1),minor (int)($parts[1] ?? 0),patch (int)($parts[2] ?? 0),];}return [major 1, minor 0, patch 0];}public function getVersion(): string{return {$this-version[major]}.{$this-version[minor]}.{$this-version[patch]};}public function bumpMajor(): string{$this-version[major];$this-version[minor] 0;$this-version[patch] 0;return $this-save();}public function bumpMinor(): string{$this-version[minor];$this-version[patch] 0;return $this-save();}public function bumpPatch(): string{$this-version[patch];return $this-save();}private function save(): string{$version $this-getVersion();file_put_contents($this-versionFile, $version);return $version;}public function generateChangelog(array $changes): string{$date date(Y-m-d);$version $this-getVersion();$log ## [{$version}] - {$date}\n\n;foreach ($changes as $type $items) {if (!empty($items)) {$log . ### {$type}\n;foreach ($items as $item) {$log . - {$item}\n;}$log . \n;}}$changelogFile CHANGELOG.md;$existing file_exists($changelogFile) ? file_get_contents($changelogFile) : ;file_put_contents($changelogFile, $log . $existing);return $version;}}$version new VersionManager();echo 当前版本: {$version-getVersion()}\n;$changes [新增 [用户注册功能, 订单导出功能],修复 [修复了分页bug, 修复了登录超时问题],];$newVersion $version-generateChangelog($changes);echo 新版本: {$newVersion}\n;// 自动打Tag$tag v{$newVersion};exec(git tag -a {$tag} -m Release {$tag});exec(git push origin {$tag});echo Git Tag: {$tag}\n;?构建产物管理和自动化部署脚本phpclass ArtifactManager{private string $artifactDir;public function __construct(string $artifactDir /var/www/artifacts){$this-artifactDir rtrim($artifactDir, /);if (!is_dir($this-artifactDir)) mkdir($this-artifactDir, 0755, true);}public function build(string $sourceDir, string $version): string{$artifactName app-{$version}.tar.gz;$artifactPath {$this-artifactDir}/{$artifactName};$exclude [--exclude.git, --excludenode_modules, --excludetests];$excludeStr implode( , $exclude);exec(tar -czf {$artifactPath} {$excludeStr} -C {$sourceDir} . 21, $output, $code);if ($code ! 0) {throw new \RuntimeException(构建失败: . implode(\n, $output));}echo 构建产物: {$artifactPath} ( . round(filesize($artifactPath) / 1024 / 1024, 2) . MB)\n;return $artifactPath;}public function deploy(string $artifact, string $targetDir): void{if (!file_exists($artifact)) {throw new \RuntimeException(构建产物不存在: {$artifact});}if (!is_dir($targetDir)) mkdir($targetDir, 0755, true);exec(tar -xzf {$artifact} -C {$targetDir} 21, $output, $code);if ($code ! 0) {throw new \RuntimeException(部署失败: . implode(\n, $output));}$this-setPermissions($targetDir);echo 部署完成: {$targetDir}\n;}private function setPermissions(string $dir): void{exec(chmod -R 755 {$dir});if (is_dir({$dir}/storage)) {exec(chmod -R 775 {$dir}/storage);}}}?自动化构建和持续交付是DevOps实践的核心。自动化流水线确保代码质量减少人工操作加快交付速度。构建、测试、部署的自动化程度越高团队的交付效率也就越高。