From 6107d4088285933255b69dba6cfcf39b95c12139 Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar <30073382+mighty840@users.noreply.github.com> Date: Tue, 6 Oct 2026 00:03:13 +0200 Subject: [PATCH] feat(platform): build commit in the version handshake MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GET /api/v1/platform/version reported the crate version (0.4.4) and no commit, so the portal's staff dashboard showed Prüfwerk without one. The agent now reads PRUEFWERK_VERSION and PRUEFWERK_COMMIT at compile time; the deploy-agent job passes the latest v* tag (without its v) and the commit as Docker build args, like the tenant-registry's GET /version. Locally, or with empty args, it falls back to the crate version and "unknown". Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB --- .gitea/workflows/ci.yml | 9 +++++ Dockerfile.agent | 5 +++ compliance-agent/src/api/handlers/platform.rs | 34 ++++++++++++++++++- 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 52e3723..1e3c147 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -227,9 +227,18 @@ jobs: apk add --no-cache git curl openssl git init && git remote add origin "${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git" git fetch --depth=1 origin "${GITHUB_SHA}" && git checkout FETCH_HEAD + # GET /api/v1/platform/version: the latest release tag without its v + # (the checkout is shallow, so fetch the history and tags first) and + # the commit. No tag → empty, and the agent reports its crate version. + git fetch --quiet --unshallow --tags origin || true + TAG=$(git describe --tags --abbrev=0 --match 'v[0-9]*' 2>/dev/null || true) + PRUEFWERK_VERSION="${TAG#v}" + echo "version: ${PRUEFWERK_VERSION:-crate} commit: ${GITHUB_SHA}" IMAGE=repo.breakpilot.com/pruefwerk/compliance-agent echo "$REGISTRY_PASSWORD" | docker login repo.breakpilot.com -u "$REGISTRY_USERNAME" --password-stdin DOCKER_BUILDKIT=1 docker build --secret id=git_token,env=PLATFORM_CI_TOKEN \ + --build-arg PRUEFWERK_VERSION="${PRUEFWERK_VERSION}" \ + --build-arg PRUEFWERK_COMMIT="${GITHUB_SHA}" \ -f Dockerfile.agent -t "$IMAGE:latest" -t "$IMAGE:${GITHUB_SHA}" . docker push "$IMAGE:latest" && docker push "$IMAGE:${GITHUB_SHA}" { command -v cosign >/dev/null 2>&1 || curl -sSfLo /usr/local/bin/cosign https://github.com/sigstore/cosign/releases/download/v2.4.3/cosign-linux-amd64 || wget -qO /usr/local/bin/cosign https://github.com/sigstore/cosign/releases/download/v2.4.3/cosign-linux-amd64; } || echo "::warning::cosign fetch failed" diff --git a/Dockerfile.agent b/Dockerfile.agent index 57bad0e..04e7971 100644 --- a/Dockerfile.agent +++ b/Dockerfile.agent @@ -17,6 +17,11 @@ FROM rust:1.94-bookworm AS builder WORKDIR /app COPY . . +# The version and commit GET /api/v1/platform/version reports (CI passes the +# latest release tag and the commit); the agent reads them at compile time and +# falls back to the crate version and "unknown" when they are empty. +ARG PRUEFWERK_VERSION= +ARG PRUEFWERK_COMMIT= # compliance-agent depends on the private firmwerk git repo (platform/firmwerk). Authenticate # the fetch with a PAT passed as a BuildKit secret (never baked into a layer). # Build with: DOCKER_BUILDKIT=1 docker build --secret id=git_token,env=PLATFORM_CI_TOKEN ... diff --git a/compliance-agent/src/api/handlers/platform.rs b/compliance-agent/src/api/handlers/platform.rs index 7b5ff60..c4a8662 100644 --- a/compliance-agent/src/api/handlers/platform.rs +++ b/compliance-agent/src/api/handlers/platform.rs @@ -432,11 +432,29 @@ pub async fn usage( })) } +/// The build version: `PRUEFWERK_VERSION` at compile time (CI: the latest +/// release tag without its `v`), else the crate version. +pub const BUILD_VERSION: &str = + non_empty_or(option_env!("PRUEFWERK_VERSION"), env!("CARGO_PKG_VERSION")); + +/// The commit the build is from: `PRUEFWERK_COMMIT` at compile time (CI: +/// the full SHA), else "unknown". The portal's version line shows 8 characters. +pub const BUILD_COMMIT: &str = non_empty_or(option_env!("PRUEFWERK_COMMIT"), "unknown"); + +/// A Docker build arg left empty is an empty env var, not an unset one. +const fn non_empty_or(value: Option<&'static str>, fallback: &'static str) -> &'static str { + match value { + Some(v) if !v.is_empty() => v, + _ => fallback, + } +} + /// `GET /api/v1/platform/version` — contract handshake (§4.1 /version). pub async fn version(State(st): State) -> Json { Json(serde_json::json!({ "product": st.agent.config.product_key, - "version": env!("CARGO_PKG_VERSION"), + "version": BUILD_VERSION, + "commit": BUILD_COMMIT, "contract": "2.0", // Deployment feature flags (PW_FEATURES). The dashboard reads the same // set the agent enforces instead of carrying its own copy of the env. @@ -750,3 +768,17 @@ pub async fn delete_git_source( "targets_archived": archived, }))) } + +#[cfg(test)] +mod build_info_tests { + use super::*; + + #[test] + fn an_unset_or_empty_build_value_falls_back() { + assert_eq!(non_empty_or(None, "0.4.4"), "0.4.4"); + assert_eq!(non_empty_or(Some(""), "unknown"), "unknown"); + assert_eq!(non_empty_or(Some("0.4.5"), "0.4.4"), "0.4.5"); + assert!(!BUILD_VERSION.is_empty()); + assert!(!BUILD_COMMIT.is_empty()); + } +} -- 2.54.1