feat: dynamic app version sourced from root VERSION file

Replaces the hardcoded version string in SettingsPage.tsx with a single
source of truth: a /VERSION file at the repo root. To cut a new release,
edit only that one file.

Frontend:
- vite.config.ts reads /VERSION at dev-server startup and injects it as
  the __APP_VERSION__ global via Vite's define mechanism (compile-time,
  zero runtime cost). Falls back to VITE_APP_VERSION env var for prod
  Docker builds that pass it as a build arg.
- vite-env.d.ts adds the TypeScript declaration for __APP_VERSION__.
- SettingsPage.tsx Badge now renders {__APP_VERSION__} instead of the
  literal string.

Backend:
- app.controller.ts reads /VERSION once at module load and includes
  "version" in both GET /api and GET /api/health responses.
- NewRelicTransactionInterceptor tags every NR transaction with
  newrelic.addCustomAttribute('appVersion', version) so releases can be
  compared in NRQL: SELECT average(duration) FROM Transaction WHERE
  appVersion = '2026.5.22'

Docker:
- docker-compose.yml mounts ./VERSION:/app/VERSION:ro in both backend
  and frontend dev containers.
- Production Dockerfiles include COPY VERSION ./ with a comment
  instructing CI to copy the root VERSION into each build context before
  docker build.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-22 10:03:44 -04:00
parent 922674eca4
commit 1dc3353e6e
9 changed files with 73 additions and 5 deletions

View File

@@ -7,6 +7,9 @@ WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
# VERSION must be copied into the build context before `docker build`.
# In CI / deploy scripts run: cp VERSION backend/VERSION (or pass --build-arg)
COPY VERSION ./
RUN npm run build
# Stage 2: Production
@@ -17,9 +20,10 @@ WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev && npm cache clean --force
# Copy compiled output and New Relic preload from builder
# Copy compiled output, New Relic preload, and VERSION from builder
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/newrelic-preload.js ./newrelic-preload.js
COPY --from=builder /app/VERSION ./VERSION
# New Relic agent — configured entirely via environment variables
ENV NEW_RELIC_NO_CONFIG_FILE=true

View File

@@ -1,4 +1,20 @@
import { Controller, Get } from '@nestjs/common';
import { readFileSync } from 'fs';
import { resolve } from 'path';
// Read at module load time — one I/O call for the lifetime of the process.
// VERSION file lives at the container root (/app/VERSION), mounted from the
// repo root via docker-compose. Falls back to package.json version if absent
// (e.g. in environments that pass APP_VERSION as an env var instead).
function readAppVersion(): string {
try {
return readFileSync(resolve(process.cwd(), 'VERSION'), 'utf-8').trim();
} catch {
return process.env.APP_VERSION ?? 'unknown';
}
}
const APP_VERSION = readAppVersion();
@Controller()
export class AppController {
@@ -13,6 +29,7 @@ export class AppController {
getRoot() {
return {
status: 'ok',
version: APP_VERSION,
timestamp: new Date().toISOString(),
service: 'hoa-financial-platform',
};
@@ -23,6 +40,7 @@ export class AppController {
getHealth() {
return {
status: 'ok',
version: APP_VERSION,
timestamp: new Date().toISOString(),
service: 'hoa-financial-platform',
};

View File

@@ -1,12 +1,15 @@
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable } from 'rxjs';
import { Request } from 'express';
import { readFileSync } from 'fs';
import { resolve } from 'path';
/**
* Sets a meaningful New Relic transaction name for every HTTP request.
* Sets a meaningful New Relic transaction name for every HTTP request and
* tags each transaction with the running app version.
*
* Without this, NestJS's setGlobalPrefix('api') causes the New Relic Express
* instrumentation to group ALL requests under the generic bucket
* Without the name override, NestJS's setGlobalPrefix('api') causes the New Relic
* Express instrumentation to group ALL requests under the generic bucket
* "Expressjs/GET/api$" (the compiled regex for the global prefix router),
* making per-endpoint APM data completely blind.
*
@@ -14,12 +17,25 @@ import { Request } from 'express';
* the matched pattern, e.g. "/api/accounts/:id") and calls newrelic.setTransactionName()
* to override the auto-detected name with "METHOD /route/pattern".
*
* The appVersion custom attribute lets you filter / compare releases in NRQL:
* SELECT average(duration) FROM Transaction WHERE appVersion = '2026.5.22'
*
* Gracefully no-ops when:
* - NEW_RELIC_ENABLED is not 'true' (dev / CI)
* - newrelic package is not installed
* - The NR agent fails to load for any reason
*/
function readAppVersion(): string {
try {
return readFileSync(resolve(process.cwd(), 'VERSION'), 'utf-8').trim();
} catch {
return process.env.APP_VERSION ?? 'unknown';
}
}
const APP_VERSION = readAppVersion();
let newrelic: any;
try {
if (process.env.NEW_RELIC_ENABLED === 'true') {
@@ -40,6 +56,9 @@ export class NewRelicTransactionInterceptor implements NestInterceptor {
// 404s get a useful name like "GET /api/unknown-path" instead of "Expressjs/GET/api$".
const route: string = (req.route as any)?.path ?? req.path;
newrelic.setTransactionName(`${req.method} ${route}`);
// Tag every transaction with the release version so you can segment NR
// dashboards and alerts by deployment.
newrelic.addCustomAttribute('appVersion', APP_VERSION);
}
return next.handle();
}