From bcda5617b596d5b9b0e9ed5fae6bb2e4c46ff4e0 Mon Sep 17 00:00:00 2001 From: rock Date: Sun, 13 Sep 2026 14:14:28 +0900 Subject: [PATCH] infra: add CronJob for runner disk cleanup - Runs daily at 2 AM to clean Docker, Cargo cache, /tmp - Prevents 'no space left on device' errors in CI builds - Deploy to homelab: kubectl apply -f k8s/infra/runner-cleanup-cronjob.yaml - Adjust namespace and node selectors for your environment --- k8s/infra/runner-cleanup-cronjob.yaml | 121 ++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 k8s/infra/runner-cleanup-cronjob.yaml diff --git a/k8s/infra/runner-cleanup-cronjob.yaml b/k8s/infra/runner-cleanup-cronjob.yaml new file mode 100644 index 0000000..28e9017 --- /dev/null +++ b/k8s/infra/runner-cleanup-cronjob.yaml @@ -0,0 +1,121 @@ +# CronJob to periodically clean Gitea Actions runner disk space +# Prevents "no space left on device" errors during Docker builds +# Deploy to: kubectl apply -f k8s/infra/runner-cleanup-cronjob.yaml + +apiVersion: batch/v1 +kind: CronJob +metadata: + name: runner-disk-cleanup + namespace: ci # Adjust to your runner namespace + labels: + app: runner-cleanup +spec: + # Run daily at 2 AM + schedule: "0 2 * * *" + + # Keep last 3 successful jobs + successfulJobsHistoryLimit: 3 + failedJobsHistoryLimit: 1 + + jobTemplate: + spec: + template: + metadata: + labels: + app: runner-cleanup + spec: + serviceAccountName: runner-cleanup + + # Run on node with Gitea Actions runner + affinity: + nodeAffinity: + requiredDuringSchedulingIgnoredDuringExecution: + nodeSelectorTerms: + - matchExpressions: + - key: kubernetes.io/hostname + operator: In + values: + - runner-node # Adjust to your runner node name + + containers: + - name: cleanup + image: docker:24 + securityContext: + privileged: true # Needed to access Docker daemon + command: + - /bin/sh + - -c + - | + echo "=== Runner disk cleanup at $(date) ===" + + df -h / + echo "" + + echo "Cleaning Docker..." + docker system prune -af --volumes 2>&1 | tail -5 + + echo "" + echo "Cleaning Cargo cache..." + rm -rf /root/.cargo/registry/cache 2>/dev/null + rm -rf /root/.cargo/registry/index 2>/dev/null + rm -rf /root/.cargo/git 2>/dev/null + + echo "" + echo "Cleaning /tmp..." + rm -rf /tmp/* 2>/dev/null + + echo "" + echo "Disk after cleanup:" + df -h / + + volumeMounts: + - name: docker-sock + mountPath: /var/run/docker.sock + - name: runner-home + mountPath: /root + + volumes: + # Access Docker daemon on host + - name: docker-sock + hostPath: + path: /var/run/docker.sock + # Access runner home directory + - name: runner-home + hostPath: + path: /home/runner # Adjust to your runner home path + + restartPolicy: OnFailure + +--- +# ServiceAccount for cleanup job +apiVersion: v1 +kind: ServiceAccount +metadata: + name: runner-cleanup + namespace: ci + +--- +# Role for cleanup job +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: runner-cleanup +rules: + - apiGroups: [""] + resources: ["nodes"] + verbs: ["get", "list"] + +--- +# RoleBinding +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + name: runner-cleanup +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: runner-cleanup +subjects: + - kind: ServiceAccount + name: runner-cleanup + namespace: ci