|
1 | 1 | ###############################################
|
2 | 2 | # Build stage #
|
3 | 3 | ###############################################
|
4 |
| -FROM rust:1.85 AS build |
5 |
| - |
| 4 | +FROM rust:1.85 AS base |
6 | 5 | WORKDIR /app
|
7 | 6 |
|
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/ |
36 | 26 | RUN cargo build -p memory-testing --release
|
37 | 27 |
|
38 | 28 | ###############################################
|
|
0 commit comments