Skip to content

Commit 6249c2b

Browse files
authored
Merge pull request #138 from ByteInternet/feature/slack-include-branch-from-ci-envs
Display appropiate branchname based on CI runner
2 parents 794d98d + 33342aa commit 6249c2b

File tree

3 files changed

+89
-4
lines changed

3 files changed

+89
-4
lines changed

src/Deployer/Task/Common/DefaultsTaskGlobal.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Hypernode\Deploy\Stdlib\CpuCoreInfo;
99
use Hypernode\Deploy\Stdlib\ReleaseInfo;
1010
use Hypernode\DeployConfiguration\Configuration;
11+
use Hypernode\Deploy\Stdlib\TargetFinder;
1112

1213
use function Deployer\set;
1314

@@ -23,10 +24,16 @@ class DefaultsTaskGlobal extends TaskBase
2324
*/
2425
private $releaseInfo;
2526

26-
public function __construct(CpuCoreInfo $cpuInfo, ReleaseInfo $releaseInfo)
27+
/**
28+
* @var TargetFinder
29+
*/
30+
private $targetFinder;
31+
32+
public function __construct(CpuCoreInfo $cpuInfo, ReleaseInfo $releaseInfo, TargetFinder $targetFinder)
2733
{
2834
$this->cpuInfo = $cpuInfo;
2935
$this->releaseInfo = $releaseInfo;
36+
$this->targetFinder = $targetFinder;
3037
}
3138

3239
public function configure(Configuration $config): void
@@ -41,6 +48,10 @@ public function configure(Configuration $config): void
4148
return $this->releaseInfo->getMessage();
4249
});
4350

51+
set('target', function () {
52+
return $this->targetFinder->getTarget();
53+
});
54+
4455
set('commit_sha', function () {
4556
try {
4657
return $this->releaseInfo->getCommitSha();

src/Stdlib/ReleaseInfo.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public function getCommitSha(): string
2525
public function getMessage(): string
2626
{
2727
$body = [];
28-
$body[] = parse('Successful deployment to **{{stage}}**');
29-
$body[] = parse('Branch: `{{branch}}`');
28+
$body[] = parse('Successful deployment to *{{stage}}*');
29+
$body[] = parse('Branch: `{{target}}`');
3030
$body[] = parse('User: `{{user}}`');
3131
$body[] = parse('Commit: `{{commit_sha}}`');
3232

@@ -40,7 +40,7 @@ public function getMessage(): string
4040
}
4141

4242
$body[] = '';
43-
$body[] = '**Servers:**';
43+
$body[] = '*Servers:*';
4444
foreach ($this->getServers() as $server) {
4545
$body[] = '- ' . $server;
4646
}

src/Stdlib/TargetFinder.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Hypernode\Deploy\Stdlib;
4+
5+
use function Deployer\get;
6+
7+
class TargetFinder
8+
{
9+
public function getTarget(): string
10+
{
11+
$branch = get('branch', 'HEAD');
12+
if (!empty($branch) && $branch != 'HEAD') {
13+
return $branch;
14+
}
15+
16+
$branch = $this->getBranchFromCI();
17+
if (!empty($branch)) {
18+
return $branch;
19+
}
20+
21+
return get('branch', 'HEAD');
22+
}
23+
24+
private function getBranchFromCI(): ?string
25+
{
26+
// Check GitHub Actions
27+
if ($githubBranch = getenv('GITHUB_HEAD_REF')) {
28+
return $githubBranch;
29+
}
30+
if ($githubBaseRef = getenv('GITHUB_REF')) {
31+
return $this->parseGithubRef($githubBaseRef);
32+
}
33+
34+
// Check GitLab CI
35+
if ($gitlabBranch = getenv('CI_COMMIT_REF_NAME')) {
36+
return $gitlabBranch;
37+
}
38+
39+
// Check Bitbucket Pipelines
40+
if ($bitbucketBranch = getenv('BITBUCKET_BRANCH')) {
41+
return $bitbucketBranch;
42+
}
43+
44+
// Check Azure Pipelines
45+
if ($azureBranch = getenv('BUILD_SOURCEBRANCH')) {
46+
return $this->parseAzureBranch($azureBranch);
47+
}
48+
49+
return null;
50+
}
51+
52+
private function parseGithubRef(string $ref): ?string
53+
{
54+
// Extract branch or tag name from refs/heads/ or refs/tags/
55+
if (preg_match('#refs/heads/(.+)#', $ref, $matches)) {
56+
return $matches[1];
57+
}
58+
if (preg_match('#refs/tags/(.+)#', $ref, $matches)) {
59+
return $matches[1];
60+
}
61+
62+
return null;
63+
}
64+
65+
private function parseAzureBranch(string $branch): ?string
66+
{
67+
// Extract branch name from refs/heads/
68+
if (strpos($branch, 'refs/heads/') === 0) {
69+
return substr($branch, strlen('refs/heads/'));
70+
}
71+
72+
return $branch;
73+
}
74+
}

0 commit comments

Comments
 (0)