Skip to content

Commit

Permalink
fix: Fix baked-in commit SHA in simplified version (#20342)
Browse files Browse the repository at this point in the history
fix: Fix baked in commit SHA in simplified version
  • Loading branch information
Twixes authored Feb 14, 2024
1 parent de33672 commit 4914341
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/container-images-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ jobs:
with:
fetch-depth: 2

- name: Override git.py
run: >
echo "def get_git_commit(): return '${GITHUB_SHA}'" > posthog/git.py
echo "def get_git_branch(): return '${GITHUB_REF_NAME}'" >> posthog/git.py
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

Expand Down
11 changes: 10 additions & 1 deletion posthog/git.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import subprocess
from typing import Optional

# In production images, the two functions below are overwritten by container-images-cd.yml
_git_commit_baked_in: Optional[str] = None
try:
# Docker containers should have a commit.txt file in the base directory with the git
# commit hash used to generate them.
with open("commit.txt") as f:
_git_commit_baked_in = f.read()
except FileNotFoundError:
pass


def get_git_commit() -> Optional[str]:
"""Return the short hash of the last commit.
Example: get_git_commit() => "4ff54c8d"
"""
if _git_commit_baked_in:
return _git_commit_baked_in
try:
return subprocess.check_output(["git", "rev-parse", "--short", "HEAD"]).decode("utf-8").strip()
except Exception:
Expand Down
11 changes: 2 additions & 9 deletions posthog/settings/sentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from sentry_sdk.integrations.django import DjangoIntegration
from sentry_sdk.integrations.logging import LoggingIntegration
from sentry_sdk.integrations.redis import RedisIntegration
from posthog.git import get_git_commit

from posthog.settings import get_from_env
from posthog.settings.base_variables import TEST
Expand Down Expand Up @@ -143,15 +144,7 @@ def sentry_init() -> None:
sentry_logging = LoggingIntegration(level=sentry_logging_level, event_level=None)
profiles_sample_rate = get_from_env("SENTRY_PROFILES_SAMPLE_RATE", type_cast=float, default=0.0)

release = None
try:
# Docker containers should have a commit.txt file in the base directory with the git
# commit hash used to generate them.
with open("commit.txt") as f:
release = f.read()
except:
# The release isn't required, it's just nice to have.
pass
release = get_git_commit()

sentry_sdk.init(
send_default_pii=send_pii,
Expand Down

0 comments on commit 4914341

Please sign in to comment.