Add build version indicator to sidebar menu

Display commit date and short hash (e.g., "2026-01-08 fb59ef7") below
the ANIMALTRACK title in the sidebar. In development, reads from git
directly; in Docker, reads from BUILD_DATE/BUILD_COMMIT env vars
injected at build time from the Nix flake.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-08 10:36:44 +00:00
parent fb59ef72a8
commit 5c12eb553c
4 changed files with 46 additions and 3 deletions

View File

@@ -1,4 +1,4 @@
{ pkgs, pythonEnv, python }: { pkgs, pythonEnv, python, buildDate ? "unknown", buildCommit ? "unknown" }:
let let
# Build animaltrack as a package # Build animaltrack as a package
@@ -59,6 +59,8 @@ pkgs.dockerTools.buildImage {
"PATH=${pkgs.lib.makeBinPath [ pkgs.busybox pkgs.bash pkgs.sqlite pythonEnv animaltrack ]}" "PATH=${pkgs.lib.makeBinPath [ pkgs.busybox pkgs.bash pkgs.sqlite pythonEnv animaltrack ]}"
"PYTHONPATH=${pythonEnv}/${pythonEnv.sitePackages}:${animaltrack}/${pythonEnv.sitePackages}" "PYTHONPATH=${pythonEnv}/${pythonEnv.sitePackages}:${animaltrack}/${pythonEnv.sitePackages}"
"PYTHONUNBUFFERED=1" "PYTHONUNBUFFERED=1"
"BUILD_DATE=${buildDate}"
"BUILD_COMMIT=${buildCommit}"
]; ];
ExposedPorts = { ExposedPorts = {
"5000/tcp" = {}; "5000/tcp" = {};

View File

@@ -67,7 +67,12 @@
in in
{ {
packages.${system} = { packages.${system} = {
dockerImage = import ./docker.nix { inherit pkgs pythonEnv python; }; dockerImage = let
buildDate = let
d = self.lastModifiedDate or "00000000000000";
in "${builtins.substring 0 4 d}-${builtins.substring 4 2 d}-${builtins.substring 6 2 d}";
buildCommit = self.shortRev or self.dirtyShortRev or "unknown";
in import ./docker.nix { inherit pkgs pythonEnv python buildDate buildCommit; };
}; };
devShells.${system}.default = pkgs.mkShell { devShells.${system}.default = pkgs.mkShell {

View File

@@ -0,0 +1,34 @@
# ABOUTME: Provides build information (date + commit hash) for version display.
# ABOUTME: Reads from env vars (Docker) or git (development).
import os
import subprocess
def get_build_info() -> str:
"""Returns build info in '2025-01-08 fb59ef7' format.
Checks BUILD_DATE and BUILD_COMMIT env vars first (set by Docker),
then falls back to reading git info at runtime (development).
Returns 'unknown' if neither source is available.
"""
build_date = os.environ.get("BUILD_DATE")
build_commit = os.environ.get("BUILD_COMMIT")
if build_date and build_commit:
return f"{build_date} {build_commit}"
# Try git at runtime (development mode)
try:
result = subprocess.run(
["git", "log", "-1", "--format=%cs %h"],
capture_output=True,
text=True,
timeout=5,
)
if result.returncode == 0 and result.stdout.strip():
return result.stdout.strip()
except (subprocess.SubprocessError, FileNotFoundError, OSError):
pass
return "unknown"

View File

@@ -4,6 +4,7 @@
from fasthtml.common import A, Button, Div, Nav, Script, Span, Style from fasthtml.common import A, Button, Div, Nav, Script, Span, Style
from fasthtml.svg import Path, Svg from fasthtml.svg import Path, Svg
from animaltrack.build_info import get_build_info
from animaltrack.models.reference import UserRole from animaltrack.models.reference import UserRole
from animaltrack.web.templates.icons import EggIcon, FeedIcon, MoveIcon from animaltrack.web.templates.icons import EggIcon, FeedIcon, MoveIcon
@@ -213,7 +214,8 @@ def Sidebar( # noqa: N802
return Nav( return Nav(
# Logo/Brand # Logo/Brand
Div( Div(
Span("ANIMALTRACK", cls="text-amber-600 font-bold tracking-wider text-sm"), Div("ANIMALTRACK", cls="text-amber-600 font-bold tracking-wider text-sm"),
Div(get_build_info(), cls="text-stone-600 text-[10px] tracking-wide"),
cls="px-4 py-4 border-b border-stone-800", cls="px-4 py-4 border-b border-stone-800",
), ),
# Primary navigation # Primary navigation