59 lines
2.0 KiB
Python
59 lines
2.0 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Utility script to set version environment variables for local development.
|
|
This replaces the need for version.txt file creation.
|
|
"""
|
|
|
|
import os
|
|
import subprocess
|
|
import json
|
|
from datetime import datetime
|
|
|
|
def get_git_info():
|
|
"""Get current git commit hash and branch"""
|
|
try:
|
|
# Get current commit hash
|
|
commit_hash = subprocess.check_output(['git', 'rev-parse', 'HEAD'],
|
|
text=True, stderr=subprocess.DEVNULL).strip()
|
|
|
|
# Get current branch
|
|
branch = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD'],
|
|
text=True, stderr=subprocess.DEVNULL).strip()
|
|
|
|
# Get latest tag
|
|
try:
|
|
latest_tag = subprocess.check_output(['git', 'describe', '--tags', '--abbrev=0'],
|
|
text=True, stderr=subprocess.DEVNULL).strip()
|
|
except subprocess.CalledProcessError:
|
|
latest_tag = 'unknown'
|
|
|
|
return {
|
|
'commit': commit_hash,
|
|
'branch': branch,
|
|
'tag': latest_tag
|
|
}
|
|
except (subprocess.CalledProcessError, FileNotFoundError):
|
|
return {
|
|
'commit': 'unknown',
|
|
'branch': 'unknown',
|
|
'tag': 'unknown'
|
|
}
|
|
|
|
def set_version_env():
|
|
"""Set version environment variables"""
|
|
git_info = get_git_info()
|
|
|
|
# Set environment variables
|
|
os.environ['APP_VERSION'] = git_info['tag']
|
|
os.environ['GIT_COMMIT'] = git_info['commit']
|
|
os.environ['GIT_BRANCH'] = git_info['branch']
|
|
os.environ['DEPLOYED_AT'] = datetime.utcnow().isoformat()
|
|
|
|
print("Version environment variables set:")
|
|
print(f"APP_VERSION: {os.environ['APP_VERSION']}")
|
|
print(f"GIT_COMMIT: {os.environ['GIT_COMMIT']}")
|
|
print(f"GIT_BRANCH: {os.environ['GIT_BRANCH']}")
|
|
print(f"DEPLOYED_AT: {os.environ['DEPLOYED_AT']}")
|
|
|
|
if __name__ == '__main__':
|
|
set_version_env() |