From 06e60fb2121b915528b3b41cd5c3952af72d9ec7 Mon Sep 17 00:00:00 2001 From: ducklet Date: Fri, 10 May 2024 17:01:18 +0200 Subject: [PATCH] refactor: use .python-version for docker build --- .dockerignore | 2 ++ Dockerfile | 10 +++++++--- scripts/docker-build | 33 +++++++++++++++++++++++++++------ scripts/docker-run | 10 +++++++++- 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/.dockerignore b/.dockerignore index 983e069..8bffc59 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,4 +1,6 @@ +**/__pycache__ *.local +.* /data /tests /unwind-ui diff --git a/Dockerfile b/Dockerfile index 00b6a2c..66014c6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,6 @@ -FROM docker.io/library/python:3.12-alpine +ARG PYTHON_VERSION + +FROM docker.io/library/python:${PYTHON_VERSION}-alpine RUN apk update --no-cache \ && apk upgrade --no-cache \ @@ -9,14 +11,16 @@ RUN addgroup -g 10001 py \ WORKDIR /var/app -COPY requirements.txt ./ +COPY build/requirements.txt ./ RUN pip install --no-cache-dir --upgrade \ --requirement requirements.txt USER 10000:10001 -COPY . ./ +COPY run ./ +COPY scripts ./scripts +COPY unwind ./unwind ENV UNWIND_DATA="/data" VOLUME $UNWIND_DATA diff --git a/scripts/docker-build b/scripts/docker-build index 8f4c093..b8a6861 100755 --- a/scripts/docker-build +++ b/scripts/docker-build @@ -6,20 +6,41 @@ cd "$RUN_DIR" builddir=build +_latest_python_version() { + filter="$1" + filter_regex=$(echo "$filter" | sed 's/\./\\\\./') + curl -sfL https://api.github.com/repos/python/cpython/git/refs/tags \ + | jq -r 'map(select(.ref | test("^refs/tags/v?'"$filter_regex"'($|\\.)")) | .ref | match("refs/tags/v?(.*)").captures[0].string)[]' \ + | grep -xE '[.0-9]+' \ + | sort --version-sort \ + | tail -n 1 +} + [ -z "${DEBUG:-}" ] || set -x mkdir -p "$builddir" +python_version_filter=$(cat .python-version) +python_version=$(_latest_python_version "$python_version_filter") + +if [ -z "$python_version" ]; then + echo >&2 "No Python version found matching the filter: $python_version_filter" + exit 1 +fi + +githash_short=$(git rev-parse --short HEAD) +githash_long=$(git rev-parse HEAD) +version="$githash_short" +echo "$version" >"$builddir"/version + poetry export \ --with=build \ --output="$builddir"/requirements.txt -githash=$(git rev-parse --short HEAD) -today=$(date -u '+%Y.%m.%d') -version="${today}+${githash}" -echo "$version" >"$builddir"/version - $DOCKER_BIN build \ --pull \ - --tag "code.dumpr.org/ducklet/unwind":"$version" \ + --build-arg="PYTHON_VERSION=$python_version" \ + --label="python.version=$python_version" \ + --label="git.hash=$githash_long" \ + --tag="code.dumpr.org/ducklet/unwind":"$version" \ . diff --git a/scripts/docker-run b/scripts/docker-run index 407b199..6ecdd2c 100755 --- a/scripts/docker-run +++ b/scripts/docker-run @@ -1,6 +1,7 @@ #!/bin/sh -eu : "${DOCKER_BIN:=docker}" +: "${UNWIND_PORT:=8097}" cd "$RUN_DIR" @@ -8,11 +9,18 @@ cd "$RUN_DIR" version=$(cat build/version) +localhost=127.0.0.1 +localport=8000 +netloc="$localhost:$localport" + +echo >&2 "Unwind will be available on http://$netloc/ +" + $DOCKER_BIN run \ --init \ -it --rm \ --read-only \ --memory '500m' \ - --publish 127.0.0.1:8000:8000 \ + --publish "$netloc":"$UNWIND_PORT" \ --volume "$RUN_DIR"/data:/data \ "code.dumpr.org/ducklet/unwind":"$version"