use Cargo Chef to use pre-built Rust backend container image in local testing

This commit is contained in:
continuist 2025-09-19 18:34:05 -04:00
parent 13f554ba6d
commit 9086906180
2 changed files with 29 additions and 14 deletions

View file

@ -1,16 +1,31 @@
# Rust testing environment for CI/CD # planner: produce recipe.json
ARG REGISTRY_HOST=localhost FROM rust:1.89-slim AS planner
ARG OWNER_REPO=owner/repo WORKDIR /app
FROM ${REGISTRY_HOST}/${OWNER_REPO}/rust:1.75-slim RUN apt-get update && apt-get install -y --no-install-recommends pkg-config libssl-dev ca-certificates postgresql-client curl && rm -rf /var/lib/apt/lists/*
RUN cargo install --locked cargo-chef
COPY . .
RUN cargo chef prepare --recipe-path recipe.json
# Install additional tools needed for testing # deps: compile only external crates
RUN apt-get update && apt-get install -y \ FROM rust:1.89-slim AS deps
postgresql-client \ WORKDIR /app
curl \ RUN apt-get update && apt-get install -y --no-install-recommends pkg-config libssl-dev ca-certificates postgresql-client curl && rm -rf /var/lib/apt/lists/*
&& rm -rf /var/lib/apt/lists/* RUN cargo install --locked cargo-chef
ENV CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse
COPY --from=planner /app/recipe.json recipe.json
RUN cargo chef cook --release --recipe-path recipe.json
# Set working directory # builder: compile local workspace crates using cached deps
FROM rust:1.89-slim AS builder
WORKDIR /app
COPY --from=deps /usr/local/cargo /usr/local/cargo
COPY --from=deps /usr/local/rustup /usr/local/rustup
COPY --from=deps /app/target /app/target
COPY . .
RUN cargo build --release --locked
# runtime: minimal test environment
FROM rust:1.89-slim AS runtime
RUN apt-get update && apt-get install -y --no-install-recommends postgresql-client curl && rm -rf /var/lib/apt/lists/*
WORKDIR /workspace/backend WORKDIR /workspace/backend
# Keep container running for testing
CMD ["sleep", "infinity"] CMD ["sleep", "infinity"]

View file

@ -17,7 +17,7 @@ spec:
- containerPort: 5432 - containerPort: 5432
- name: backend - name: backend
image: rust:1.89-slim image: localhost/sharenet-test-rust:latest
workingDir: /app/backend workingDir: /app/backend
env: env:
- name: DATABASE_URL - name: DATABASE_URL