From 33342aa27a86f5453148d84f1027e011397d1c59 Mon Sep 17 00:00:00 2001 From: Jonathan Visser Date: Wed, 15 Jan 2025 14:07:14 +0100 Subject: [PATCH] Display appropiate branchname based on CI runner --- .../Task/Common/DefaultsTaskGlobal.php | 13 +++- src/Stdlib/ReleaseInfo.php | 6 +- src/Stdlib/TargetFinder.php | 74 +++++++++++++++++++ 3 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 src/Stdlib/TargetFinder.php diff --git a/src/Deployer/Task/Common/DefaultsTaskGlobal.php b/src/Deployer/Task/Common/DefaultsTaskGlobal.php index a899762..da36b27 100644 --- a/src/Deployer/Task/Common/DefaultsTaskGlobal.php +++ b/src/Deployer/Task/Common/DefaultsTaskGlobal.php @@ -8,6 +8,7 @@ use Hypernode\Deploy\Stdlib\CpuCoreInfo; use Hypernode\Deploy\Stdlib\ReleaseInfo; use Hypernode\DeployConfiguration\Configuration; +use Hypernode\Deploy\Stdlib\TargetFinder; use function Deployer\set; @@ -23,10 +24,16 @@ class DefaultsTaskGlobal extends TaskBase */ private $releaseInfo; - public function __construct(CpuCoreInfo $cpuInfo, ReleaseInfo $releaseInfo) + /** + * @var TargetFinder + */ + private $targetFinder; + + public function __construct(CpuCoreInfo $cpuInfo, ReleaseInfo $releaseInfo, TargetFinder $targetFinder) { $this->cpuInfo = $cpuInfo; $this->releaseInfo = $releaseInfo; + $this->targetFinder = $targetFinder; } public function configure(Configuration $config): void @@ -41,6 +48,10 @@ public function configure(Configuration $config): void return $this->releaseInfo->getMessage(); }); + set('target', function () { + return $this->targetFinder->getTarget(); + }); + set('commit_sha', function () { try { return $this->releaseInfo->getCommitSha(); diff --git a/src/Stdlib/ReleaseInfo.php b/src/Stdlib/ReleaseInfo.php index c819f41..1970d98 100644 --- a/src/Stdlib/ReleaseInfo.php +++ b/src/Stdlib/ReleaseInfo.php @@ -25,8 +25,8 @@ public function getCommitSha(): string public function getMessage(): string { $body = []; - $body[] = parse('Successful deployment to **{{stage}}**'); - $body[] = parse('Branch: `{{branch}}`'); + $body[] = parse('Successful deployment to *{{stage}}*'); + $body[] = parse('Branch: `{{target}}`'); $body[] = parse('User: `{{user}}`'); $body[] = parse('Commit: `{{commit_sha}}`'); @@ -40,7 +40,7 @@ public function getMessage(): string } $body[] = ''; - $body[] = '**Servers:**'; + $body[] = '*Servers:*'; foreach ($this->getServers() as $server) { $body[] = '- ' . $server; } diff --git a/src/Stdlib/TargetFinder.php b/src/Stdlib/TargetFinder.php new file mode 100644 index 0000000..5f19d50 --- /dev/null +++ b/src/Stdlib/TargetFinder.php @@ -0,0 +1,74 @@ +getBranchFromCI(); + if (!empty($branch)) { + return $branch; + } + + return get('branch', 'HEAD'); + } + + private function getBranchFromCI(): ?string + { + // Check GitHub Actions + if ($githubBranch = getenv('GITHUB_HEAD_REF')) { + return $githubBranch; + } + if ($githubBaseRef = getenv('GITHUB_REF')) { + return $this->parseGithubRef($githubBaseRef); + } + + // Check GitLab CI + if ($gitlabBranch = getenv('CI_COMMIT_REF_NAME')) { + return $gitlabBranch; + } + + // Check Bitbucket Pipelines + if ($bitbucketBranch = getenv('BITBUCKET_BRANCH')) { + return $bitbucketBranch; + } + + // Check Azure Pipelines + if ($azureBranch = getenv('BUILD_SOURCEBRANCH')) { + return $this->parseAzureBranch($azureBranch); + } + + return null; + } + + private function parseGithubRef(string $ref): ?string + { + // Extract branch or tag name from refs/heads/ or refs/tags/ + if (preg_match('#refs/heads/(.+)#', $ref, $matches)) { + return $matches[1]; + } + if (preg_match('#refs/tags/(.+)#', $ref, $matches)) { + return $matches[1]; + } + + return null; + } + + private function parseAzureBranch(string $branch): ?string + { + // Extract branch name from refs/heads/ + if (strpos($branch, 'refs/heads/') === 0) { + return substr($branch, strlen('refs/heads/')); + } + + return $branch; + } +}