Implement suggested changes to make cargo-chef work for local crates
Some checks failed
Podman Rootless Demo / test-backend (push) Failing after 7m53s

This commit is contained in:
continuist 2025-09-19 21:45:56 -04:00
parent 5be5159d88
commit 0790b63dc3
2 changed files with 44 additions and 54 deletions

View file

@ -46,32 +46,35 @@ jobs:
- name: Create network - name: Create network
run: podman --remote network create integ-${{ env.RUN_ID }} run: podman --remote network create integ-${{ env.RUN_ID }}
- name: Check if dependencies changed - name: Generate cache key from lockfile
id: check-deps id: cache-key
run: | run: |
# Change to backend directory and get hash of Cargo.toml and Cargo.lock
cd "$GITHUB_WORKSPACE/backend" cd "$GITHUB_WORKSPACE/backend"
DEPS_HASH=$(sha256sum Cargo.toml Cargo.lock | sha256sum | cut -d' ' -f1) LOCK_HASH=$(sha256sum Cargo.lock | cut -d' ' -f1)
echo "deps_hash=$DEPS_HASH" >> $GITHUB_OUTPUT SHORT_HASH=$(echo "$LOCK_HASH" | cut -c1-12)
echo "cache_key=$SHORT_HASH" >> $GITHUB_OUTPUT
# Check if image exists with this hash tag echo "Using cache key: $SHORT_HASH"
if podman --remote manifest inspect "$PREBUILT_BACKEND_TEST_IMAGE:$DEPS_HASH" >/dev/null 2>&1; then
echo "rebuild_needed=false" >> $GITHUB_OUTPUT
else
echo "rebuild_needed=true" >> $GITHUB_OUTPUT
fi
- name: Build optimized Rust test image (if needed) - name: Build dependencies cache image
if: steps.check-deps.outputs.rebuild_needed == 'true'
run: | run: |
# Build without caching to avoid policy issues # Build deps stage with layer caching
podman --remote build --no-cache \ podman --remote build \
--target deps \
-f backend/Dockerfile.test-rust \ -f backend/Dockerfile.test-rust \
-t "$PREBUILT_BACKEND_TEST_IMAGE:${{ steps.check-deps.outputs.deps_hash }}" \ -t "$PREBUILT_BACKEND_TEST_IMAGE:deps-${{ steps.cache-key.outputs.cache_key }}" \
-t "$PREBUILT_BACKEND_TEST_IMAGE:latest" --layers
podman --remote push "$PREBUILT_BACKEND_TEST_IMAGE:${{ steps.check-deps.outputs.deps_hash }}" podman --remote push "$PREBUILT_BACKEND_TEST_IMAGE:deps-${{ steps.cache-key.outputs.cache_key }}"
podman --remote push "$PREBUILT_BACKEND_TEST_IMAGE:latest"
- name: Build full test image with cached dependencies
run: |
# Build final image using cached deps
podman --remote build \
--target runner \
-f backend/Dockerfile.test-rust \
-t "$PREBUILT_BACKEND_TEST_IMAGE:test-${{ github.sha }}" \
--cache-from "$PREBUILT_BACKEND_TEST_IMAGE:deps-${{ steps.cache-key.outputs.cache_key }}" \
--layers
- name: Start PostgreSQL - name: Start PostgreSQL
run: | run: |
@ -98,28 +101,13 @@ jobs:
-v /home/ci-service/.cache:/c \ -v /home/ci-service/.cache:/c \
alpine:3.20 sh -lc 'mkdir -p /c/cargo' alpine:3.20 sh -lc 'mkdir -p /c/cargo'
- name: Run backend tests with cached dependencies - name: Run backend tests
run: | run: |
set -euo pipefail # Run tests in the pre-built test image
set -o pipefail podman --remote run --rm \
tar --exclude .git --exclude target -C "$GITHUB_WORKSPACE" -cf - . | \
podman --remote run --rm -i \
--network integ-${{ env.RUN_ID }} \ --network integ-${{ env.RUN_ID }} \
-e CARGO_HOME=/cargo \
-e DATABASE_URL=postgres://postgres:password@test-postgres-${{ env.RUN_ID }}:5432/sharenet_test \ -e DATABASE_URL=postgres://postgres:password@test-postgres-${{ env.RUN_ID }}:5432/sharenet_test \
-v /home/ci-service/.cache/cargo:/cargo \ "$PREBUILT_BACKEND_TEST_IMAGE:test-${{ github.sha }}"
"$PREBUILT_BACKEND_TEST_IMAGE:${{ steps.check-deps.outputs.deps_hash }}" \
sh -euxc '
mkdir -p /workspace
tar -x -C /workspace
cd /workspace/backend
# Use pre-built dependencies from the image
export CARGO_TARGET_DIR=/app/target
export CARGO_REGISTRY_DIR=/usr/local/cargo/registry
# Build local crates using pre-built external dependencies
cargo build --release --locked
cargo test --lib -- --test-threads=1
'
- name: Cleanup - name: Cleanup
if: always() if: always()

View file

@ -1,30 +1,32 @@
# planner: produce recipe.json # Base stage with consistent working directory
FROM docker.io/rust:1.89-slim AS planner FROM docker.io/rust:1.89-slim AS base
WORKDIR /app WORKDIR /app
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/*
# Planner: produce recipe.json
FROM base AS planner
RUN apt-get update && apt-get install -y --no-install-recommends pkg-config libssl-dev ca-certificates && rm -rf /var/lib/apt/lists/*
RUN cargo install --locked cargo-chef RUN cargo install --locked cargo-chef
COPY . . COPY . .
RUN cargo chef prepare --recipe-path recipe.json RUN cargo chef prepare --recipe-path recipe.json
# deps: compile only external crates # Deps: compile only external crates
FROM docker.io/rust:1.89-slim AS deps FROM base AS deps
WORKDIR /app RUN apt-get update && apt-get install -y --no-install-recommends pkg-config libssl-dev ca-certificates && rm -rf /var/lib/apt/lists/*
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 RUN cargo install --locked cargo-chef
ENV CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse ENV CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse
COPY --from=planner /app/recipe.json recipe.json COPY --from=planner /app/recipe.json recipe.json
RUN cargo chef cook --release --recipe-path recipe.json RUN cargo chef cook --release --recipe-path recipe.json
# builder: compile local workspace crates using cached deps # Test-build: compile workspace with pre-built dependencies
FROM docker.io/rust:1.89-slim AS builder FROM base AS test-build
WORKDIR /app
COPY --from=deps /usr/local/cargo /usr/local/cargo COPY --from=deps /usr/local/cargo /usr/local/cargo
COPY --from=deps /usr/local/rustup /usr/local/rustup COPY --from=deps /usr/local/rustup /usr/local/rustup
COPY --from=deps /app/target /app/target COPY --from=deps /app/target /app/target
COPY . . COPY . .
RUN cargo build --release --locked RUN cargo test --workspace --locked --no-run
# runtime: minimal test environment - reuse builder stage which already has dependencies # Runner: minimal runtime for test execution
FROM docker.io/rust:1.89-slim AS runtime FROM base AS runner
WORKDIR /workspace/backend COPY --from=test-build /app/target /app/target
CMD ["sleep", "infinity"] WORKDIR /app
CMD ["cargo", "test", "--workspace", "--locked", "--", "--test-threads=1"]