stack.basicstack.de/apps/forgejo-runner/forgejo-runner-statefulset.yaml
CTO Agent 27397fe859 feat: migrate Forgejo runner to StatefulSet with Docker-in-Docker
Replaces the Deployment-based runner with a StatefulSet using a proper
Docker-in-Docker sidecar for improved isolation and state management.

Key changes:
- StatefulSet deployment for stable pod identity and persistent storage
- Docker-in-Docker init container (privileged) providing isolated Docker daemon
- Two persistent volumes: runner-data (1Gi) for config, docker-data (20Gi) for image cache
- Uses hcloud-volumes storage class for durability
- Each replica gets its own Docker daemon and image cache

This addresses the workflow failure where job containers (node:24-bookworm)
couldn't access Docker because the CLI was missing. The DinD sidecar provides
a complete Docker environment for all job containers.

Fixes: DEV-335

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-07-18 16:41:20 +00:00

124 lines
3.5 KiB
YAML

apiVersion: apps/v1
kind: StatefulSet
metadata:
name: forgejo-runner
namespace: forgejo
spec:
serviceName: forgejo-runner
replicas: 1
selector:
matchLabels:
app: forgejo-runner
template:
metadata:
labels:
app: forgejo-runner
spec:
restartPolicy: Always
serviceAccountName: forgejo-runner
volumes:
- name: docker-socket
emptyDir: {}
- name: config
configMap:
name: forgejo-runner-config
initContainers:
- name: docker
image: docker:28-dind
securityContext:
privileged: true
volumeMounts:
- name: docker-socket
mountPath: /var/run
- name: docker-data
mountPath: /var/lib/docker
startupProbe:
exec:
command: ["/usr/bin/test", "-S", "/var/run/docker.sock"]
initialDelaySeconds: 5
periodSeconds: 2
failureThreshold: 30
livenessProbe:
exec:
command: ["/usr/bin/test", "-S", "/var/run/docker.sock"]
periodSeconds: 10
restartPolicy: Always
containers:
- name: runner
image: code.forgejo.org/forgejo/runner:4.0.1
imagePullPolicy: IfNotPresent
command:
- sh
- -c
- |
# Wait for Forgejo to be ready
while ! wget -q -O- https://forgejo.basicstack.de/api/healthz > /dev/null 2>&1; do
echo "Waiting for Forgejo to be ready..."
sleep 5
done
# Wait for Docker daemon to be ready
while ! docker version > /dev/null 2>&1; do
echo "Waiting for Docker daemon to be ready..."
sleep 2
done
# Re-register if the stored address is the internal cluster URL
if [ -f /data/.runner ] && grep -q "svc.cluster.local" /data/.runner; then
echo "Detected internal cluster URL in .runner — forcing re-registration with external URL"
rm /data/.runner
fi
# Register runner if not already registered
if [ ! -f /data/.runner ]; then
forgejo-runner register \
--no-interactive \
--instance https://forgejo.basicstack.de \
--token "$RUNNER_TOKEN" \
--name "k3s-dind-runner-${HOSTNAME##*-}" \
--labels ubuntu-latest:docker://node:24-bookworm,ubuntu-22.04:docker://node:24-bookworm
fi
# Start runner
forgejo-runner daemon --config /etc/forgejo-runner/config.yaml
env:
- name: RUNNER_TOKEN
valueFrom:
secretKeyRef:
name: forgejo-runner-token
key: token
- name: DOCKER_HOST
value: unix:///var/run/docker.sock
volumeMounts:
- name: runner-data
mountPath: /data
- name: docker-socket
mountPath: /var/run
- name: config
mountPath: /etc/forgejo-runner
resources:
requests:
cpu: 200m
memory: 512Mi
limits:
cpu: "4"
memory: 4Gi
volumeClaimTemplates:
- metadata:
name: runner-data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: hcloud-volumes
- metadata:
name: docker-data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
storageClassName: hcloud-volumes