From 495c09975abbeb0c98a35cb37956a6bc4b186748 Mon Sep 17 00:00:00 2001 From: avalonche Date: Wed, 11 Dec 2024 11:20:49 +1100 Subject: [PATCH] Docker build time in CI --- .github/workflows/release.yml | 31 ++++++--------- Dockerfile | 73 ++++++++++++++++++++++++++--------- 2 files changed, 66 insertions(+), 38 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 15dda34..39a87f7 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -37,36 +37,27 @@ jobs: type=pep440,pattern={{major}}.{{minor}} type=raw,value=latest,enable=${{ !contains(env.RELEASE_VERSION, '-') }} - # https://github.com/WarpBuilds/rust-cache - - name: Run WarpBuilds/rust-cache - uses: WarpBuilds/rust-cache@v2 - with: - cache-on-failure: true - - # https://github.com/Mozilla-Actions/sccache-action - - name: Setup sccache-action - uses: mozilla-actions/sccache-action@v0.0.5 - - - name: Set env vars - run: | - echo "SCCACHE_GHA_ENABLED=true" >> $GITHUB_ENV - echo "RUSTC_WRAPPER=sccache" >> $GITHUB_ENV - - name: Set up QEMU - uses: docker/setup-qemu-action@v2 + uses: docker/setup-qemu-action@v3 - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v2 + uses: docker/setup-buildx-action@v3 - name: Login to DockerHub - uses: docker/login-action@v2 + uses: docker/login-action@v3 with: username: ${{ secrets.FLASHBOTS_DOCKERHUB_USERNAME }} password: ${{ secrets.FLASHBOTS_DOCKERHUB_TOKEN }} - name: Build and push - uses: docker/build-push-action@v3 + uses: docker/build-push-action@v5 with: + cache-from: | + type=gha,scope=amd64 + type=gha,scope=arm64 + cache-to: | + type=gha,mode=max,scope=amd64 + type=gha,mode=max,scope=arm64 context: . push: true build-args: | @@ -79,7 +70,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout sources - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Create release id: create_release diff --git a/Dockerfile b/Dockerfile index c2b4446..10b34d2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,27 +1,64 @@ -FROM lukemathwalker/cargo-chef:latest AS chef +# +# Base container (with sccache and cargo-chef) +# +# - https://github.com/mozilla/sccache +# - https://github.com/LukeMathWalker/cargo-chef +# +# Based on https://depot.dev/blog/rust-dockerfile-best-practices +# +FROM rust:1.82 as base + +ARG FEATURES + +RUN cargo install sccache --version ^0.8 +RUN cargo install cargo-chef --version ^0.1 + +RUN apt-get update \ + && apt-get install -y clang libclang-dev + +ENV CARGO_HOME=/usr/local/cargo +ENV RUSTC_WRAPPER=sccache +ENV SCCACHE_DIR=/sccache + +# +# Planner container (running "cargo chef prepare") +# +FROM base AS planner WORKDIR /app -# Prepare build plan -FROM chef AS planner -COPY ./Cargo.toml ./Cargo.lock ./ -COPY ./src ./src -RUN cargo chef prepare +COPY . . -# Build application -FROM chef AS builder +RUN --mount=type=cache,target=/usr/local/cargo/registry \ + --mount=type=cache,target=/usr/local/cargo/git \ + --mount=type=cache,target=$SCCACHE_DIR,sharing=locked \ + cargo chef prepare --recipe-path recipe.json -# Install system dependencies -RUN apt-get update && \ - apt-get install -y openssl libclang-dev libssl3 && \ - apt-get clean && \ - rm -rf /var/lib/apt/lists/* +# +# Builder container (running "cargo chef cook" and "cargo build --release") +# +FROM base as builder +WORKDIR /app +# Default binary filename +ARG ROLLUP_BOOST_BIN="rollup-boost" +COPY --from=planner /app/recipe.json recipe.json + +RUN --mount=type=cache,target=$SCCACHE_DIR,sharing=locked \ + cargo chef cook --release --recipe-path recipe.json -COPY --from=planner /app/recipe.json . -RUN cargo chef cook --release COPY . . -RUN cargo build --release -FROM debian:bullseye-slim AS final -COPY --from=builder /app/target/release/rollup-boost /usr/local/bin/ +RUN --mount=type=cache,target=/usr/local/cargo/registry \ + --mount=type=cache,target=/usr/local/cargo/git \ + --mount=type=cache,target=$SCCACHE_DIR,sharing=locked \ + cargo build --release --features="$FEATURES" --package=${ROLLUP_BOOST_BIN} + +# +# Runtime container +# +FROM gcr.io/distroless/cc-debian12 +WORKDIR /app + +ARG ROLLUP_BOOST_BIN="rollup-boost" +COPY --from=builder /app/target/release/${ROLLUP_BOOST_BIN} /usr/local/bin/ ENTRYPOINT ["/usr/local/bin/rollup-boost"] \ No newline at end of file