-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathTargetFinder.php
More file actions
74 lines (60 loc) · 1.8 KB
/
TargetFinder.php
File metadata and controls
74 lines (60 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
namespace Hypernode\Deploy\Stdlib;
use function Deployer\get;
class TargetFinder
{
public function getTarget(): string
{
$branch = get('branch', 'HEAD');
if (!empty($branch) && $branch != 'HEAD') {
return $branch;
}
$branch = $this->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;
}
}