litellm/.github/actions/cache-cargo-build/action.yml
Kerry Lu b0c734ff6e
ci: reuse Rust bridge builds across PR pushes and refresh the Cargo cache (#52718)
* Reuse PR Rust bridges and refresh main Cargo caches

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* Cache gateway prebuilts on pull requests

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-11-19 01:38:54 +00:00

63 lines
2.8 KiB
YAML

name: "Cache the Rust build"
description: >-
Cache the Cargo registry and target directory the root package's build needs,
so unchanged production inputs reuse a build and source changes refresh it.
litellm builds through maturin, which compiles litellm/rust/crates/python-bridge
in the dev profile for editable installs. `uv sync` therefore pays a full build
in every job that installs the workspace. Nothing caught it, because the uv cache
holds wheels uv downloads rather than wheels it builds, and a path dependency
whose source moves every commit could never hit that cache anyway. Cargo rebuilds
only what changed when its target directory survives, so a warm job pays for the
bridge crate alone.
The key namespace is separate from test-rust.yml's check and release caches. They
cache the same directory for different workloads, and a shared key would let
whichever ran first deny the others a save.
inputs:
profile:
description: "Cargo profile the build uses (dev or release)"
required: false
default: "dev"
runs:
using: composite
steps:
- name: Define the Cargo cache key
id: key
shell: bash
env:
PREFIX: ${{ runner.os }}-maturin-${{ inputs.profile }}-v2-${{ hashFiles('litellm/rust/rust-toolchain.toml') }}
LOCK: ${{ hashFiles('litellm/rust/Cargo.lock') }}
SOURCE: ${{ hashFiles('litellm/rust/Cargo.toml', 'litellm/rust/.cargo/config.toml', 'litellm/rust/crates/*/Cargo.toml', 'litellm/rust/crates/*/src/**', 'litellm/rust/crates/*/build.rs', 'litellm/rust/crates/python-bridge/preflight_contract.json', 'litellm/rust/crates/python-compat/generated/nonprintable.rs', 'litellm/rust/crates/*/.sqlx/*.json', 'litellm/rust/crates/*/migrations/**', 'model_prices_and_context_window.json', '!**/*.md', '!**/tests/**', '!**/benches/**') }}
run: |
echo "key=${PREFIX}-${LOCK}-${SOURCE}" >> "$GITHUB_OUTPUT"
echo "lock=${PREFIX}-${LOCK}-" >> "$GITHUB_OUTPUT"
echo "toolchain=${PREFIX}-" >> "$GITHUB_OUTPUT"
- name: Restore the Cargo registry and target directory
if: github.ref == 'refs/heads/main'
uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
with:
path: |
~/.cargo/registry
~/.cargo/git
litellm/rust/target
key: ${{ steps.key.outputs.key }}
restore-keys: |
${{ steps.key.outputs.lock }}
${{ steps.key.outputs.toolchain }}
- name: Restore the Cargo registry and target directory
if: github.ref != 'refs/heads/main'
uses: actions/cache/restore@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
with:
path: |
~/.cargo/registry
~/.cargo/git
litellm/rust/target
key: ${{ steps.key.outputs.key }}
restore-keys: |
${{ steps.key.outputs.lock }}
${{ steps.key.outputs.toolchain }}