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 <lucien@phundrak.com>
This commit is contained in:
Lucien Cartier-Tilet 2023-02-09 11:59:39 +01:00
parent fd71b959f1
commit 8e925b884e
Signed by: phundrak
GPG Key ID: BD7789E705CB8DCA
3 changed files with 16 additions and 3 deletions

View File

@ -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"

View File

@ -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" ]

1
dummy.rs Normal file
View File

@ -0,0 +1 @@
fn main() {}