Skip to content

Commit 991a8d5

Browse files
committed
Dynamic version support with UV
1 parent 38ba72f commit 991a8d5

4 files changed

Lines changed: 41 additions & 72 deletions

File tree

.diploi/helm/app.yaml

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@ spec:
4141
command: ["/bin/sh", "-c"]
4242
args:
4343
- |
44+
set -e
45+
uv python install {{ ((.Values.envMap.PYTHON_VERSION).value | default "3.12") | quote }}
46+
uv venv .venv --clear
4447
if [ -f pyproject.toml ]; then
45-
echo "Pyproject.toml found, installing dependencies with uv" && \
46-
uv venv .venv --clear && \
47-
uv sync --frozen --no-cache --link-mode=copy
48+
uv sync
4849
elif [ -f requirements.txt ]; then
49-
echo "requirements.txt found, installing dependencies with pip" && \
50-
uv venv .venv --clear && \
51-
uv pip install --python .venv/bin/python -r requirements.txt --link-mode=copy
52-
else
53-
echo "No dependency definition found" >&2
54-
exit 1
55-
fi && \
56-
cp --remove-destination "$(readlink /app{{ .Values.folder }}/.venv/bin/python)" /app{{ .Values.folder }}/.venv/bin/python 2>/dev/null || :
50+
uv pip install -r requirements.txt
51+
fi
52+
env:
53+
- name: UV_PYTHON
54+
value: {{ ((.Values.envMap.PYTHON_VERSION).value | default "3.12") | quote }}
55+
- name: UV_PYTHON_INSTALL_DIR
56+
value: /app{{ .Values.folder }}/.python
5757
workingDir: /app{{ .Values.folder }}
5858
volumeMounts:
5959
- name: app-mount
@@ -83,6 +83,10 @@ spec:
8383
{{- end }}
8484
{{- end }}
8585
env:
86+
- name: UV_PYTHON
87+
value: {{ ((.Values.envMap.PYTHON_VERSION).value | default "3.12") | quote }}
88+
- name: UV_PYTHON_INSTALL_DIR
89+
value: /app{{ .Values.folder }}/.python
8690
{{- range .Values.env }}
8791
- name: {{ .identifier }}
8892
value: {{ .value | quote }}

.python-version

Lines changed: 0 additions & 1 deletion
This file was deleted.

Dockerfile

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,32 @@
11
# Use a Python image with uv pre-installed
2-
FROM ghcr.io/astral-sh/uv:python3.11-bookworm
2+
FROM ghcr.io/astral-sh/uv:bookworm-slim
33

44
# This will be set by the GitHub action to the folder containing this component.
55
ARG FOLDER=/app
66

7+
# This will be set by the GitHub action if "PYTHON_VERSION" ENV is set in diploi.yaml
8+
ARG PYTHON_VERSION=3.12
9+
10+
RUN mkdir -p /.cache/uv && chown -R 1000:1000 /.cache
11+
RUN mkdir -p /.local/share/uv/python && chown -R 1000:1000 /.local/share/uv
12+
13+
COPY --chown=1000:1000 . /app
714
WORKDIR ${FOLDER}
815

9-
# Enable bytecode compilation
1016
ENV UV_COMPILE_BYTECODE=1
11-
12-
# Copy from the cache instead of linking since it's a mounted volume
1317
ENV UV_LINK_MODE=copy
18+
ENV PATH="$FOLDER/.venv/bin:$PATH"
19+
ENV UV_PYTHON=${PYTHON_VERSION}
20+
ENV HOME=/tmp
1421

15-
# Ensure installed tools can be executed out of the box
16-
ENV UV_TOOL_BIN_DIR=/usr/local/bin
17-
18-
COPY . /app
22+
USER 1000:1000
1923

20-
RUN if [ -f requirements.txt ]; then \
21-
echo "requirements.txt found, installing dependencies with uv pip" && \
22-
uv venv .venv --clear && \
24+
RUN uv python install ${PYTHON_VERSION} && \
25+
uv venv .venv && \
26+
if [ -f pyproject.toml ]; then \
27+
uv sync --locked --no-dev || uv sync --no-dev; \
28+
elif [ -f requirements.txt ]; then \
2329
uv pip install -r requirements.txt; \
24-
else \
25-
echo "Using uv sync for dependency installation" && \
26-
uv sync --locked --no-dev; \
2730
fi
2831

29-
# Place executables in the environment at the front of the path
30-
ENV PATH="$FOLDER/.venv/bin:$PATH"
31-
32-
# Reset the entrypoint, don't invoke `uv`
33-
ENTRYPOINT []
34-
35-
EXPOSE 8000
36-
ENV PORT=8000
37-
ENV HOST="0.0.0.0"
38-
3932
CMD ["uv", "run", "--frozen", "src/main.py"]

Dockerfile.dev

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,20 @@
1-
# Use a Python image with uv pre-installed
2-
FROM ghcr.io/astral-sh/uv:python3.11-bookworm
1+
FROM ghcr.io/astral-sh/uv:bookworm-slim
32

43
# This will be set by the GitHub action to the folder containing this component.
54
ARG FOLDER=/app
65

7-
COPY --chown=1000:1000 . /app
8-
WORKDIR ${FOLDER}
6+
RUN mkdir /app && chown 1000:1000 /app
97

10-
# Enable bytecode compilation
11-
ENV UV_COMPILE_BYTECODE=1
8+
RUN mkdir -p /.cache/uv && chown -R 1000:1000 /.cache
9+
RUN mkdir -p /.local/share/uv/python && chown -R 1000:1000 /.local/share/uv
10+
RUN mkdir -p /.local/bin && chown -R 1000:1000 /.local/bin
1211

13-
# Ensure installed tools can be executed out of the box
14-
ENV UV_TOOL_BIN_DIR=/usr/local/bin
12+
WORKDIR /app
1513

16-
# Place executables in the environment at the front of the path
14+
ENV UV_LINK_MODE=copy
15+
ENV UV_PYTHON_INSTALL_DIR=$FOLDER/.python
1716
ENV PATH="$FOLDER/.venv/bin:$PATH"
1817

19-
# Fix uv + non-root environment
20-
ENV HOME=/tmp
21-
ENV XDG_DATA_HOME=/tmp/.local/share
22-
ENV XDG_CACHE_HOME=/tmp/.cache
23-
24-
USER root
25-
RUN apt-get update && apt-get install -y nodejs npm \
26-
&& npm install -g nodemon \
27-
&& rm -rf /var/lib/apt/lists/*
28-
29-
# Allow uv to use cache
30-
RUN mkdir -p /.cache/uv && chown 1000:1000 /.cache /.cache/uv
31-
3218
USER 1000:1000
3319

34-
EXPOSE 8000
35-
ENV PORT=8000
36-
ENV HOST="0.0.0.0"
37-
38-
ENTRYPOINT ["/usr/local/bin/nodemon"]
39-
40-
CMD ["--delay", "1", \
41-
"--watch", "pyproject.toml", \
42-
"--watch", "requirements.txt", \
43-
"--watch", ".venv/lib/*", \
44-
"--watch", ".venv/lib64/*", \
45-
"--watch", "src", \
46-
"--ext", "py", \
47-
"--exec", "sh -c 'if [ -f pyproject.toml ]; then uv run --isolated --with . python src/main.py; elif [ -f requirements.txt ]; then uv run --isolated --with-requirements requirements.txt python src/main.py; else uv run --isolated python src/main.py; fi'"]
20+
CMD ["uv", "run", "python", "src/main.py"]

0 commit comments

Comments
 (0)