This commit is contained in:
Mateusz Gruszczyński committed 2026-11-03 10:38:23 +01:00
1 parent fb5f01d291
commit 7168bc4683
10 files changed
+370 -72

No files matched your search

+57 -37
View File
@@ -2,7 +2,7 @@
set -Eeuo pipefail
# Increment when package payload/installation semantics change without a source commit.
PACKAGING_REVISION=6
PACKAGING_REVISION=7
OPNSENSE_PACKAGING_REVISION=4
FREEBSD_NATIVE_PACKAGING_REVISION=2
@@ -44,40 +44,60 @@ atomic_write() {
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)"
is_true() {
case "${1:-}" in
1|true|TRUE|yes|YES|on|ON) return 0 ;;
*) return 1 ;;
esac
}
require_positive_integer() {
local name=$1 value=$2
[[ "$value" =~ ^[1-9][0-9]*$ ]] || fatal "$name must be a positive integer"
}
# Keep the newest N artifacts matching a package family. An optional exclude
# glob is useful for repositories that contain both rustproxy-* and
# rustproxy-docker-agent-* files in the same directory.
prune_package_files() {
local directory=$1 pattern=$2 keep=$3 exclude_pattern=${4:-}
require_positive_integer PACKAGE_RETENTION "$keep"
[[ -d "$directory" ]] || return 0
local -a find_args=("$directory" -maxdepth 1 -type f -name "$pattern")
if [[ -n "$exclude_pattern" ]]; then
find_args+=( ! -name "$exclude_pattern" )
fi
mapfile -t stale < <(
find "${find_args[@]}" -printf '%T@ %p\n' \
| sort -nr \
| awk -v keep="$keep" 'NR>keep {$1=""; sub(/^ /,""); print}'
)
local path
for path in "${stale[@]}"; do
[[ -n "$path" ]] || continue
log "package retention: removing $path"
rm -f -- "$path"
done
}
prune_release_dirs() {
local releases_dir=$1 keep=$2 protected=${3:-}
require_positive_integer RELEASE_RETENTION "$keep"
[[ -d "$releases_dir" ]] || return 0
mapfile -t stale < <(
find "$releases_dir" -mindepth 1 -maxdepth 1 -type d -printf '%T@ %p\n' \
| sort -nr \
| awk -v keep="$keep" 'NR>keep {$1=""; sub(/^ /,""); print}'
)
local path
for path in "${stale[@]}"; do
[[ -n "$path" && "$path" != "$protected" ]] || continue
log "release retention: removing $path"
rm -rf -- "$path"
done
}