From 8e925b884ef3a5d19c8edff233614962db708c5c Mon Sep 17 00:00:00 2001 From: Lucien Cartier-Tilet Date: Thu, 9 Feb 2023 11:59:39 +0100 Subject: [PATCH] perf(backend): Cache build of dependencies When building the Docker image, first compile the project with a fake main file, namely `dummy.rs`. Once dependencies are built, compile the actual project by switching back to `src/main.rs`. Signed-off-by: Lucien Cartier-Tilet --- Cargo.toml | 4 ++++ Dockerfile | 14 +++++++++++--- dummy.rs | 1 + 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 dummy.rs diff --git a/Cargo.toml b/Cargo.toml index 54856fc..60e668f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,3 +15,7 @@ serde = { version = "1.0", features = ["derive"] } dotenvy = "0.15" gql_client = "1.0.7" human-panic = "1.1.0" + +[[bin]] +name = "phuncache" +path = "src/main.rs" diff --git a/Dockerfile b/Dockerfile index 0f5472c..0d0e283 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,12 +2,20 @@ FROM rust:alpine WORKDIR /app -COPY Cargo.toml /app/Cargo.toml -COPY Cargo.lock /app/Cargo.lock -COPY src /app/src RUN apk update && apk add pkgconfig openssl-dev musl-dev +COPY Cargo.toml . +COPY dummy.rs . + +# Cache building +RUN sed -i 's|src/main.rs|dummy.rs|' Cargo.toml RUN cargo build --release +RUN cargo build +RUN sed -i 's|dummy.rs|src/main.rs|' Cargo.toml + +# Building normally +COPY src src +RUN cargo build RUN cargo install --path . ENTRYPOINT [ "phuncache" ] diff --git a/dummy.rs b/dummy.rs new file mode 100644 index 0000000..f328e4d --- /dev/null +++ b/dummy.rs @@ -0,0 +1 @@ +fn main() {}