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
9 changes: 9 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,12 @@
!zebrad
!docker/entrypoint.sh
!docker/runtime-entrypoint.sh

# TODO:
# replace git with a supported way of resetting files modified by cargo-chef, with the correct timestamps.
# (This will improve Docker cache performance when the changes in a PR don't impact Rust builds.)
#
# Background:
# https://hackmd.io/@kobzol/S17NS71bh#Using-cargo-chef
# https://github.com/LukeMathWalker/cargo-chef/issues/180#issuecomment-1552965888
!.git
35 changes: 28 additions & 7 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 \
git \
; \
rm -rf /var/lib/apt/lists/* /tmp/*

Expand Down Expand Up @@ -102,15 +103,25 @@ 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

# Copy across the source files and .git directory, so their timestamps are before the build cache timestamps.
COPY . .

# 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: is it faster to use --tests here?
RUN cargo chef cook --release --features "${TEST_FEATURES} ${FEATURES}" --workspace --recipe-path recipe.json
# TODO: add --locked when cargo-chef supports it
RUN cargo chef cook --tests --release --features "${TEST_FEATURES} ${FEATURES}" --workspace --recipe-path recipe.json

COPY . .
# Undo the source file changes made by cargo-chef.
# This also invalidates the cargo cache for those files by updating their timestamps,
# which makes sure the fake empty binaries created by cargo-chef are rebuilt.
RUN git reset --hard HEAD

# 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
Expand All @@ -127,10 +138,20 @@ ENTRYPOINT [ "/entrypoint.sh" ]
# `test` stage. This step is a dependency for the `runtime` stage, which uses the resulting
# zebrad binary from this step.
FROM deps AS release
RUN cargo chef cook --release --features "${FEATURES}" --recipe-path recipe.json

# Copy the git sources as a base for the build cache.
COPY . .
# Build zebra

# 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

# Undo the source file changes made by cargo-chef, so the fake empty zebrad binary is rebuilt.
RUN git reset --hard HEAD

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

COPY ./docker/runtime-entrypoint.sh /
Expand Down