gmao/app_new/core/build_info.py

44 lines
1.3 KiB
Python
Raw Normal View History

2026-08-22 00:21:17 +02:00
"""Informations sur le build actuellement exécuté."""
import os
import subprocess
from pathlib import Path
def get_build_commit():
"""Retourne l'identifiant du commit déployé.
En production, le dépôt Git n'est pas forcément présent dans l'image :
les variables d'environnement sont donc prioritaires. Le fallback Git
permet de conserver un affichage utile en développement.
"""
for variable in ("GIT_COMMIT", "GIT_VERSION", "COMMIT_SHA", "SOURCE_VERSION"):
2026-08-22 00:21:17 +02:00
value = (os.environ.get(variable) or "").strip()
if value:
return value
# Les images Docker existantes écrivent déjà l'argument de build dans ce
# fichier, sans embarquer le dépôt Git.
version_file = Path('/app/.git-version')
try:
value = version_file.read_text(encoding='utf-8').strip()
except (OSError, UnicodeError):
value = ''
if value:
return value
2026-08-22 00:21:17 +02:00
repository = Path(__file__).resolve().parents[2]
try:
result = subprocess.run(
["git", "rev-parse", "HEAD"],
cwd=repository,
check=True,
capture_output=True,
text=True,
timeout=1,
)
except (OSError, subprocess.SubprocessError):
return "inconnu"
commit = result.stdout.strip()
return commit or "inconnu"