Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display appropiate branchname based on CI runner #138

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/Deployer/Task/Common/DefaultsTaskGlobal.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions src/Stdlib/ReleaseInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}}`');

Expand All @@ -40,7 +40,7 @@ public function getMessage(): string
}

$body[] = '';
$body[] = '**Servers:**';
$body[] = '*Servers:*';
foreach ($this->getServers() as $server) {
$body[] = '- ' . $server;
}
Expand Down
74 changes: 74 additions & 0 deletions src/Stdlib/TargetFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
}
Loading