Skip to content

Commit

Permalink
Fix: GitHub CI workflow selects dev when building prod branch (#5428)
Browse files Browse the repository at this point in the history
  • Loading branch information
achave11-ucsc committed Jan 11, 2025
1 parent 9a43141 commit de3e8b6
Showing 1 changed file with 58 additions and 19 deletions.
77 changes: 58 additions & 19 deletions scripts/check_branch.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from dataclasses import (
dataclass,
)
import os
import sys
from typing import (
Expand Down Expand Up @@ -37,29 +40,54 @@ def __init__(self,
f'only {allowed}personal deployments.')


def check_branch(branch: Optional[str], deployment: str) -> None:
deployment = config.Deployment(deployment)
if deployment.is_shared:
deployments = config.shared_deployments_for_branch(branch)
if deployments is None or deployment not in deployments:
raise BranchDeploymentMismatch(branch, deployment, deployments)
@dataclass(kw_only=True)
class BranchContext:
name: Optional[str]
base_ref: Optional[str]
head_ref: Optional[str]
platform: str


def gitlab_branch() -> Optional[str]:
def branch_context() -> BranchContext:
"""
Return the current branch if we're on GitLab, else `None`
Determines the current branch context by checking multiple environment
variables across different CI platforms and local environments.
"""
# Gitlab checks out a specific commit which results in a detached HEAD
# (no active branch). Extract the branch name from the runner environment.
return os.environ.get('CI_COMMIT_REF_NAME')
github_base = os.environ.get('GITHUB_BASE_REF')
github_head = os.environ.get('GITHUB_HEAD_REF')
gitlab_branch = os.environ.get('CI_COMMIT_REF_NAME')
if github_base is not None or github_head is not None:
context = BranchContext(
name=github_head,
base_ref=github_base,
head_ref=github_head,
platform='github'
)
elif gitlab_branch is not None:
context = BranchContext(
name=gitlab_branch,
base_ref=None,
head_ref=None,
platform='gitlab'
)
else:
repo = git.Repo(config.project_root)
local_branch = None if repo.head.is_detached else repo.active_branch.name
context = BranchContext(
name=local_branch,
base_ref=None,
head_ref=None,
platform='local'
)
return context


def local_branch() -> Optional[str]:
"""
Return `None` if detached head, else the current branch
"""
repo = git.Repo(config.project_root)
return None if repo.head.is_detached else repo.active_branch.name
def check_branch(branch: Optional[str], deployment: str) -> None:
deployment = config.Deployment(deployment)
if deployment.is_shared:
deployments = config.shared_deployments_for_branch(branch)
if deployments is None or deployment not in deployments:
raise BranchDeploymentMismatch(branch, deployment, deployments)


def main(argv):
Expand All @@ -71,15 +99,26 @@ def main(argv):
help='Print the deployment matching the current branch or exit '
'with non-zero status code if no such deployment exists.')
args = parser.parse_args(argv)
branch = gitlab_branch() or local_branch()
context = branch_context()
deployment = default_deployment(context.name)
if args.print:
deployment = default_deployment(branch)
if deployment is None:
sys.exit(1)
else:
print(deployment)
else:
if context.platform == 'github' and context.base_ref:
branch = context.base_ref
else:
branch = context.name
check_branch(branch, config.deployment_stage)
allowed_deployments = config.shared_deployments_for_branch(branch)
if deployment.name == 'prod':
if branch != 'prod' and context.base_ref != 'prod':
raise BranchDeploymentMismatch(branch, deployment, allowed_deployments)
elif deployment.name == 'dev':
if branch != 'develop' and context.base_ref != 'develop':
raise BranchDeploymentMismatch(branch, deployment, allowed_deployments)


if __name__ == '__main__':
Expand Down

0 comments on commit de3e8b6

Please sign in to comment.