FWIW, I use --mount=type=cache
in my docker builds and had zero issues with it so far.
I was worried that I would need to clear the caches all the time because swift build
would get confused and require a clean build - but so far it has been quite the soldier. after ~300 changes/builds I had to clear the caches exactly once (after switching git branches)
here is an excerpt of what I use:
ARG SWIFT_VERSION=5.8
FROM swift:$SWIFT_VERSION AS builder
WORKDIR /build
# Resolve all dependencies if there was a change
COPY ./Package.* ./
RUN --mount=type=cache,target=/build/.build swift package resolve
# Copy all sources and build all
COPY . .
# build with caching and copy app out of cached folder
RUN --mount=type=cache,target=/build/.build \
swift build -c release \
&& cp -a .build/release/ /release
FROM swift:$SWIFT_VERSION-slim
COPY --from=builder /release/${SERVICE_NAME} ./app
ENTRYPOINT ["./app"]