Skip to content

Commit d5efd04

Browse files
authored
Update memory-testing Dockerfile to use cargo-chef (#259)
## 🎟️ Tracking <!-- Paste the link to the Jira or GitHub issue or otherwise describe / point to where this change is coming from. --> ## 📔 Objective The previous Dockerfile was trying to copy only the Cargo.toml and creating fake lib.rs files in an attempt to build the dependencies before the rest of the source code was copied, which would help with build caching and improve build times considerably. This was very error prone and would break any time extra workspace dependencies were added. This PR migrates that process to use `cargo-chef` instead, which automatically handles that process for us. The dependencies get cached just as well as before, but we don't need to fiddle with creating fake lib.rs to make the compiler happy. I've also updated the dockerignore to include some files that were previously missing. ## ⏰ Reminders before review - Contributor guidelines followed - All formatters and local linters executed and passed - Written new unit and / or integration tests where applicable - Protected functional changes with optionality (feature flags) - Used internationalization (i18n) for all UI strings - CI builds passed - Communicated to DevOps any deployment requirements - Updated any necessary documentation (Confluence, contributing docs) or informed the documentation team ## 🦮 Reviewer guidelines <!-- Suggested interactions but feel free to use (or not) as you desire! --> - 👍 (`:+1:`) or similar for great changes - 📝 (`:memo:`) or ℹ️ (`:information_source:`) for notes or general info - ❓ (`:question:`) for questions - 🤔 (`:thinking:`) or 💭 (`:thought_balloon:`) for more open inquiry that's not quite a confirmed issue and could potentially benefit from discussion - 🎨 (`:art:`) for suggestions / improvements - ❌ (`:x:`) or ⚠️ (`:warning:`) for more significant problems or concerns needing attention - 🌱 (`:seedling:`) or ♻️ (`:recycle:`) for future improvements or indications of technical debt - ⛏ (`:pick:`) for minor or nitpick changes
1 parent 5449a5a commit d5efd04

File tree

2 files changed

+25
-33
lines changed

2 files changed

+25
-33
lines changed

crates/memory-testing/Dockerfile

+20-30
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,28 @@
11
###############################################
22
# Build stage #
33
###############################################
4-
FROM rust:1.85 AS build
5-
4+
FROM rust:1.85 AS base
65
WORKDIR /app
76

8-
# Copy dependency files and create dummy files to allow cargo to build the dependencies in a separate stage
9-
COPY Cargo.toml Cargo.lock /app/
10-
COPY crates/bitwarden-crypto/Cargo.toml /app/crates/bitwarden-crypto/
11-
COPY crates/memory-testing/Cargo.toml /app/crates/memory-testing/
12-
13-
# Patch Cargo.toml to remove `, "bitwarden_license/*"` from the members array
14-
RUN sed -i 's/\([" ]*\)"bitwarden_license\/\*"[ ,]*\([" ]*\)/\1\2/g' Cargo.toml
15-
16-
RUN mkdir -p /app/crates/bitwarden-crypto/src \
17-
/app/crates/bitwarden-crypto/benches \
18-
/app/crates/memory-testing/src \
19-
&& touch /app/crates/bitwarden-crypto/src/lib.rs \
20-
/app/crates/bitwarden-crypto/benches/default_allocator.rs \
21-
/app/crates/bitwarden-crypto/benches/zeroizing_allocator.rs \
22-
&& echo 'fn main(){}' > /app/crates/memory-testing/src/main.rs \
23-
&& cargo build -p memory-testing --release
24-
25-
# Delete dummy files and copy the actual source code
26-
RUN rm /app/crates/bitwarden-crypto/src/lib.rs \
27-
/app/crates/bitwarden-crypto/benches/default_allocator.rs \
28-
/app/crates/bitwarden-crypto/benches/zeroizing_allocator.rs \
29-
/app/crates/memory-testing/src/main.rs
30-
31-
COPY crates/bitwarden-crypto /app/crates/bitwarden-crypto
32-
COPY crates/memory-testing/src /app/crates/memory-testing/src
33-
34-
# Build the project. We use touch to force a rebuild of the now real files
35-
RUN touch /app/crates/bitwarden-crypto/src/lib.rs /app/crates/memory-testing/src/main.rs
7+
# Make sure the correct rust toolchain is installed only once
8+
COPY rust-toolchain.toml /app/
9+
RUN rustup show
10+
11+
# Install cargo-chef, to cache dependency builds
12+
RUN cargo install cargo-chef --version 0.1.71 --locked
13+
14+
# Prepare the recipe for the dependencies, which will be built as the first part of the next stage
15+
FROM base AS planner
16+
COPY . /app/
17+
RUN cargo chef prepare --recipe-path recipe.json
18+
19+
# Build dependencies based on the recipe of the previous stage, then copy the source and build the project
20+
FROM base AS build
21+
COPY --from=planner /app/recipe.json recipe.json
22+
RUN cargo chef cook -p memory-testing --release --recipe-path recipe.json
23+
24+
# Copy the source here to reuse the cached dependencies
25+
COPY . /app/
3626
RUN cargo build -p memory-testing --release
3727

3828
###############################################
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
*
2-
!crates/*
3-
!Cargo.toml
4-
!Cargo.lock
2+
!/crates/*
3+
!/bitwarden_license/*
4+
!/Cargo.toml
5+
!/Cargo.lock
6+
!/rust-toolchain.toml

0 commit comments

Comments
 (0)