mtkennerly / poetry-dynamic-versioning

Plugin for Poetry to enable dynamic versioning based on VCS tags
MIT License
607 stars 35 forks source link

poetry version invalidates docker cache #123

Closed BlackZork closed 1 year ago

BlackZork commented 1 year ago

docker --version Docker version 24.0.2, build cb74dfcd85

Due to #122 I have to bind a whole repository when trying to use poetry inside a docker image.

Running poetry version outside invalidates a docker layer that stores poetry dependencies.

I attached a sample project pd-docker.tar.gz

To replicate:

  1. unzip project && cd into it
  2. run poetry install
  3. run docker build --build-context=git=.git .
 => [builder 2/6] RUN pip install poetry==1.5.0 poetry-dynamic-versioning==0.21.5                                                                                                                                                               15.4s
 => [builder 3/6] WORKDIR /pd-docker                                                                                                                                                                                                             0.0s
 => [builder 4/6] COPY pyproject.toml poetry.lock ./                                                                                                                                                                                             0.1s
 => [builder 5/6] RUN touch README.md                                                                                                                                                                                                            0.3s
 => [builder 6/6] RUN --mount=from=git,src=.,target=.git poetry install --no-root && rm -rf /tmp/poetry_cache                                                                                                                                    2.9s
 => exporting to image                                                                                                                                                                                                                           1.8s
 => => exporting layers                                                                                                                                                                                                                          1.8s
 => => writing image sha256:44a764713a1f388326a04085a5e9cb2cffd3a646fe6d91508824f9ae324312e2  
  1. run docker build --build-context=git=.git . again. It should not download anything:
 => CACHED [builder 2/6] RUN pip install poetry==1.5.0 poetry-dynamic-versioning==0.21.5                                                                                                                                                         0.0s
 => CACHED [builder 3/6] WORKDIR /pd-docker                                                                                                                                                                                                      0.0s
 => CACHED [builder 4/6] COPY pyproject.toml poetry.lock ./                                                                                                                                                                                      0.0s
 => CACHED [builder 5/6] RUN touch README.md                                                                                                                                                                                                     0.0s
 => CACHED [builder 6/6] RUN --mount=from=git,src=.,target=.git poetry install --no-root && rm -rf /tmp/poetry_cache                                                                                                                             0.0s
 => exporting to image                                                                                                                                                                                                                           0.0s
 => => exporting layers                                                                                                                                                                                                                          0.0s
 => => writing image sha256:44a764713a1f388326a04085a5e9cb2cffd3a646fe6d91508824f9ae324312e2  
  1. run poetry version
  2. run docker build --build-context=git=.git . again

Repeat steps 6 and 7. Sometimes docker considers layer 6/6 as not changed, but most of the time it is rebuild.

This layer should not be rebuild after running poetry version

BlackZork commented 1 year ago

As I misunderstood that I can pass version to POETRY_DYNAMIC_VERSIONING_BYPASS this case is not as important for me, because now I can run poetry install --no-root without mounting .git directory.

To avoid installation of dependencies on every build of devel container I had to set version as constant this way:

# Set version to constant value to prevent
# dependencies layer invalidation
ENV POETRY_DYNAMIC_VERSIONING_BYPASS=1
RUN poetry install --without dev --no-root && rm -rf $POETRY_CACHE_DIR

ARG my_version
ENV POETRY_DYNAMIC_VERSIONING_BYPASS=$my_version
# At runtime we will mount sources permanently from docker-compose
# rw is needed for poetry install to overwrite __version__ variable
# which will be lost anyway. We have to use pkg_resources.get_distribution to
# discover package version at runtime
RUN --mount=type=bind,src=pd-docker,target=pd-docker,rw poetry install --without dev && rm -rf $POETRY_CACHE_DIR

I think you can close this issue.