Fix version endpoint to read commit-hash.txt from CI build

The deploy environment has no .git directory. Now reads
commit-hash.txt (written by CI) from the virtualenv, repo
root, or /opt/remarkbox before falling back to git rev-parse.
CI build step now copies commit-hash.txt into the virtualenv.
This commit is contained in:
russell@unturf.com 2026-02-01 16:57:19 -05:00
parent f0e365fd43
commit 4f60f6fbed
2 changed files with 25 additions and 2 deletions

View file

@ -37,6 +37,8 @@ build:
- sha512sum env.tar.gz >> env.tar.gz.hash
# Create commit-hash.txt to track this build's git commit hash.
- echo $CI_COMMIT_SHA >> commit-hash.txt
# Also place it inside the virtualenv so the deployed app can read it.
- cp commit-hash.txt /opt/remarkbox/env/commit-hash.txt
artifacts:
paths:
- env.tar.gz

View file

@ -1,6 +1,7 @@
import os
import re
import subprocess
import sys
from pyramid.response import Response
from pyramid.view import view_config
@ -53,11 +54,31 @@ def get_param(request, key, default=None):
def _get_git_commit():
"""Read the current git commit hash, once at import time."""
"""Read the current git commit hash, once at import time.
Checks commit-hash.txt (written by CI build) first,
then falls back to git rev-parse for dev environments.
"""
# CI build writes commit-hash.txt as a build artifact
_this_dir = os.path.dirname(os.path.abspath(__file__))
for candidate in [
os.path.join(_this_dir, "..", "..", "commit-hash.txt"), # repo root
os.path.join(sys.prefix, "commit-hash.txt"), # inside virtualenv
"/opt/remarkbox/commit-hash.txt",
]:
try:
with open(candidate) as f:
sha = f.read().strip()
if sha:
return sha[:7]
except OSError:
pass
# Dev environment: read from git
try:
return subprocess.check_output(
["git", "rev-parse", "--short", "HEAD"],
cwd=os.path.dirname(os.path.abspath(__file__)),
cwd=_this_dir,
stderr=subprocess.DEVNULL,
).decode().strip()
except Exception: