Skip to content

Commit 88d9b69

Browse files
committed
Burn version into image
1 parent c80c15c commit 88d9b69

File tree

7 files changed

+42
-41
lines changed

7 files changed

+42
-41
lines changed

Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
FROM python:3.12-bullseye AS builder-base
44

5+
ARG PROJECT_VERSION
6+
57
# Install dependencies for the `psql` command.
68
# Must match the version of the postgres service in the compose file!
79
RUN apt-get update \
@@ -42,6 +44,7 @@ ADD . /app
4244
RUN --mount=type=cache,target=/root/.cache/uv \
4345
uv sync --frozen
4446

47+
ENV PROJECT_VERSION=${PROJECT_VERSION}
4548

4649
# production image
4750
FROM builder-base AS production
@@ -55,3 +58,5 @@ ADD . /app
5558

5659
RUN --mount=type=cache,target=/root/.cache/uv \
5760
uv sync --frozen --no-dev
61+
62+
ENV PROJECT_VERSION=${PROJECT_VERSION}

adit_radis_shared/cli_commands.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,13 @@ def compose_up(
7272
"Check ENVIRONMENT setting in .env file."
7373
)
7474

75-
version = helpers.get_latest_local_version_tag()
76-
if not helpers.is_production():
77-
version += "-dev"
78-
79-
cmd = f"{helpers.build_compose_cmd(profile)} up"
80-
81-
if not build:
82-
cmd += " --no-build"
75+
if build:
76+
cmd = f"{helpers.build_compose_cmd()} build"
77+
cmd += f" --build-arg PROJECT_VERSION={helpers.get_project_version()}-cli"
78+
helpers.execute_cmd(cmd)
8379

84-
cmd += " --detach"
85-
helpers.execute_cmd(cmd, env={"PROJECT_VERSION": version})
80+
cmd = f"{helpers.build_compose_cmd(profile)} up --no-build --detach"
81+
helpers.execute_cmd(cmd)
8682

8783

8884
def compose_down(
@@ -104,27 +100,21 @@ def stack_deploy(build: Annotated[bool, typer.Option(help="Build images")] = Fal
104100

105101
helpers.prepare_environment()
106102

107-
config = helpers.load_config_from_env_file()
108-
if config.get("ENVIRONMENT") != "production":
103+
if not helpers.is_production():
109104
sys.exit(
110105
"stack-deploy task can only be used in production environment. "
111106
"Check ENVIRONMENT setting in .env file."
112107
)
113108

114109
if build:
115110
cmd = f"{helpers.build_compose_cmd()} build"
111+
cmd += f" --build-arg PROJECT_VERSION={helpers.get_project_version()}-cli"
116112
helpers.execute_cmd(cmd)
117113

118-
version = helpers.get_latest_local_version_tag()
119-
if not helpers.is_production():
120-
version += "-dev"
121-
122114
# Docker Swarm Mode does not support .env files so we load the .env file manually
123115
# and pass the content as an environment variables.
124116
env = helpers.load_config_from_env_file()
125117

126-
env["PROJECT_VERSION"] = version
127-
128118
cmd = "docker stack deploy --detach "
129119
cmd += f" -c {helpers.get_compose_base_file()}"
130120
cmd += f" -c {helpers.get_compose_env_file()}"

adit_radis_shared/cli_helpers.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from cryptography.x509.oid import NameOID
2121
from django.core.management.utils import get_random_secret_key
2222
from dotenv import dotenv_values
23+
from dunamai import Pattern, Version
2324

2425
PROJECT_ID: str | None = None
2526
ROOT_PATH: Path | None = None
@@ -210,17 +211,8 @@ def get_latest_remote_version_tag(owner, repo) -> str | None:
210211
return None
211212

212213

213-
def get_latest_local_version_tag() -> str:
214-
# Get all tags sorted by creation date (newest first)
215-
result = capture_cmd("git tag -l --sort=-creatordate")
216-
all_tags = result.splitlines()
217-
218-
version_pattern = re.compile(r"^\d+\.\d+\.\d+$")
219-
for tag in all_tags:
220-
if version_pattern.match(tag):
221-
return tag
222-
223-
return "0.0.0"
214+
def get_project_version() -> str:
215+
return Version.from_git(pattern=Pattern.DefaultUnprefixed).serialize()
224216

225217

226218
def generate_django_secret_key():

docker-compose.base.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ x-app: &default-app
1414
DJANGO_SECRET_KEY: ${DJANGO_SECRET_KEY:?}
1515
DJANGO_SERVER_EMAIL: ${DJANGO_SERVER_EMAIL:?}
1616
IS_DOCKER_CONTAINER: 1
17-
PROJECT_VERSION: ${PROJECT_VERSION:-vX.Y.Z}
1817
SITE_DOMAIN: ${SITE_DOMAIN:?}
1918
SITE_NAME: ${SITE_NAME:?}
2019
SUPERUSER_AUTH_TOKEN: ${SUPERUSER_AUTH_TOKEN:-}

example_project/example_project/settings/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
# The source paths of the project
2626
SOURCE_PATHS = [BASE_PATH / "adit_radis_shared", BASE_PATH / "example_project"] # noqa: F405
2727

28-
# Fetch version from the environment which is passed through from the latest git version tag
29-
PROJECT_VERSION = env.str("PROJECT_VERSION", default="vX.Y.Z")
28+
# Fetch version from the environment which is passed through from the git version tag
29+
PROJECT_VERSION = env.str("PROJECT_VERSION", default="0.0.0")
3030

3131
# The project URL used in the navbar and footer
3232
PROJECT_URL = "https://github.com/openradx/adit-radis-shared"

pyproject.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@ dependencies = [
2222
"django-revproxy ~= 0.13",
2323
"django-tables2 ~= 2.7",
2424
"djangorestframework ~= 3.15",
25+
"dunamai>=1.23.0",
2526
"environs[django] ~= 14.1",
2627
"procrastinate[django] ~= 3.0",
2728
"psycopg[binary] ~= 3.2",
29+
"python-dotenv ~= 1.0",
30+
"requests ~= 2.32",
2831
"Twisted[tls,http2] ~= 24.11",
32+
"typer ~= 0.15",
2933
"wait-for-it ~= 2.3",
3034
"watchfiles ~= 1.0",
3135
"whitenoise ~= 6.9",
@@ -57,12 +61,9 @@ dev = [
5761
"pytest-playwright ~= 0.7",
5862
"pytest-timeout ~= 2.3",
5963
"pytest-watch ~= 4.2",
60-
"python-dotenv ~= 1.0",
6164
"pywatchman ~= 2.0",
62-
"requests ~= 2.32",
6365
"ruff ~= 0.9",
6466
"time-machine ~= 2.16",
65-
"typer ~= 0.15",
6667
"vermin ~= 1.6",
6768
]
6869

uv.lock

Lines changed: 20 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)