#!/usr/bin/env bash # Cleanup of the runner's Docker daemon and of the container registry. The daemon is the same one that builds # Encelado, Mimante and Alberto's other projects (and very probably the one of the Unraid applications), and a real # Febe may run on it too. Since 21 October 2026 (decision of Alberto) the cleanup also removes what nobody needs any # more, of any project: the images no container uses, the build cache, the leftovers of act_runner. Never a # container, a volume or a network that does not belong to the CI. # # Of the Febe CI, what can be proven to be its own is removed. The labels febe.ci.workflow, febe.ci.run, febe.ci.ref # and febe.ci.commit say where an image comes from, but they do NOT choose what to remove: they are also in the # published images (:main, :sha-, :X.Y.Z, :latest), and Docker copies them into every container created from those, # so a real Febe carries them too. What is removed is recognised like this: # - test containers: the label febe.ci.prova=, which only prova-immagine.sh sets with # «docker run --label» and which no image carries (PipelineTests checks it), AND the exact name # febe-prova- (the other test containers, without a name, run with --rm and Docker removes them by # itself as soon as they end); # - tested images: the local name febe-ci:, removed by name, never by id and never with -f. If the same # image also has a registry name, only febe-ci: is removed; if a container uses it, it stays; # - buildx builders: the container buildx_buildkit_febe-ci-0, with the moby/buildkit image, and its # …_state volume, which hold the build cache of that run; # - the febe:ci image left by the old verification workflow (before 20 October 2026), if nobody uses it. # Of act_runner, the leftovers of the jobs are removed: act_runner names everything it creates for a job # GITEA-ACTIONS-TASK-… (the job container, its volumes and -env, its network # --network). Only stopped containers, and volumes and networks no container uses, older than 90 # minutes: a job that starts creates them and uses them at once. The act-toolcache volume (the tool cache, shared # among the jobs) stays. # # pulizia-docker.sh esecuzione everything the run created (at the start and at the end of its # «immagine» job, always) # pulizia-docker.sh penzolanti the unnamed images no container uses and the daemon's dangling build cache # (the part no image shares) unused for 24 hours, of all projects (at the end # of every «immagine» job, after «esecuzione») # pulizia-docker.sh residui [minuti] the leftovers of interrupted runs, older than N minutes (default 90: a job # lasts at most 60 minutes, so none of them is still alive) # pulizia-docker.sh settimanale residui, febe:ci, unnamed images older than 72 hours: the narrow cleanup of # before 21 October 2026, now only by hand # pulizia-docker.sh completa residui, febe:ci, act_runner leftovers, the images no container uses # created more than 24 hours ago, the build cache unused for 24 hours and, # with REGISTRY_TOKEN, the registry's sha- tags beyond the 10 most recent and # the CI artifacts (febe-ci) older than 14 days («pulizia» workflow, every # Monday and by hand) # pulizia-docker.sh --prova lists what it would remove and removes nothing: only commands that read # (on the registry only GET) # # All the arguments are checked before touching Docker: an option other than --prova (--dry-run, -n, --Prova) or an # extra argument (prova without dashes) stops everything with code 2, instead of starting the real cleanup in place # of the dry run. # # Never docker system prune, container prune, volume prune or network prune; never image prune -a or builder prune -a # without the 24-hour filter; never a container, a volume or a network that does not belong to the CI. The images are # removed by Docker itself (image prune), which never removes an image used by a container, not even a stopped one; # with -a, though, it removes their extra names, and the script writes them out (in the dry run it estimates them, # depending on the store). On the registry never main, latest or an X.Y.Z version: only the sha- tags of # the verification and the -ci. and -rilascio. artifacts of the generic febe-ci package. # # In the «residui», «settimanale» and «completa» modes a test container is removed only if it is stopped. One still # running after 90 minutes (its job was killed right during the container test) stays where it is: it is reported, # with the command to remove it by hand. The builders are the exception: they are always running and their cache goes # away only by removing the container; they are recognised by the exact name and the buildkit image, never by a label. # # An object already gone is not an error: the cleanup can be repeated. In the «esecuzione», «penzolanti» and «residui» # modes a problem becomes a warning (a green job does not turn red because of the cleanup, which the Monday workflow # repeats); in the «settimanale» and «completa» modes it fails the job, so that someone looks at it. The registry is # the exception: a refusal by its API is only a warning, and the following Monday it is tried again. # # Variables for the registry («completa» mode): REGISTRY_TOKEN (token with «package: read and write», never printed), # GITEA_API (the server's API, for example https://gitea.example/api/v1) and PROPRIETARIO (the owner of the febe # package). The choice of the versions to remove lives in versioni-registro.sh, next to this script. set -uo pipefail uso() { [[ -z "${1:-}" ]] || echo "$1" >&2 echo "Uso: $0 esecuzione | penzolanti | residui [minuti] | settimanale | completa [--prova]" >&2 exit 2 } prova=0 argomenti=() for argomento in "$@"; do case "$argomento" in --prova) prova=1 ;; -*) uso "Opzione sconosciuta: «$argomento». L'unica è --prova." ;; *) argomenti+=("$argomento") ;; esac done modo="${argomenti[0]:-}" case "$modo" in esecuzione) ((${#argomenti[@]} == 2)) || uso "Il modo esecuzione vuole il numero dell'esecuzione e nient'altro." ;; residui) ((${#argomenti[@]} <= 2)) || uso "Il modo residui vuole al più i minuti." ;; penzolanti | settimanale | completa) ((${#argomenti[@]} == 1)) || uso "Il modo $modo non vuole altri argomenti: «${argomenti[1]}»." ;; *) uso ;; esac cartella_script="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" problemi=0 # In a dry run: the containers (id and name) and the image names the real cleanup would remove, so that the lists # that follow treat them as already removed (a volume used only by a container that would go away, for example, # would go away too). previsti=() nomi_previsti=() # For the summary: how many objects per kind, in the order they appear, and the space reclaimed by Docker. declare -A conteggi=() tipi=() note=() # The build cache that is pruned is the one of the daemon's builder, «default». docker builder is an alias of docker # buildx when the plugin is there, and buildx works on the current builder: if someone had made the run's builder # current (costruisci-immagine.sh builder does not), the «esecuzione» mode would have just removed it. The other # buildx commands of this script name the builder explicitly. export BUILDX_BUILDER=default avviso() { echo "::warning::$*" problemi=$((problemi + 1)) } # A warning that does not turn the job red: for the registry. segnala() { echo "::warning::$*" } conta() { [[ -n "${conteggi[$1]+x}" ]] || tipi+=("$1") conteggi[$1]=$((${conteggi[$1]:-0} + 1)) } # True if the first word is among the ones that follow. fra() { local cercata="$1" voce shift for voce in "$@"; do [[ "$voce" == "$cercata" ]] && return 0 done return 1 } # Minutes elapsed since an RFC 3339 instant as Docker writes it (Created of a container or an image, CreatedAt of a # volume, {{json .Created}} of a network, in quotes), with or without fractions of a second, with «Z» or with the # offset from universal time (+02:00). If it cannot be read, 0: when in doubt the object is young and stays. eta_minuti() { local creato="${1:-}" secondi creato="${creato//\"/}" [[ "$creato" =~ ^([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2})(\.[0-9]+)?(Z|[+-][0-9]{2}:[0-9]{2})$ ]] \ || { echo 0; return; } secondi=$(date -u -d "${BASH_REMATCH[1]}${BASH_REMATCH[3]}" +%s 2> /dev/null) || { echo 0; return; } echo $((($(date -u +%s) - secondi) / 60)) } durata() { local minuti="$1" if ((minuti >= 2 * 1440)); then echo "$((minuti / 1440)) giorni" elif ((minuti >= 120)); then echo "$((minuti / 60)) ore" else echo "$minuti minuti" fi } # Among the given containers (ids, one per line), those that remain after this cleanup: in a dry run without the ones # the real cleanup would remove. restanti() { local id while read -r id; do [[ -n "$id" ]] || continue fra "$id" "${previsti[@]}" || echo "$id" done <<< "${1:-}" } # The CI test containers, one per line «id name»: the label febe.ci.prova (of the given run, or of any run if the # first argument is empty) and the exact name febe-prova-