Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Make cargo-chef effective for CI builds #763

Merged
merged 3 commits into from
Oct 13, 2023
Merged

Conversation

athoscouto
Copy link
Contributor

@athoscouto athoscouto commented Oct 11, 2023

We need to cache Docker layers to cargo-chef to work properly.
This uses the Github Actions cache to save Docker layers, making docker build faster when no deps are changed.
It also changes the order of docker layers, to optimize cache usage when rebuilding and layer reutilization when pushing to the registry.

@athoscouto athoscouto force-pushed the optimize-docker-build-4 branch from 38a60b4 to e21dc73 Compare October 11, 2023 22:04
@athoscouto athoscouto force-pushed the optimize-docker-build-4 branch from 6204f87 to fa94d1b Compare October 12, 2023 02:17
@athoscouto athoscouto changed the title Make cargo-chef effective for CI builds 2 Make cargo-chef effective for CI builds Oct 12, 2023
@athoscouto athoscouto marked this pull request as ready for review October 12, 2023 02:20
# We need to install and set as default the toolchain specified in rust-toolchain.toml
# Otherwise cargo-chef will build dependencies using wrong toolchain
# This also prevents planner and builder steps from installing the toolchain over and over again
COPY rust-toolchain.toml rust-toolchain.toml
RUN cat rust-toolchain.toml | grep "channel" | awk '{print $3}' | sed 's/\"//g' > toolchain.txt \
&& rustup update $(cat toolchain.txt) \
&& rustup default $(cat toolchain.txt) \
&& rm toolchain.txt rust-toolchain.toml
&& rm toolchain.txt rust-toolchain.toml \
&& cargo install cargo-chef
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually a regression. If rust-toolchain.toml changes cargo install cargo-chef will be re-run. Previously it would be cached.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the toolchain changes we'll have to rebuild everything anyway, the cost of running cargo install cargo-chef is marginal.
I think the benefit of having all rust tools built by the same toolchain outweighs the downsides.
Let me know if you still want me to move it back.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fine. Just don't name the commit "Optimize ... layers" when in fact you're de-optimizing it.
If you have named your intend of "cleaning up" the layers, I wouldn't add such comment at all.

Comment on lines +40 to +42
COPY docker-entrypoint.sh /usr/local/bin

COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that reverse order would be better since docker-entrypoint.sh probably changes more often than ca-certificates.

Also don't you need to add apt-get install -y ca-certificates to builder stage?
I think it would be better to add apt-get install -y ca-certificates to chef stage and copy certificates from there so that changed dependencies wouldn't trigger reinstallation of certificates.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that reverse order would be better since docker-entrypoint.sh probably changes more often than ca-certificates.

That is true, but the entrypoint changes pretty rarely, and when that happens we'll only save a layer that weights the size of ca-certificates.crt. I preferred to keep it tidy by grouping together COPYs from the same place.

Let me know if you still want me to reorder.

Also don't you need to add apt-get install -y ca-certificates to builder stage?
I think it would be better to add apt-get install -y ca-certificates to chef stage and copy certificates from there so that changed dependencies wouldn't trigger reinstallation of certificates.

We don't have to because our Rust base image already does it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation about the rust base image containing ca-certificates.
Regarding the order. I'm fine with whatever order you pick. Just please name your intend correctly. If the commit was saying "reorder the layers to make them more readable at the expense of build speed" I wouldn't even add such comment. Saying that you're speeding something up when in fact you're doing the opposite is confusing.

@athoscouto athoscouto requested a review from haaawk October 12, 2023 20:23
@MarinPostma
Copy link
Contributor

I'll merge that, we'll improve on it if we're unhappy

@MarinPostma MarinPostma added this pull request to the merge queue Oct 13, 2023
Merged via the queue into main with commit 340e278 Oct 13, 2023
@MarinPostma MarinPostma deleted the optimize-docker-build-4 branch October 13, 2023 07:38
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants