diff --git a/rootfs/tmp/etc/act_runner/default_config.yaml b/rootfs/tmp/etc/act_runner/default_config.yaml index 265db74..19bbc02 100644 --- a/rootfs/tmp/etc/act_runner/default_config.yaml +++ b/rootfs/tmp/etc/act_runner/default_config.yaml @@ -19,6 +19,13 @@ runner: env_file: .env # The timeout for a job to be finished. timeout: 3h + # While idle, remove per-job networks and volumes of jobs this runner did not live to tear down + idle_cleanup_interval: 10m + # While idle, remove stale workdirs older than this + workdir_cleanup_age: 24h + # Prune images, build cache, containers and volumes left in the nested docker after every task + post_task_script: /usr/local/bin/runner-post-task + post_task_script_timeout: 2m # Whether skip verifying the TLS certificate of the Gitea instance. insecure: true # The timeout for fetching the job from the Gitea instance. @@ -51,6 +58,8 @@ runner: #- 'linux:host,ubuntu-latest:docker://catthehacker/ubuntu:full-latest' container: + # Remove containers, networks and volumes a job creates through its docker proxy when the job ends + sweep: true # Specifies the network to which the container will connect. network: 'bridge' # Whether to use privileged mode or not when launching task containers (privileged mode is required for Docker-in-Docker). @@ -69,3 +78,10 @@ cache: enabled: true external_server: 'http://REPLACE_RUNNER_CACHE_HOST:REPLACE_RUNNER_CACHE_PORT/' external_secret: 'REPLACE_RUNNER_CACHE_SECRET' + +health_check: + # Pause fetching new tasks while free disk space is low instead of filling the disk + enabled: true + min_free_disk_space_mb: 102400 + interval: 30s + timeout: 10s diff --git a/rootfs/usr/local/bin/cleanup-runners b/rootfs/usr/local/bin/cleanup-runners index bb5c55c..5cfc190 100755 --- a/rootfs/usr/local/bin/cleanup-runners +++ b/rootfs/usr/local/bin/cleanup-runners @@ -25,11 +25,11 @@ 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:-72h}" -# Remove all unused images (not just dangling) once the docker data dir reaches this usage percent -RUNNER_CLEANUP_DISK_PERCENT="${RUNNER_CLEANUP_DISK_PERCENT:-80}" -# Directory holding the nested dockerd's data, used for the disk usage check -RUNNER_CLEANUP_DOCKER_DIR="${RUNNER_CLEANUP_DOCKER_DIR:-/data/docker}" +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 @@ -48,15 +48,10 @@ __prune() { fi } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -# Print the usage percent of the filesystem holding $1 -__disk_percent() { - df -P "$1" 2>/dev/null | awk 'NR==2 {gsub("%", "", $5); print $5}' -} -# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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_DISK_PERCENT RUNNER_CLEANUP_DOCKER_DIR" \ + "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 @@ -85,10 +80,11 @@ else __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 disk is getting full; jobs re-pull what they need - disk_used="$(__disk_percent "$RUNNER_CLEANUP_DOCKER_DIR" || true)" - if [[ "$disk_used" =~ ^[0-9]+$ ]] && [ "$disk_used" -ge "$RUNNER_CLEANUP_DISK_PERCENT" ]; then - __log "$RUNNER_CLEANUP_DOCKER_DIR is ${disk_used}% used (limit ${RUNNER_CLEANUP_DISK_PERCENT}%), removing unused images" + # 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 diff --git a/rootfs/usr/local/bin/runner-post-task b/rootfs/usr/local/bin/runner-post-task new file mode 100755 index 0000000..3a873ad --- /dev/null +++ b/rootfs/usr/local/bin/runner-post-task @@ -0,0 +1,57 @@ +#!/usr/bin/env bash +# shellcheck shell=bash +# - - - - - - - - - - - - - - - - - - - - - - - - - +##@Version : 202611271200-git +# @@Author : Jason Hempstead +# @@Contact : jason@casjaysdev.pro +# @@License : LICENSE.md +# @@ReadME : runner-post-task --help +# @@Copyright : Copyright: (c) 2026 Jason Hempstead, Casjays Developments +# @@Created : Friday, Nov 27, 2026 12:00 EST +# @@File : runner-post-task +# @@Description : Prune nested docker leftovers after every act runner task +# @@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="202611271200-git" +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +# Settings - all overridable via environment +# Free space (MiB) on the nested docker's filesystem below which every unused image is removed too +RUNNER_CLEANUP_MIN_FREE_MB="${RUNNER_CLEANUP_MIN_FREE_MB:-204800}" +RUNNER_CLEANUP_LOG_FILE="${RUNNER_CLEANUP_LOG_FILE:-/data/logs/act_runner/cleanup.log}" +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +__log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] post-task: $*" >>"$RUNNER_CLEANUP_LOG_FILE" 2>&1; } +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then + printf '%s\n' "Usage: runner-post-task [--help|--version]" \ + "Runs after every act runner task: prunes stopped containers, dangling images, build cache and unused volumes" \ + "of the nested docker, and every unused image when free space is low." \ + "Env: RUNNER_CLEANUP_MIN_FREE_MB RUNNER_CLEANUP_LOG_FILE" + exit 0 +elif [ "$1" = "--version" ]; then + echo "$VERSION" + exit 0 +fi +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +mkdir -p "$(dirname -- "$RUNNER_CLEANUP_LOG_FILE")" +# Never fail the runner: this is additive housekeeping and a non-zero exit only logs a warning +docker info >/dev/null 2>&1 || exit 0 +# Containers other jobs are still using are never touched; only stopped ones older than 30 minutes go +docker container prune -f --filter "until=30m" >/dev/null 2>&1 +docker image prune -f >/dev/null 2>&1 +docker builder prune -f --filter "until=1h" >/dev/null 2>&1 +docker volume prune -f >/dev/null 2>&1 +docker_root="$(docker info -f '{{.DockerRootDir}}' 2>/dev/null)" +free_mb="$(df -Pm "${docker_root:-/var/lib/docker}" 2>/dev/null | awk 'NR==2 {print $4}')" +if [[ "$free_mb" =~ ^[0-9]+$ ]] && [ "$free_mb" -lt "$RUNNER_CLEANUP_MIN_FREE_MB" ]; then + __log "only ${free_mb} MiB free (limit ${RUNNER_CLEANUP_MIN_FREE_MB}), removing unused images" + docker image prune -af >/dev/null 2>&1 +fi +exit 0 diff --git a/rootfs/usr/local/etc/docker/init.d/05-dockerd.sh b/rootfs/usr/local/etc/docker/init.d/05-dockerd.sh index 051b39c..1a70686 100755 --- a/rootfs/usr/local/etc/docker/init.d/05-dockerd.sh +++ b/rootfs/usr/local/etc/docker/init.d/05-dockerd.sh @@ -391,6 +391,15 @@ __run_pre_execute_checks() { # safe to clear docker's own pidfile here before each start attempt. [ -f "/tmp/docker.pid" ] && rm -f "/tmp/docker.pid" + # The nested docker only holds CI scratch (job layers, build cache, pulled images), so start it + # from empty data: layers left behind by jobs that were killed mid-run never pile up, and with the + # vfs storage driver each of them is a full copy of an image. Set RUNNER_DOCKER_WIPE_ON_START=no to keep it + if [ "${RUNNER_DOCKER_WIPE_ON_START:-yes}" = "yes" ]; then + for docker_data_dir in buildkit containerd containers image network overlay2 fuse-overlayfs plugins runtimes swarm tmp vfs volumes; do + [ -d "/var/lib/docker/$docker_data_dir" ] && rm -rf "/var/lib/docker/${docker_data_dir:?}" + done + fi + # Clean up orphaned containers before dockerd starts # This prevents "failed to load container" errors on restart if [ -d "/data/docker/containers" ]; then