Files
jobtrackingapp/JobTrackerApi/Services/BuildMetadata.cs
T

40 lines
1.7 KiB
C#

using System.Reflection;
namespace JobTrackerApi.Services;
// Build metadata (version, commit, stamp) comes from configuration, populated by
// docker-compose as App__Version / App__CommitSha / App__BuildStamp.
//
// This lives here because two callers need the same answer: the admin system page and
// the anonymous /health endpoint. /health used to read the APP_VERSION *environment
// variable* directly, which compose never sets under that name — it passes App__Version,
// binding to the App:Version configuration key. So /health always reported "unknown".
// See docs/infrastructure/deployment.md for the health/version verification contract.
public static class BuildMetadata
{
// Ignore unresolved shell/compose placeholders that would otherwise leak into the
// admin UI or a health response, e.g. $(git rev-parse --short HEAD) or ${APP_COMMIT_SHA}.
public static string? Normalize(string? value)
{
var trimmed = (value ?? string.Empty).Trim();
if (string.IsNullOrWhiteSpace(trimmed)) return null;
if ((trimmed.StartsWith("$(") && trimmed.EndsWith(")")) || (trimmed.StartsWith("${") && trimmed.EndsWith("}")))
{
return null;
}
return trimmed;
}
// Configured version, else the assembly version, else "unknown". The assembly fallback
// is what makes local development useful: nothing sets App:Version there.
public static string ResolveVersion(IConfiguration cfg)
{
var version = Normalize(cfg["App:Version"]);
if (!string.IsNullOrWhiteSpace(version)) return version;
return Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "unknown";
}
}