From e0d58f06c7d1a3bade20fc8ffad90deac8b848d6 Mon Sep 17 00:00:00 2001 From: Juan Antonio Osorio Date: Thu, 16 Jan 2025 15:15:27 +0200 Subject: [PATCH] Set codegate version from build argument in container The codegate container does not run the python program via poetry, thus it does not have the necessary package metadata to determine the version. To work around this lack of information, this enhancement leverages Docker build arguments to set a special variable that will set the version. This way, we don't need to rely on package metadata nor manually bump the version. This machinery was set up in both the local image building and in CI, so we'll get the version and a commit hash. Signed-off-by: Juan Antonio Osorio --- .github/workflows/image-publish.yml | 1 + Dockerfile | 5 +++++ Makefile | 1 + src/codegate/__init__.py | 19 +++++++++++++------ 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/.github/workflows/image-publish.yml b/.github/workflows/image-publish.yml index 2af09a74..e5c97c20 100644 --- a/.github/workflows/image-publish.yml +++ b/.github/workflows/image-publish.yml @@ -87,6 +87,7 @@ jobs: cache-to: type=gha,mode=max build-args: | LATEST_RELEASE=${{ env.LATEST_RELEASE }} + CODEGATE_VERSION=${{ steps.version-string.outputs.tag }} - name: Capture Image Digest id: image-digest run: | diff --git a/Dockerfile b/Dockerfile index 761cbdfa..68d8cbea 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,8 @@ # Builder stage: Install dependencies and build the application FROM python:3.12-slim AS builder +ARG CODEGATE_VERSION=dev + # Install system dependencies RUN apt-get update && apt-get install -y --no-install-recommends \ gcc \ @@ -21,6 +23,9 @@ RUN poetry config virtualenvs.create false && \ # Copy the rest of the application COPY . /app +# Overwrite the _VERSION variable in the code +RUN sed -i "s/_VERSION =.*/_VERSION = \"${CODEGATE_VERSION}\"/g" /app/src/codegate/__init__.py + # Build the webapp FROM node:23-slim AS webbuilder diff --git a/Makefile b/Makefile index 6db025f5..3c3074d6 100644 --- a/Makefile +++ b/Makefile @@ -34,6 +34,7 @@ image-build: DOCKER_BUILDKIT=1 $(CONTAINER_BUILD) \ -f Dockerfile \ --build-arg LATEST_RELEASE=$(curl -s "https://api.github.com/repos/stacklok/codegate-ui/releases/latest" | grep '"zipball_url":' | cut -d '"' -f 4) \ + --build-arg CODEGATE_VERSION="$(shell git describe --tags --abbrev=0)-$(shell git rev-parse --short HEAD)-dev" \ -t codegate \ . \ -t ghcr.io/stacklok/codegate:$(VER) \ diff --git a/src/codegate/__init__.py b/src/codegate/__init__.py index 106a1d9d..24f8ce3c 100644 --- a/src/codegate/__init__.py +++ b/src/codegate/__init__.py @@ -7,12 +7,19 @@ from codegate.config import Config from codegate.exceptions import ConfigurationError -try: - __version__ = metadata.version("codegate") - __description__ = metadata.metadata("codegate")["Summary"] -except metadata.PackageNotFoundError: # pragma: no cover - __version__ = "unknown" - __description__ = "codegate" +_VERSION = "dev" +_DESC = "CodeGate - A Generative AI security gateway." + +def __get_version_and_description() -> tuple[str, str]: + try: + version = metadata.version("codegate") + description = metadata.metadata("codegate")["Summary"] + except metadata.PackageNotFoundError: + version = _VERSION + description = _DESC + return version, description + +__version__, __description__ = __get_version_and_description() __all__ = ["Config", "ConfigurationError", "LogFormat", "LogLevel", "setup_logging"]