-
Notifications
You must be signed in to change notification settings - Fork 224
Expand file tree
/
Copy pathDockerfile.cache
More file actions
126 lines (104 loc) · 4.14 KB
/
Dockerfile.cache
File metadata and controls
126 lines (104 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# Copyright 2023 RobustMQ Team
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
# CI Dependency Cache Image
# ==============================================================================
# This Dockerfile uses cargo-chef to pre-compile ALL dependencies for CI.
# It creates a Docker image with all 864 dependencies compiled, so that
# unit tests only need to compile the project code (2-3 minutes vs 25 minutes).
#
# Usage:
# - This image is automatically built when Cargo.lock changes
# - unit-test.yml uses this image to speed up tests
# - Stores compiled dependencies in /usr/local/cargo and /build/target
# ==============================================================================
# ==============================================================================
# Stage 1: Planner - Generate dependency recipe
# ==============================================================================
FROM lukemathwalker/cargo-chef:latest-rust-1.90 AS planner
WORKDIR /build
# Copy all Cargo.toml and Cargo.lock files
COPY Cargo.toml Cargo.lock ./
COPY src/*/Cargo.toml ./src/*/
COPY src/common/*/Cargo.toml ./src/common/*/
COPY tests/Cargo.toml ./tests/
# Auto-create dummy src/lib.rs or src/main.rs for every crate found via Cargo.toml
RUN find . -name "Cargo.toml" -not -path "./Cargo.toml" -exec dirname {} \; | while read dir; do \
mkdir -p "$dir/src"; \
if grep -q '\[\[bin\]\]' "$dir/Cargo.toml" 2>/dev/null; then \
echo 'fn main() {}' > "$dir/src/main.rs"; \
fi; \
echo '' > "$dir/src/lib.rs"; \
done
# Copy protocol build.rs (needed for cargo metadata)
COPY src/protocol/build.rs ./src/protocol/
# Generate recipe.json - this contains the dependency graph
RUN cargo chef prepare --recipe-path recipe.json
# ==============================================================================
# Stage 2: Builder - Compile all dependencies
# ==============================================================================
FROM lukemathwalker/cargo-chef:latest-rust-1.90 AS builder
WORKDIR /build
# Install system dependencies (same as CI environment)
RUN apt-get update && apt-get install -y \
protobuf-compiler \
llvm \
libclang-dev \
cmake \
pkg-config \
libssl-dev \
clang \
lld \
&& rm -rf /var/lib/apt/lists/* \
&& apt-get clean
# Set environment for faster builds
ENV CARGO_BUILD_JOBS=10 \
CARGO_INCREMENTAL=0 \
RUSTC_WRAPPER="" \
CARGO_TERM_COLOR=always
# Copy recipe from planner
COPY --from=planner /build/recipe.json recipe.json
# Build dependencies - this is the expensive part!
# This layer will be cached unless Cargo.lock changes
RUN cargo chef cook --recipe-path recipe.json --tests
# ==============================================================================
# Stage 3: Runtime - Final image with compiled dependencies
# ==============================================================================
FROM rust:1.90.0-bookworm AS runtime
WORKDIR /build
# Install system dependencies (for running cargo build/test)
RUN apt-get update && apt-get install -y \
protobuf-compiler \
llvm \
libclang-dev \
cmake \
pkg-config \
libssl-dev \
clang \
lld \
&& rm -rf /var/lib/apt/lists/* \
&& apt-get clean
# Copy compiled dependencies from builder
COPY --from=builder /usr/local/cargo /usr/local/cargo
COPY --from=builder /build/target /build/target
# Set environment variables
ENV CARGO_BUILD_JOBS=10 \
CARGO_INCREMENTAL=0 \
CARGO_TERM_COLOR=always \
RUSTC_WRAPPER=""
# Verify cargo is working
RUN cargo --version && rustc --version
# Default command
CMD ["/bin/bash"]
# Trigger build