--- # Weekly Docker cleanup. Every pinned image version stays on disk after a bump, so without # this the disk of a busy runner fills and every job fails at container setup. # The settings are validated in validate.yml. # ExecCondition runs the gate script below: it skips the run (not a failure) while a container # younger than gitea_runner_docker_prune_job_max_age runs, so a prune never removes an image # or container a job needs. An older container is a leaked job container (a job cannot run # past its timeout): it is logged and ignored, or one leak would block the prune for good. # The window between that check and the prune is a few seconds; a job started inside it can # fail once and is re-run. A skipped run is not retried before the next timer elapse: on a busy # week set a shorter gitea_runner_docker_prune_schedule (e.g. daily). # The ExecStart= lines start with '-' so a failing step does not skip the later ones. # Named volumes are never pruned (docker volume prune without -a removes anonymous ones only). - name: Install Docker prune gate script become: true ansible.builtin.copy: dest: /usr/local/sbin/gitea-runner-docker-prune-gate owner: root group: root mode: "0755" # raw: the Go template braces are not Jinja. The tag shares the shebang line: Jinja keeps the # newline after {% raw %}, and a file that does not start with "#!" cannot be exec'd by systemd. content: | {% raw %}#!/bin/sh # Exit 0 when no container younger than $1 seconds runs (prune may proceed), 1 to skip. # Every failure skips: the prune must not run when the state is unknown. max_age="$1" case "$max_age" in '' | *[!0-9]*) echo "invalid max age '$max_age'"; exit 1 ;; esac now=$(date +%s) ids=$(/usr/bin/docker ps -q) || { echo "docker ps failed"; exit 1; } for id in $ids; do started=$(/usr/bin/docker inspect --format '{{.State.StartedAt}}' "$id") || { echo "cannot inspect $id"; exit 1; } started=$(date -d "$started" +%s) || { echo "cannot parse start time of $id"; exit 1; } age=$((now - started)) if [ "$age" -lt "$max_age" ]; then echo "container $id is $age s old: a job may be running, skipping the prune" exit 1 fi echo "ignoring container $id, running for $age s (leaked job container)" done exit 0 {% endraw %} when: gitea_runner_docker_prune_enabled | bool tags: [gitea_runner, prune] - name: Install Docker prune service become: true ansible.builtin.copy: dest: /etc/systemd/system/gitea-runner-docker-prune.service owner: root group: root mode: "0644" content: | [Unit] Description=Prune unused Docker data on the Gitea runner host After=docker.service Requires=docker.service [Service] Type=oneshot ExecCondition=/usr/local/sbin/gitea-runner-docker-prune-gate {{ gitea_runner_docker_prune_job_max_age }} ExecStart=-/usr/bin/docker container prune --force --filter until={{ gitea_runner_docker_prune_container_age }} ExecStart=-/usr/bin/docker image prune --all --force --filter until={{ gitea_runner_docker_prune_image_age }} ExecStart=-/usr/bin/docker builder prune --force --filter until={{ gitea_runner_docker_prune_image_age }} ExecStart=-/usr/bin/docker volume prune --force register: gitea_runner_prune_service when: gitea_runner_docker_prune_enabled | bool tags: [gitea_runner, prune] - name: Install Docker prune timer become: true ansible.builtin.copy: dest: /etc/systemd/system/gitea-runner-docker-prune.timer owner: root group: root mode: "0644" content: | [Unit] Description=Scheduled Docker prune on the Gitea runner host [Timer] OnCalendar={{ gitea_runner_docker_prune_schedule }} Persistent=true RandomizedDelaySec=10m [Install] WantedBy=timers.target register: gitea_runner_prune_timer when: gitea_runner_docker_prune_enabled | bool tags: [gitea_runner, prune] - name: Reload systemd for the prune units become: true ansible.builtin.systemd: daemon_reload: true when: - gitea_runner_docker_prune_enabled | bool - gitea_runner_prune_service is changed or gitea_runner_prune_timer is changed tags: [gitea_runner, prune] # Not in check mode: on the first run the unit files above are only simulated, so systemd # would not find the timer. A changed timer is restarted to re-arm a new schedule. - name: Enable the Docker prune timer become: true ansible.builtin.systemd: name: gitea-runner-docker-prune.timer enabled: true state: "{{ 'restarted' if gitea_runner_prune_timer is changed else 'started' }}" when: - gitea_runner_docker_prune_enabled | bool - not ansible_check_mode tags: [gitea_runner, prune]