Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(docker): Stop resetting the cargo-chef cache in the Dockerfile #6934

Merged
merged 12 commits into from
Jun 22, 2023
52 changes: 47 additions & 5 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ FROM chef AS deps
SHELL ["/bin/bash", "-xo", "pipefail", "-c"]
COPY --from=planner /opt/zebrad/recipe.json recipe.json

# Install zebra build deps
# Install zebra build deps and Dockerfile deps
RUN apt-get -qq update && \
apt-get -qq install -y --no-install-recommends \
llvm \
libclang-dev \
clang \
ca-certificates \
protobuf-compiler \
rsync \
; \
rm -rf /var/lib/apt/lists/* /tmp/*

Expand Down Expand Up @@ -102,20 +103,42 @@ FROM deps AS tests
COPY --from=us-docker.pkg.dev/zealous-zebra/zebra/zcash-params /root/.zcash-params /root/.zcash-params
COPY --from=us-docker.pkg.dev/zealous-zebra/zebra/lightwalletd /opt/lightwalletd /usr/local/bin

# cargo uses timestamps for its cache, so they need to be in this order:
# unmodified source files < previous build cache < modified source files
# Check the source timestamps:
RUN ls -l
COPY . .
RUN ls -l zebrad/src/bin zebra-network/src

# Re-hydrate the minimum project skeleton identified by `cargo chef prepare` in the planner stage,
# over the top of the original source files,
# and build it to cache all possible sentry and test dependencies.
#
# This is the caching Docker layer for Rust!
# This is the caching Docker layer for Rust tests!
# It creates fake empty test binaries so dependencies are built, but Zebra is not fully built.
#
# TODO: add --locked when cargo-chef supports it
RUN cargo chef cook --tests --release --features "${TEST_FEATURES} ${FEATURES}" --workspace --recipe-path recipe.json

COPY . .
# Test Zebra
# Undo the source file changes made by cargo-chef.
# rsync invalidates the cargo cache for the changed files only, by updating their timestamps.
# This makes sure the fake empty binaries created by cargo-chef are rebuilt.
COPY --from=planner /opt/zebrad zebra-original
RUN rsync --recursive --checksum --itemize-changes --verbose zebra-original/ .
RUN rm -r zebra-original

# Check the source and cache timestamps:
RUN ls -l zebrad/src/bin zebra-network/src
RUN ls -l target/release target/release/deps/libvergen*

# Build Zebra test binaries, but don't run them
RUN cargo test --locked --release --features "${TEST_FEATURES} ${FEATURES}" --workspace --no-run
RUN cp /opt/zebrad/target/release/zebrad /usr/local/bin
RUN cp /opt/zebrad/target/release/zebra-checkpoints /usr/local/bin

# Check the binary and cached dependency timestamps:
RUN ls -l target/release target/release/deps/libvergen*

COPY ./docker/entrypoint.sh /
RUN chmod u+x /entrypoint.sh

Expand All @@ -129,13 +152,32 @@ ENTRYPOINT [ "/entrypoint.sh" ]
# zebrad binary from this step.
FROM deps AS release

# Check the source timestamps:
RUN ls -l
COPY . .
RUN ls -l zebrad/src/bin zebra-network/src

# This is the caching layer for Rust zebrad builds.
# It creates a fake empty zebrad binary, see above for details.
#
# TODO: add --locked when cargo-chef supports it
RUN cargo chef cook --release --features "${FEATURES}" --package zebrad --bin zebrad --recipe-path recipe.json

COPY . .
# Undo the source file changes made by cargo-chef, so the fake empty zebrad binary is rebuilt.
COPY --from=planner /opt/zebrad zebra-original
RUN rsync --recursive --checksum --itemize-changes --verbose zebra-original/ .
RUN rm -r zebra-original

# Check the source and cache timestamps:
RUN ls -l zebrad/src/bin zebra-network/src
RUN ls -l target/release target/release/deps/libvergen*

# Build zebrad
RUN cargo build --locked --release --features "${FEATURES}" --package zebrad --bin zebrad

# Check the binary and cached dependency timestamps:
RUN ls -l target/release target/release/deps/libvergen*

COPY ./docker/runtime-entrypoint.sh /
RUN chmod u+x /runtime-entrypoint.sh

Expand Down