Skip to content

Commit

Permalink
Merge pull request #570 from launchableinc/fix-collect-git-branch-nam…
Browse files Browse the repository at this point in the history
…e-command

Fix way of collecting git branch names
  • Loading branch information
Konboi authored Jul 10, 2023
2 parents d18f0f5 + e448da1 commit 658f19b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
63 changes: 53 additions & 10 deletions launchable/commands/record/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from tabulate import tabulate

from launchable.utils.key_value_type import normalize_key_value_types
from launchable.utils.link import LinkKind, capture_link
from launchable.utils.link import CIRCLECI_KEY, GITHUB_ACTIONS_KEY, JENKINS_URL_KEY, LinkKind, capture_link

from ...utils import subprocess
from ...utils.authentication import get_org_workspace
Expand All @@ -17,6 +17,14 @@
from ...utils.session import clean_session_files, write_build
from .commit import commit

JENKINS_GIT_BRANCH_KEY = "GIT_BRANCH"
JENKINS_GIT_LOCAL_BRANCH_KEY = "GIT_LOCAL_BRANCH"
GITHUB_ACTIONS_GITHUB_HEAD_REF_KEY = "GITHUB_HEAD_REF"
GITHUB_ACTIONS_GITHUB_BASE_REF_KEY = "GITHUB_BASE_REF"
CIRCLECI_CIRCLE_BRANCH_KEY = "CIRCLE_BRANCH"
CODE_BUILD_BUILD_ID_KEY = "CODEBUILD_BUILD_ID"
CODE_BUILD_WEBHOOK_HEAD_REF_KEY = "CODEBUILD_WEBHOOK_HEAD_REF"


@click.command()
@click.option(
Expand Down Expand Up @@ -122,15 +130,7 @@ def build(ctx: click.core.Context, build_name: str, source: List[str], max_days:
hash = subprocess.check_output("git rev-parse HEAD".split(), cwd=repo_dist).decode().replace("\n", "")
sources.append((repo_name, repo_dist, hash))

branch_name = None
try:
# TODO: Sometimes cannot get an actual branch name on GitHub Actions. Need to improve this method
branch_name = subprocess.check_output(
"git rev-parse --abbrev-ref HEAD".split(),
cwd=repo_dist).decode().replace(
"\n", "")
except Exception:
branch_name = ""
branch_name = _get_branch_name(repo_dist)

branch_name_map[repo_name] = branch_name

Expand Down Expand Up @@ -248,3 +248,46 @@ def build(ctx: click.core.Context, build_name: str, source: List[str], max_days:
))

write_build(build_name)


def _get_branch_name(repo_dist: str) -> str:

# Jenkins
# ref:
# https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/Complete-Jenkins-Git-environment-variables-list-for-batch-jobs-and-shell-script-builds
if os.environ.get(JENKINS_URL_KEY):
if os.environ.get(JENKINS_GIT_BRANCH_KEY):
return os.environ[JENKINS_GIT_BRANCH_KEY]
elif os.environ[JENKINS_GIT_LOCAL_BRANCH_KEY]:
return os.environ[JENKINS_GIT_LOCAL_BRANCH_KEY]
# Github Actions
# ref: https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
if os.environ.get(GITHUB_ACTIONS_KEY):
if os.environ.get(GITHUB_ACTIONS_GITHUB_HEAD_REF_KEY):
return os.environ[GITHUB_ACTIONS_GITHUB_HEAD_REF_KEY]
elif os.environ.get(GITHUB_ACTIONS_GITHUB_BASE_REF_KEY):
return os.environ[GITHUB_ACTIONS_GITHUB_BASE_REF_KEY]
# CircleCI
# ref: https://circleci.com/docs/variables/
if os.environ.get(CIRCLECI_KEY):
if os.environ.get(CIRCLECI_CIRCLE_BRANCH_KEY):
return os.environ[CIRCLECI_CIRCLE_BRANCH_KEY]
# AWS CodeBuild
# ref: https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-env-vars.html
if os.environ.get(CODE_BUILD_BUILD_ID_KEY):
if os.environ.get(CODE_BUILD_WEBHOOK_HEAD_REF_KEY):
# refs/head/<branch name>
return os.environ[CODE_BUILD_WEBHOOK_HEAD_REF_KEY].split("/")[-1]

branch_name = ""
try:
refs = subprocess.check_output(
"git show-ref | grep '^'$(git rev-parse HEAD)",
cwd=repo_dist).decode().split("\n")
if len(refs) > 0:
# e.g) ed6de84bde58d51deebe90e01ddfa5fa78899b1c refs/heads/branch-name
branch_name = refs[0].split("/")[-1]
except Exception:
# cannot get branch name by git command
pass
return branch_name
3 changes: 2 additions & 1 deletion tests/commands/record/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class BuildTest(CliTestCase):
# make sure the output of git-submodule is properly parsed
@responses.activate
@mock.patch.dict(os.environ, {"LAUNCHABLE_TOKEN": CliTestCase.launchable_token})
@mock.patch.dict(os.environ, {"GITHUB_ACTIONS": ""})
@mock.patch('launchable.utils.subprocess.check_output')
# to tests on GitHub Actions
@mock.patch.dict(os.environ, {"GITHUB_ACTIONS": ""})
Expand All @@ -21,7 +22,7 @@ def test_submodule(self, mock_check_output):
# the first call is git rev-parse HEAD
('c50f5de0f06fe16afa4fd1dd615e4903e40b42a2').encode(),
# the second call is git rev-parse --abbrev-ref HEAD
('main').encode(),
('ed6de84bde58d51deebe90e01ddfa5fa78899b1c refs/head/main\ned6de84bde58d51deebe90e01ddfa5fa78899b1c refs/remotes/origin/main\n').encode(), # noqa: E501
# the third call is git submodule status --recursive
(
' 491e03096e2234dab9a9533da714fb6eff5dcaa7 foo (v1.51.0-560-g491e030)\n'
Expand Down

0 comments on commit 658f19b

Please sign in to comment.