Skip to content

Commit 43575e9

Browse files
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.
1 parent 09f8cb4 commit 43575e9

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

.github/workflows/docker-image.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,10 @@ jobs:
3535
context: .
3636
file: ./misc/server/Dockerfile
3737
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
38+
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 }}
39+
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 }}
3840
tags: ${{ steps.meta.outputs.tags }}
3941
labels: ${{ steps.meta.outputs.labels }}
42+
env:
43+
AWS_ACCESS_KEY_ID: ${{ vars.TEST_PLANS_BUILD_CACHE_KEY_ID }}
44+
AWS_SECRET_ACCESS_KEY: ${{ secrets.TEST_PLANS_BUILD_CACHE_KEY }}

misc/server/Dockerfile

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
FROM rust:1.72-bullseye as builder
2-
WORKDIR /usr/src/rust-libp2p-server
1+
# syntax=docker/dockerfile:1.5-labs
2+
FROM rust:1.67.0 as chef
3+
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
4+
RUN cargo install --locked --root /usr/local libp2p-lookup --version 0.6.4
5+
WORKDIR /app
36

4-
# Run with access to the target cache to speed up builds
5-
WORKDIR /workspace
7+
FROM chef AS planner
8+
COPY . .
9+
RUN cargo chef prepare --recipe-path recipe.json
610

7-
RUN --mount=type=cache,target=/usr/local/cargo/registry \
8-
cargo install --locked --root /usr/local libp2p-lookup --version 0.6.4
9-
10-
ADD . .
11-
RUN --mount=type=cache,target=./target \
12-
--mount=type=cache,target=/usr/local/cargo/registry \
13-
cargo build --release --package libp2p-server
14-
15-
RUN --mount=type=cache,target=./target \
16-
mv ./target/release/libp2p-server /usr/local/bin/libp2p-server
11+
FROM chef AS builder
12+
COPY --from=planner /app/recipe.json recipe.json
13+
# Build dependencies - this is the caching Docker layer!
14+
RUN cargo chef cook --release --package libp2p-server --recipe-path recipe.json
15+
# Build application
16+
COPY . .
17+
RUN cargo build --release --package libp2p-server
1718

1819
FROM gcr.io/distroless/cc
1920
COPY --from=builder /usr/local/bin/libp2p-server /usr/local/bin/libp2p-lookup /usr/local/bin/

0 commit comments

Comments
 (0)