Files
gitea/rootfs/usr/local/bin/cleanup-runners
T
casjay ee13ddf242
gitea / release-gitea (push) Successful in 12m4s
🐛 Keep the nested docker clean after CI runs
- rootfs/tmp/etc/act_runner/default_config.yaml: idle_cleanup_interval, workdir_cleanup_age, post_task_script, container.sweep and a health_check that pauses new tasks below 100 GiB free (runner count and capacity unchanged)
- rootfs/usr/local/bin/runner-post-task: new, prunes stopped containers, dangling images, build cache and unused volumes after every task, every unused image when free space is low
- rootfs/usr/local/bin/cleanup-runners: prune age 72h -> 6h; low-space guard uses free MiB on the nested docker root (asked from docker) instead of the percent of a path that did not exist
- rootfs/usr/local/etc/docker/init.d/05-dockerd.sh: start the nested docker from empty data (RUNNER_DOCKER_WIPE_ON_START, default yes) so layers from killed jobs never pile up
2026-11-27 10:59:13 -05:00

103 lines
5.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# shellcheck shell=bash
# - - - - - - - - - - - - - - - - - - - - - - - - -
##@Version : 202610031200-git
# @@Author : Jason Hempstead
# @@Contact : jason@casjaysdev.pro
# @@License : LICENSE.md
# @@ReadME : cleanup-runners --help
# @@Copyright : Copyright: (c) 2026 Jason Hempstead, Casjays Developments
# @@Created : Saturday, Oct 03, 2026 12:00 EDT
# @@File : cleanup-runners
# @@Description : Prune docker and act cache leftovers from act runner jobs
# @@Changelog : New script
# @@TODO : Better documentation
# @@Other :
# @@Resource :
# @@Terminal App : no
# @@sudo/root : no
# @@Template : shell/bash
# - - - - - - - - - - - - - - - - - - - - - - - - -
# shellcheck disable=SC1001,SC1003,SC2001,SC2003,SC2016,SC2031,SC2090,SC2115,SC2120,SC2155,SC2199,SC2229,SC2317,SC2329
# - - - - - - - - - - - - - - - - - - - - - - - - -
VERSION="202610031200-git"
set -eo pipefail
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Settings - all overridable via environment
# Only prune containers/networks/build cache older than this (docker duration, e.g. 72h)
RUNNER_CLEANUP_UNTIL="${RUNNER_CLEANUP_UNTIL:-6h}"
# Remove all unused images (not just dangling) once free space on the nested dockerd's filesystem drops below this many MiB
RUNNER_CLEANUP_MIN_FREE_MB="${RUNNER_CLEANUP_MIN_FREE_MB:-204800}"
# Directory holding the nested dockerd's data, used for the free space check (empty = ask docker)
RUNNER_CLEANUP_DOCKER_DIR="${RUNNER_CLEANUP_DOCKER_DIR:-}"
# Remove cloned actions in act's cache that have not changed in this many days (0 disables)
RUNNER_CLEANUP_ACT_CACHE_DAYS="${RUNNER_CLEANUP_ACT_CACHE_DAYS:-7}"
# Space separated list of act action cache directories
RUNNER_CLEANUP_ACT_CACHE_DIRS="${RUNNER_CLEANUP_ACT_CACHE_DIRS:-$HOME/.cache/act /root/.cache/act}"
RUNNER_CLEANUP_LOG_FILE="${RUNNER_CLEANUP_LOG_FILE:-/data/logs/act_runner/cleanup.log}"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
__log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" >>"$RUNNER_CLEANUP_LOG_FILE" 2>&1; }
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Run a prune command, log its reclaimed-space line; a failure is logged, never fatal
__prune() {
local output=""
if output="$("$@" 2>&1)"; then
__log "$* :: $(printf '%s\n' "$output" | grep -i -- 'reclaimed' | tail -n1)"
else
__log "FAILED: $* :: $output"
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
printf '%s\n' "Usage: cleanup-runners [--help|--version]" \
"Prunes docker containers, volumes, networks, build cache and images left by act runner jobs." \
"Env: RUNNER_CLEANUP_UNTIL RUNNER_CLEANUP_MIN_FREE_MB RUNNER_CLEANUP_DOCKER_DIR" \
" RUNNER_CLEANUP_ACT_CACHE_DAYS RUNNER_CLEANUP_ACT_CACHE_DIRS RUNNER_CLEANUP_LOG_FILE"
exit 0
elif [ "$1" = "--version" ]; then
echo "$VERSION"
exit 0
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
mkdir -p "$(dirname -- "$RUNNER_CLEANUP_LOG_FILE")"
if ! docker info >/dev/null 2>&1; then
__log "docker is not reachable, skipping docker cleanup"
else
__log "Starting cleanup"
# Exited job containers left behind when a runner died mid-job (running containers are never touched)
__prune docker container prune -f --filter "until=$RUNNER_CLEANUP_UNTIL"
# Job networks that no container uses anymore
__prune docker network prune -f --filter "until=$RUNNER_CLEANUP_UNTIL"
# Unused act-* job volumes; docker recreates a named volume if a starting job needs it
act_volumes="$(docker volume ls -q --filter dangling=true --filter name=act- 2>/dev/null || true)"
if [ -n "$act_volumes" ]; then
# shellcheck disable=SC2086
__prune docker volume rm $act_volumes
fi
# Anonymous volumes no container uses
__prune docker volume prune -f
# Build cache from jobs that run docker build
__prune docker builder prune -af --filter "until=$RUNNER_CLEANUP_UNTIL"
# Untagged (dangling) images
__prune docker image prune -f
# Every image not used by a container, only when free space is getting low; jobs re-pull what they need
docker_dir="${RUNNER_CLEANUP_DOCKER_DIR:-$(docker info -f '{{.DockerRootDir}}' 2>/dev/null || true)}"
free_mb="$(df -Pm "${docker_dir:-/var/lib/docker}" 2>/dev/null | awk 'NR==2 {print $4}' || true)"
if [[ "$free_mb" =~ ^[0-9]+$ ]] && [ "$free_mb" -lt "$RUNNER_CLEANUP_MIN_FREE_MB" ]; then
__log "${docker_dir:-/var/lib/docker} has ${free_mb} MiB free (limit ${RUNNER_CLEANUP_MIN_FREE_MB}), removing unused images"
__prune docker image prune -af
fi
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Cloned actions (one directory per action@ref) that have not been updated recently
if [[ "$RUNNER_CLEANUP_ACT_CACHE_DAYS" =~ ^[1-9][0-9]*$ ]]; then
for act_cache_dir in $RUNNER_CLEANUP_ACT_CACHE_DIRS; do
[ -d "$act_cache_dir" ] || continue
removed="$(find "$act_cache_dir" -mindepth 1 -maxdepth 1 -mtime +"$RUNNER_CLEANUP_ACT_CACHE_DAYS" 2>/dev/null | wc -l || true)"
find "$act_cache_dir" -mindepth 1 -maxdepth 1 -mtime +"$RUNNER_CLEANUP_ACT_CACHE_DAYS" -exec rm -rf {} + 2>/dev/null || true
__log "Removed $removed stale entries from $act_cache_dir"
done
fi
__log "Cleanup finished"
exit 0