diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index de6deb4..16c1633 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -74,17 +74,23 @@ jobs: run: bash scripts/build-erlang-base-images.sh --push - name: Build eventhub image + env: + REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }} + GITEA_TOKEN: ${{ secrets.REGISTRY_PASSWORD }} run: | set -euo pipefail - APP_VERSION="$(tr -d '[:space:]' < VERSION 2>/dev/null || echo 0.0)" + APP_VERSION="$(bash scripts/resolve-product-version.sh)" + APP_BUILD="${GITHUB_RUN_NUMBER:-0}" GIT_SHA="${GITHUB_SHA:0:12}" BUILT_AT="$(date -u +%Y-%m-%dT%H:%M:%SZ)" + echo "Bake: version=${APP_VERSION} build=${APP_BUILD} git_sha=${GIT_SHA}" bash scripts/retry-docker-build.sh 5 60 -- \ docker buildx build \ --network=host \ --pull \ --file docker/Dockerfile \ --build-arg "EVENTHUB_VERSION=${APP_VERSION}" \ + --build-arg "EVENTHUB_BUILD=${APP_BUILD}" \ --build-arg "EVENTHUB_GIT_SHA=${GIT_SHA}" \ --build-arg "EVENTHUB_BUILT_AT=${BUILT_AT}" \ --tag eventhub:latest \ diff --git a/docker/Dockerfile b/docker/Dockerfile index 0d2b150..e9b1e69 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -36,10 +36,12 @@ RUN collect-runtime-libs.sh /app/release /opt/runtime-libs FROM ${ERLANG_RUNTIME_IMAGE} ARG EVENTHUB_VERSION=0.0 +ARG EVENTHUB_BUILD=0 ARG EVENTHUB_GIT_SHA=unknown ARG EVENTHUB_BUILT_AT= ENV EVENTHUB_VERSION=${EVENTHUB_VERSION} \ + EVENTHUB_BUILD=${EVENTHUB_BUILD} \ EVENTHUB_GIT_SHA=${EVENTHUB_GIT_SHA} \ EVENTHUB_BUILT_AT=${EVENTHUB_BUILT_AT} diff --git a/scripts/resolve-product-version.sh b/scripts/resolve-product-version.sh new file mode 100644 index 0000000..344deab --- /dev/null +++ b/scripts/resolve-product-version.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env bash +# Product version (MAJOR.MINOR) из EventHubSpec/VERSION. +# Fallback: локальный VERSION (офлайн / без токена). +set -euo pipefail + +BASE="${GITEA_BASE_URL:-https://git.sabilin.com}" +TOKEN="${GITEA_TOKEN:-${REGISTRY_PASSWORD:-}}" +REF="${PRODUCT_VERSION_REF:-master}" +FALLBACK_FILE="${PRODUCT_VERSION_FILE:-VERSION}" + +fetch_spec() { + local url="${BASE}/api/v1/repos/EventHub/EventHubSpec/raw/VERSION?ref=${REF}" + if [[ -n "${TOKEN}" ]]; then + curl -fsSL -H "Authorization: token ${TOKEN}" "${url}" + else + curl -fsSL "${url}" + fi +} + +ver="" +if raw="$(fetch_spec 2>/dev/null)"; then + ver="$(printf '%s' "${raw}" | grep -Eo '[0-9]+\.[0-9]+' | head -1 || true)" +fi + +if [[ -z "${ver}" && -f "${FALLBACK_FILE}" ]]; then + echo "WARN: EventHubSpec/VERSION недоступен — fallback ${FALLBACK_FILE}" >&2 + ver="$(grep -Eo '[0-9]+\.[0-9]+' "${FALLBACK_FILE}" | head -1 || true)" +fi + +if [[ -z "${ver}" ]]; then + echo "ERROR: не удалось получить product version (Spec + local VERSION)" >&2 + exit 1 +fi + +if ! [[ "${ver}" =~ ^[0-9]+\.[0-9]+$ ]]; then + echo "ERROR: некорректный VERSION '${ver}' (ожидается MAJOR.MINOR)" >&2 + exit 1 +fi + +printf '%s\n' "${ver}" diff --git a/src/handlers/admin/admin_handler_health.erl b/src/handlers/admin/admin_handler_health.erl index 976c087..45f5c9c 100644 --- a/src/handlers/admin/admin_handler_health.erl +++ b/src/handlers/admin/admin_handler_health.erl @@ -29,7 +29,8 @@ trails() -> properties => #{ status => #{type => string}, service => #{type => string}, - version => #{type => string}, + version => #{type => string, description => <<"MAJOR.MINOR from EventHubSpec/VERSION">>}, + build => #{type => integer, description => <<"CI run_number (per-repo)">>}, git_sha => #{type => string}, built_at => #{type => string} } diff --git a/src/handlers/handler_health.erl b/src/handlers/handler_health.erl index 017ea5c..948ec4a 100644 --- a/src/handlers/handler_health.erl +++ b/src/handlers/handler_health.erl @@ -31,7 +31,8 @@ trails() -> properties => #{ status => #{type => string}, service => #{type => string}, - version => #{type => string}, + version => #{type => string, description => <<"MAJOR.MINOR from EventHubSpec/VERSION">>}, + build => #{type => integer, description => <<"CI run_number (per-repo)">>}, git_sha => #{type => string}, built_at => #{type => string} } diff --git a/src/infra/eventhub_build_info.erl b/src/infra/eventhub_build_info.erl index 221bac9..d22d87e 100644 --- a/src/infra/eventhub_build_info.erl +++ b/src/infra/eventhub_build_info.erl @@ -1,13 +1,15 @@ -module(eventhub_build_info). --export([info/0, version/0, git_sha/0, built_at/0]). +-export([info/0, version/0, build/0, git_sha/0, built_at/0]). %% @doc Runtime build identity from env (baked at image build or set in compose). +%% version = MAJOR.MINOR (EventHubSpec/VERSION); build = CI run_number (per-repo). -spec info() -> map(). info() -> #{ status => <<"ok">>, service => <<"eventhub">>, version => version(), + build => build(), git_sha => git_sha(), built_at => built_at() }. @@ -15,6 +17,20 @@ info() -> -spec version() -> binary(). version() -> env("EVENTHUB_VERSION", <<"0.0">>). +-spec build() -> non_neg_integer(). +build() -> + case os:getenv("EVENTHUB_BUILD") of + false -> 0; + [] -> 0; + Value -> + try list_to_integer(Value) of + N when is_integer(N), N >= 0 -> N; + _ -> 0 + catch + error:badarg -> 0 + end + end. + -spec git_sha() -> binary(). git_sha() -> env("EVENTHUB_GIT_SHA", <<"unknown">>). diff --git a/test/unit/admin_handler_health_tests.erl b/test/unit/admin_handler_health_tests.erl index c3904c7..fb37fbc 100644 --- a/test/unit/admin_handler_health_tests.erl +++ b/test/unit/admin_handler_health_tests.erl @@ -25,6 +25,8 @@ test_health_get() -> ?assertEqual(<<"ok">>, maps:get(<<"status">>, Result)), ?assertEqual(<<"eventhub">>, maps:get(<<"service">>, Result)), ?assert(is_map_key(<<"version">>, Result)), + ?assert(is_map_key(<<"build">>, Result)), + ?assert(is_integer(maps:get(<<"build">>, Result))), ?assert(is_map_key(<<"git_sha">>, Result)). test_health_wrong_method() ->