34 lines
999 B
Python
34 lines
999 B
Python
|
|
"""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", "COMMIT_SHA", "SOURCE_VERSION"):
|
||
|
|
value = (os.environ.get(variable) or "").strip()
|
||
|
|
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"
|