Felipe97/llama-cpp-compiled
01.2k
1#!/bin/bash2# Delete GitHub Actions caches matching a key prefix, oldest first.3#4# Usage: ccache-clear.sh --key KEY [--older DURATION] [--min N] [--dry-run]5# --key: cache key prefix to match and delete (without the ccache- prefix)6# --older: only delete caches created more than DURATION ago (e.g. 5m, 1h, 1d);7# by default all matching caches are deleted8# --min: stop deleting if fewer than N caches would remain (default: 0)9# --dry-run: only print the caches that would be deleted, without deleting them10#11# Env (when running in GitHub Actions):12# GH_TOKEN: token for the gh CLI13# GITHUB_REPOSITORY: owner/repo of the caches to manage14set -euo pipefail15 16KEY=""17OLDER=""18MIN=019DRY_RUN=false20while [[ $# -gt 0 ]]; do21 case "$1" in22 --key) [[ $# -ge 2 ]] || { echo "Missing value for $1" >&2; exit 1; }; KEY="$2"; shift 2 ;;23 --older) [[ $# -ge 2 ]] || { echo "Missing value for $1" >&2; exit 1; }; OLDER="$2"; shift 2 ;;24 --min) [[ $# -ge 2 ]] || { echo "Missing value for $1" >&2; exit 1; }; MIN="$2"; shift 2 ;;25 --dry-run) DRY_RUN=true; shift ;;26 *) echo "Unknown argument: $1"; exit 1 ;;27 esac28done29 30command -v gh >/dev/null 2>&1 || { echo "Error: GitHub CLI (gh) is required" >&2; exit 1; }31[[ -n "${GITHUB_REPOSITORY:-}" ]] || { echo "Error: GITHUB_REPOSITORY not set" >&2; exit 1; }32[[ -n "$KEY" ]] || { echo "Error: --key is required" >&2; exit 1; }33[[ "$MIN" =~ ^[0-9]+$ ]] || { echo "Invalid min value: $MIN" >&2; exit 1; }34 35# Convert a duration (e.g. 90m, 1h, 1d, plain seconds) to seconds36to_seconds() {37 local val="$1"38 [[ "$val" =~ ^[0-9]+$ ]] && { echo "$val"; return 0; }39 local num="${val%?}" unit="${val: -1}" mult40 [[ "$num" =~ ^[0-9]+$ ]] || return 141 case "$unit" in42 s) mult=1 ;;43 m) mult=60 ;;44 h) mult=3600 ;;45 d) mult=86400 ;;46 *) return 1 ;;47 esac48 echo $((num * mult))49}50 51# Convert an ISO-8601 UTC timestamp (e.g. 2026-08-23T16:51:23.313693Z) to epoch seconds52to_epoch() {53 local val="$1" out54 # GNU date (e.g. Linux)55 if out=$(date -d "$val" +%s 2>/dev/null) && [[ "$out" =~ ^[0-9]+$ ]]; then56 echo "$out"57 return 058 fi59 # BSD date (e.g. macOS); fractional seconds are not needed, TZ forces UTC60 out=$(TZ=UTC date -j -f "%Y-%m-%dT%H:%M:%S" "${val:0:19}" +%s 2>/dev/null) || return 161 [[ "$out" =~ ^[0-9]+$ ]] || return 162 echo "$out"63}64 65CACHES=$(gh cache list --repo "$GITHUB_REPOSITORY" --key "ccache-$KEY" --json id,key,createdAt --jq '.[] | [.createdAt, .id, .key] | @tsv' | LC_ALL=C sort)66if [[ -z "$CACHES" ]]; then67 echo "No caches found with key prefix: $KEY"68 exit 069fi70 71TOTAL=$(( $(wc -l <<< "$CACHES") ))72 73echo "Found $TOTAL cache(s) with key prefix: $KEY (oldest first):"74while IFS=$'\t' read -r CREATED ID CACHE_KEY; do75 printf ' %s %s %s\n' "$CREATED" "$ID" "$CACHE_KEY"76done <<< "$CACHES"77 78CUTOFF=""79if [[ -n "$OLDER" ]]; then80 OLDER_SECONDS=$(to_seconds "$OLDER") || { echo "Invalid older value: $OLDER (expected e.g. 90m, 1h, 1d)" >&2; exit 1; }81 CUTOFF=$(( $(date +%s) - OLDER_SECONDS ))82fi83 84# Caches are sorted oldest first85DELETED=086while IFS=$'\t' read -r CREATED ID CACHE_KEY; do87 if [[ -n "$CUTOFF" ]]; then88 CREATED_SECONDS=$(to_epoch "$CREATED") || { echo "Failed to parse date: $CREATED" >&2; exit 1; }89 if [[ "$CREATED_SECONDS" -ge "$CUTOFF" ]]; then90 echo "Rest are not older than $OLDER, stopping"91 break92 fi93 fi94 if (( TOTAL - DELETED - 1 < MIN )); then95 echo "Keeping at least $MIN cache(s), stopping"96 break97 fi98 if [[ "$DRY_RUN" == "true" ]]; then99 echo "Would delete cache: $ID ($CACHE_KEY)"100 else101 echo "Deleting cache: $ID ($CACHE_KEY)"102 gh cache delete --repo "$GITHUB_REPOSITORY" "$ID"103 fi104 DELETED=$((DELETED + 1))105done <<< "$CACHES"106 