char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Georgi Gerganovci : apply ccache-clear with older/min/dry-run to all ccache jobs (#27602)985b14912

char/aliceai-t5-moe
3.8 KiB105 linesraw
1#!/bin/bash
2# 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 deleted
8#   --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 them
10#
11# Env (when running in GitHub Actions):
12#   GH_TOKEN:          token for the gh CLI
13#   GITHUB_REPOSITORY: owner/repo of the caches to manage
14set -euo pipefail
15
16KEY=""
17OLDER=""
18MIN=0
19DRY_RUN=false
20while [[ $# -gt 0 ]]; do
21    case "$1" in
22        --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    esac
28done
29
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 seconds
36to_seconds() {
37  local val="$1"
38  [[ "$val" =~ ^[0-9]+$ ]] && { echo "$val"; return 0; }
39  local num="${val%?}" unit="${val: -1}" mult
40  [[ "$num" =~ ^[0-9]+$ ]] || return 1
41  case "$unit" in
42    s) mult=1 ;;
43    m) mult=60 ;;
44    h) mult=3600 ;;
45    d) mult=86400 ;;
46    *) return 1 ;;
47  esac
48  echo $((num * mult))
49}
50
51# Convert an ISO-8601 UTC timestamp (e.g. 2026-08-23T16:51:23.313693Z) to epoch seconds
52to_epoch() {
53  local val="$1" out
54  # GNU date (e.g. Linux)
55  if out=$(date -d "$val" +%s 2>/dev/null) && [[ "$out" =~ ^[0-9]+$ ]]; then
56    echo "$out"
57    return 0
58  fi
59  # BSD date (e.g. macOS); fractional seconds are not needed, TZ forces UTC
60  out=$(TZ=UTC date -j -f "%Y-%m-%dT%H:%M:%S" "${val:0:19}" +%s 2>/dev/null) || return 1
61  [[ "$out" =~ ^[0-9]+$ ]] || return 1
62  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" ]]; then
67    echo "No caches found with key prefix: $KEY"
68    exit 0
69fi
70
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; do
75    printf '  %s  %s  %s\n' "$CREATED" "$ID" "$CACHE_KEY"
76done <<< "$CACHES"
77
78CUTOFF=""
79if [[ -n "$OLDER" ]]; then
80    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 ))
82fi
83
84# Caches are sorted oldest first
85DELETED=0
86while IFS=$'\t' read -r CREATED ID CACHE_KEY; do
87    if [[ -n "$CUTOFF" ]]; then
88        CREATED_SECONDS=$(to_epoch "$CREATED") || { echo "Failed to parse date: $CREATED" >&2; exit 1; }
89        if [[ "$CREATED_SECONDS" -ge "$CUTOFF" ]]; then
90            echo "Rest are not older than $OLDER, stopping"
91            break
92        fi
93    fi
94    if (( TOTAL - DELETED - 1 < MIN )); then
95        echo "Keeping at least $MIN cache(s), stopping"
96        break
97    fi
98    if [[ "$DRY_RUN" == "true" ]]; then
99        echo "Would delete cache: $ID ($CACHE_KEY)"
100    else
101        echo "Deleting cache: $ID ($CACHE_KEY)"
102        gh cache delete --repo "$GITHUB_REPOSITORY" "$ID"
103    fi
104    DELETED=$((DELETED + 1))
105done <<< "$CACHES"