Skip to content

Commit e1b3b4c

Browse files
authored
Set codegate version from build argument in container (#615)
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 <[email protected]>
1 parent 55cb0a9 commit e1b3b4c

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

.github/workflows/image-publish.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ jobs:
8787
cache-to: type=gha,mode=max
8888
build-args: |
8989
LATEST_RELEASE=${{ env.LATEST_RELEASE }}
90+
CODEGATE_VERSION=${{ steps.version-string.outputs.tag }}
9091
- name: Capture Image Digest
9192
id: image-digest
9293
run: |

Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Builder stage: Install dependencies and build the application
22
FROM python:3.12-slim AS builder
33

4+
ARG CODEGATE_VERSION=dev
5+
46
# Install system dependencies
57
RUN apt-get update && apt-get install -y --no-install-recommends \
68
gcc \
@@ -21,6 +23,9 @@ RUN poetry config virtualenvs.create false && \
2123
# Copy the rest of the application
2224
COPY . /app
2325

26+
# Overwrite the _VERSION variable in the code
27+
RUN sed -i "s/_VERSION =.*/_VERSION = \"${CODEGATE_VERSION}\"/g" /app/src/codegate/__init__.py
28+
2429
# Build the webapp
2530
FROM node:23-slim AS webbuilder
2631

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ image-build:
3434
DOCKER_BUILDKIT=1 $(CONTAINER_BUILD) \
3535
-f Dockerfile \
3636
--build-arg LATEST_RELEASE=$(curl -s "https://api.github.com/repos/stacklok/codegate-ui/releases/latest" | grep '"zipball_url":' | cut -d '"' -f 4) \
37+
--build-arg CODEGATE_VERSION="$(shell git describe --tags --abbrev=0)-$(shell git rev-parse --short HEAD)-dev" \
3738
-t codegate \
3839
. \
3940
-t ghcr.io/stacklok/codegate:$(VER) \

src/codegate/__init__.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@
77
from codegate.config import Config
88
from codegate.exceptions import ConfigurationError
99

10-
try:
11-
__version__ = metadata.version("codegate")
12-
__description__ = metadata.metadata("codegate")["Summary"]
13-
except metadata.PackageNotFoundError: # pragma: no cover
14-
__version__ = "unknown"
15-
__description__ = "codegate"
10+
_VERSION = "dev"
11+
_DESC = "CodeGate - A Generative AI security gateway."
12+
13+
def __get_version_and_description() -> tuple[str, str]:
14+
try:
15+
version = metadata.version("codegate")
16+
description = metadata.metadata("codegate")["Summary"]
17+
except metadata.PackageNotFoundError:
18+
version = _VERSION
19+
description = _DESC
20+
return version, description
21+
22+
__version__, __description__ = __get_version_and_description()
1623

1724
__all__ = ["Config", "ConfigurationError", "LogFormat", "LogLevel", "setup_logging"]
1825

0 commit comments

Comments
 (0)