This commit is contained in:
Mateusz Gruszczyński committed 2026-10-14 13:13:32 +02:00
1 parent af78f6528c
commit 910bb563f0
3 files changed
+53

No files matched your search

+38
View File
@@ -42,3 +42,41 @@ atomic_write() {
cat > "$tmp"
mv -f "$tmp" "$path"
}
# Keep Cargo's release dependency cache so subsequent builds remain incremental.
# Run only after a branch has successfully published its packages.
cleanup_cargo_target() {
[[ "${CLEAN_TARGET_AFTER_BUILD:-1}" == "1" ]] || return 0
local target="${CARGO_TARGET_DIR:-}"
local expected="${DATA_DIR:-}/cargo/target"
# Never recursively delete an arbitrary path provided via environment.
if [[ -z "$target" || "$target" != "$expected" || ! -d "$target" || -L "$target" ]]; then
log "Cargo target cleanup skipped: unexpected target directory"
return 0
fi
local days="${TARGET_CACHE_MAX_AGE_DAYS:-30}"
if [[ ! "$days" =~ ^[0-9]+$ ]] || (( days < 1 )); then
log "Cargo target cleanup skipped: invalid TARGET_CACHE_MAX_AGE_DAYS=$days"
return 0
fi
local before after
before=$(du -sk "$target" 2>/dev/null | awk '{print $1}')
# Release dependencies, fingerprints and build scripts remain cached.
# Rust incremental data is disposable; debug output is unused by release builds.
find "$target" -type d -name incremental -prune -exec rm -rf -- {} + 2>/dev/null || true
if [[ -d "$target/debug" && ! -L "$target/debug" ]]; then
rm -rf -- "$target/debug" || true
fi
# Expire old compiled dependency files gradually, not the entire target.
# Fresh build outputs stay available for subsequent Cargo invocations.
find "$target/release/deps" "$target/x86_64-unknown-freebsd/release/deps" \
-maxdepth 1 -type f -mtime "+$days" -delete 2>/dev/null || true
after=$(du -sk "$target" 2>/dev/null | awk '{print $1}')
log "Cargo target cleanup: reclaimed $(( ${before:-0} - ${after:-0} )) KiB (cache retained)"
}