From 43575e9918bd2c5d0dea076d52f82d4f31b42bb1 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 17 Oct 2023 10:27:41 +1100 Subject: [PATCH] refactor: use `cargo chef` for `rust-libp2p-server` docker build Using `cargo chef` allows us to significantly speed up the build of docker images in CI as we can make use of `cache-{to,from}` to push the created cache layers to an S3 bucket. Pull-Request: #4605. --- .github/workflows/docker-image.yml | 5 +++++ misc/server/Dockerfile | 29 +++++++++++++++-------------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index 322fb255b1c..b9cd82897c2 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -35,5 +35,10 @@ jobs: context: . file: ./misc/server/Dockerfile push: ${{ ! github.event.pull_request.head.repo.fork && github.actor != 'dependabot[bot]' }} # Only push image if we have the required permissions, i.e. not running from a fork + cache-from: ${{ ! github.event.pull_request.head.repo.fork && github.actor != 'dependabot[bot]' && type=s3,mode=max,bucket=libp2p-by-tf-aws-bootstrap,region=us-east-1,prefix=buildCache,name=rust-libp2p-server }} + cache-to: ${{ ! github.event.pull_request.head.repo.fork && github.actor != 'dependabot[bot]' && type=s3,mode=max,bucket=libp2p-by-tf-aws-bootstrap,region=us-east-1,prefix=buildCache,name=rust-libp2p-server }} tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} + env: + AWS_ACCESS_KEY_ID: ${{ vars.TEST_PLANS_BUILD_CACHE_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.TEST_PLANS_BUILD_CACHE_KEY }} diff --git a/misc/server/Dockerfile b/misc/server/Dockerfile index 72641cc3b2e..90e504f4b11 100644 --- a/misc/server/Dockerfile +++ b/misc/server/Dockerfile @@ -1,19 +1,20 @@ -FROM rust:1.72-bullseye as builder -WORKDIR /usr/src/rust-libp2p-server +# syntax=docker/dockerfile:1.5-labs +FROM rust:1.67.0 as chef +RUN wget -q -O- https://github.com/LukeMathWalker/cargo-chef/releases/download/v0.1.62/cargo-chef-x86_64-unknown-linux-gnu.tar.gz | tar -zx -C /usr/local/bin +RUN cargo install --locked --root /usr/local libp2p-lookup --version 0.6.4 +WORKDIR /app -# Run with access to the target cache to speed up builds -WORKDIR /workspace +FROM chef AS planner +COPY . . +RUN cargo chef prepare --recipe-path recipe.json -RUN --mount=type=cache,target=/usr/local/cargo/registry \ - cargo install --locked --root /usr/local libp2p-lookup --version 0.6.4 - -ADD . . -RUN --mount=type=cache,target=./target \ - --mount=type=cache,target=/usr/local/cargo/registry \ - cargo build --release --package libp2p-server - -RUN --mount=type=cache,target=./target \ - mv ./target/release/libp2p-server /usr/local/bin/libp2p-server +FROM chef AS builder +COPY --from=planner /app/recipe.json recipe.json +# Build dependencies - this is the caching Docker layer! +RUN cargo chef cook --release --package libp2p-server --recipe-path recipe.json +# Build application +COPY . . +RUN cargo build --release --package libp2p-server FROM gcr.io/distroless/cc COPY --from=builder /usr/local/bin/libp2p-server /usr/local/bin/libp2p-lookup /usr/local/bin/