"""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"): 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 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"