feat(admin): show the deployed commit and build time in the admin (#233)
Linting / lint (pull_request) Successful in 2m38s
SonarQube Analysis / sonarqube (pull_request) Failing after 29m40s

There was no way to tell which build an environment was running. That is not hypothetical: minutes after #232 merged, `npm run backfill:images` in QA failed with `tsx: not found` because the container was still serving the pre-merge image, and the only thing that revealed it was npm echoing the old script line. Had the change been anywhere other than a package.json script, the container would have looked healthy while running the wrong code.

The header now reads something like `a5076cc · built 29 Aug 20:36`. The commit answers "is this the code I expect"; the build time answers "did my redeploy actually rebuild", which is a different question and the one that would have caught the case above.

The commit is read out of `.git` directly rather than by shelling out, because node:20-bookworm-slim has no git binary and adding an apt layer so the image can print seven characters is a poor trade. `.git` is copied into the build stage only — verified absent from the final image — so no repository history reaches a deployed container.

Resolution is pure and separately tested across every shape that actually occurs: a detached HEAD holding the object name, which is what a checkout of a ref produces; a symbolic HEAD followed to a loose ref file; the same followed to packed-refs, which is what a fresh clone commonly has; peeled `^` tag lines ignored so an annotated tag cannot yield the wrong commit; and every failure path returning `unknown`. That last part is the one that matters most — this runs during a Docker build, and a version stamp must never be the thing that stops a deploy.

Served from a gated /api/admin/version rather than folded into /api/config. That endpoint is public, and a commit hash there would tell any storefront visitor exactly which revision of a public repository is deployed. An integration test asserts the gate and asserts the public config does not carry it, because the boundary is the whole point rather than an implementation detail.

Verified in the built image rather than argued: the stamp inside it reads a5076cc, matching `git rev-parse --short HEAD`, and a running container serves it from /api/admin/version while /api/config returns only what it did before.

Backend: 296 unit, 263 integration, tsc clean, lint unchanged at six pre-existing warnings. Frontend builds clean with its two pre-existing warnings untouched.

Closes #233
This commit is contained in:
2026-08-29 15:37:12 -05:00
parent a5076cc217
commit 44328d0b5c
9 changed files with 499 additions and 1 deletions
+2
View File
@@ -33,6 +33,7 @@ import Emails from './Emails';
import Settings from './Settings';
import Categories from './Categories';
import Tags from './Tags';
import BuildStamp from './BuildStamp';
import CategoryTreeSelect from './CategoryTreeSelect';
import ItemCard from '../components/ItemCard';
import InventoryFilters from './InventoryFilters';
@@ -379,6 +380,7 @@ export default function Admin() {
<Header className="site-header" style={{ background: token.colorBgContainer, borderBottom: `1px solid ${token.colorBorderSecondary}` }}>
<Title level={3} className="site-header-title" style={{ color: token.colorText }}>Admin</Title>
<div className="site-header-actions">
<BuildStamp />
<Switch checked={mode === 'dark'} onChange={toggle} checkedChildren="Dark" unCheckedChildren="Light" />
</div>
</Header>
+61
View File
@@ -0,0 +1,61 @@
import React, { useEffect, useState } from 'react';
import Typography from 'antd/es/typography';
const { Text } = Typography;
interface BuildInfo {
commit: string;
builtAt: string | null;
}
/**
* Which build this environment is running (#233).
*
* There was previously no way to tell from the screen, and a stack that had
* been redeployed could still be serving the previous image. Shown in the admin
* header rather than tucked into a settings tab, because its whole value is
* being visible without going to look for it.
*
* Served from the gated /api/admin/version rather than the public /api/config,
* so a commit hash is never handed to a storefront visitor.
*/
export default function BuildStamp() {
const [info, setInfo] = useState<BuildInfo | null>(null);
useEffect(() => {
let cancelled = false;
void fetch('/api/admin/version')
.then((res) => (res.ok ? res.json() : null))
.then((data: BuildInfo | null) => {
if (!cancelled) setInfo(data);
})
// Renders nothing on failure. A version stamp is a convenience, and it
// should not put an error in front of someone doing something else.
.catch(() => undefined);
return () => {
cancelled = true;
};
}, []);
if (!info) return null;
// Local time and no seconds: this is read by a person comparing it against
// when they pressed redeploy, not by anything that needs precision.
const built = info.builtAt
? new Date(info.builtAt).toLocaleString(undefined, {
day: 'numeric',
month: 'short',
hour: '2-digit',
minute: '2-digit'
})
: null;
return (
<Text type="secondary" style={{ fontSize: 12, fontFamily: 'monospace' }}>
{info.commit}
{built ? ` · built ${built}` : ''}
</Text>
);
}