From 4f60f6fbed66d92a2da8af754001f34744031466 Mon Sep 17 00:00:00 2001 From: "russell@unturf.com" Date: Sun, 1 Feb 2026 16:57:19 -0500 Subject: [PATCH] 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. --- .gitlab-ci.yml | 2 ++ remarkbox/api/views.py | 25 +++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index f00d6d6..030fd94 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -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 diff --git a/remarkbox/api/views.py b/remarkbox/api/views.py index ff9bac6..75c5485 100644 --- a/remarkbox/api/views.py +++ b/remarkbox/api/views.py @@ -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: