-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from hostcc/fix/python-deps-build-cache
fix: Properly collect Python dependencies during image build. Next attempt at build cache
- Loading branch information
Showing
4 changed files
with
225 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,48 @@ | ||
FROM python:3.12.5-alpine AS build | ||
COPY . /usr/src/ | ||
WORKDIR /usr/src/ | ||
FROM python:3.12.5-alpine AS deps | ||
|
||
# Rust and Cargo are required to build `pyndatic-core` on ARM platforms | ||
RUN apk add -U cargo git rust \ | ||
&& pip install build \ | ||
&& apk cache clean | ||
|
||
# Limit use of the build context to the requirements file only, to avoid cache | ||
# invalidation when other files get changed | ||
COPY requirements.txt . | ||
# Install dependencies in a separate layer to cache them | ||
RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt | ||
RUN pip install --root /tmp/target/ -r requirements.txt | ||
|
||
FROM python:3.12.5-alpine AS build | ||
|
||
RUN pip install build | ||
|
||
# Build the package | ||
RUN python -m build \ | ||
&& pip install --root target/ dist/*-`cat version.txt`*.whl | ||
ARG VERSION | ||
RUN test -z "${VERSION}" && echo "No 'VERSION' argument provided, exiting" \ | ||
&& exit 1 || true | ||
|
||
# Writeable mount is needed for src/*.egg-info the `setup` module wants to | ||
# create. `pip install --no-deps` is to skip installing dependencies to the | ||
# package thus requiring extra prerequisites and extending the build time - | ||
# those already fulfilled by `deps` stage | ||
RUN \ | ||
--mount=type=bind,target=source/,rw \ | ||
SETUPTOOLS_SCM_PRETEND_VERSION_FOR_ENERGOMERA_HASS_MQTT=${VERSION} \ | ||
python -m build --outdir /tmp/dist/ source/ \ | ||
&& pip install --no-deps --root /tmp/target/ /tmp/dist/*-${VERSION}*.whl | ||
|
||
FROM python:3.12.5-alpine | ||
# Ensure all the OS updates are applied to the resulting image | ||
RUN apk -U upgrade \ | ||
&& apk cache clean | ||
|
||
COPY --from=deps \ | ||
/tmp/target/usr/local/lib/ \ | ||
/usr/local/lib/ | ||
COPY --from=build \ | ||
/usr/src/target/root/.local/lib/ /usr/local/lib/ | ||
/tmp/target/usr/local/lib/ \ | ||
/usr/local/lib/ | ||
COPY --from=build \ | ||
/usr/src/target/root/.local/bin/ \ | ||
/tmp/target/usr/local/bin/ \ | ||
/usr/local/bin/ | ||
|
||
ENTRYPOINT ["energomera-hass-mqtt"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.