apiVersion: apps/v1
kind: Deployment
metadata:
  name: forgejo-runner
  namespace: forgejo
  labels:
    app: forgejo-runner
    app.kubernetes.io/component: forgejo-runner
    app.kubernetes.io/instance: forgejo-runner
    app.kubernetes.io/name: forgejo-runner
spec:
  replicas: 2
  selector:
    matchLabels:
      app: forgejo-runner
  template:
    metadata:
      name: forgejo-runner
      labels:
        app: forgejo-runner
        app.kubernetes.io/component: forgejo-runner
        app.kubernetes.io/instance: forgejo-runner
        app.kubernetes.io/name: forgejo-runner
    spec:
      automountServiceAccountToken: false
      restartPolicy: Always
      containers:
        - name: runner
          image: code.forgejo.org/forgejo/runner:7.0.0
          imagePullPolicy: IfNotPresent
          command:
            - /bin/bash
            - -c
          args:
            - |
              while ! nc -z localhost 2376 </dev/null ; do
                echo 'Waiting for docker daemon...' ;
                sleep 5 ;
                done ;
              echo "Done waiting for docker daemon, registering"
              forgejo-runner create-runner-file --secret ${RUNNER_TOKEN} --name ${RUNNER_NAME} --instance ${FORGEJO_INSTANCE_URL};
              echo "Done registering, starting runner"
              forgejo-runner --config /conf/config.yaml daemon
          env:
            - name: DOCKER_HOST
              value: tcp://localhost:2376
            - name: DOCKER_CERT_PATH
              value: /certs/client
            - name: DOCKER_TLS_VERIFY
              value: '0'
            - name: RUNNER_TOKEN
              valueFrom:
                secretKeyRef:
                  name: runner-secret
                  key: token
            - name: RUNNER_NAME
              valueFrom:
                configMapKeyRef:
                  name: forgejo-runner-config
                  key: runner-id
            - name: FORGEJO_INSTANCE_URL
              value: FORGEJO_SERVICE_URL # http://forgejo-service:3000
          resources:
            limits:
              cpu: '1'
              ephemeral-storage: 3Gi
              memory: 4Gi
            requests:
              cpu: 100m
              ephemeral-storage: '0'
              memory: 64Mi
          volumeMounts:
            - name: docker-certs
              mountPath: /certs
            - name: runner-data
              mountPath: /data
            - name: tmp
              mountPath: /tmp
            - name: config
              mountPath: /conf
              readOnly: true
          securityContext:
            allowPrivilegeEscalation: false
            capabilities:
              drop:
                - ALL
            privileged: false
            readOnlyRootFilesystem: true
            runAsNonRoot: true
            seccompProfile:
              type: RuntimeDefault
          ports:
            - containerPort: 80
              name: runner-port
        - name: daemon
          image: docker.io/docker:28.3.0-dind
          env:
            - name: DOCKER_TLS_CERTDIR
              value: /certs
          resources:
            limits:
              cpu: '1'
              ephemeral-storage: 3Gi
              memory: 4Gi
            requests:
              cpu: 100m
              ephemeral-storage: '0'
              memory: 64Mi
          securityContext:
            privileged: true
          volumeMounts:
            - name: docker-certs
              mountPath: /certs
      volumes:
        - name: config
          configMap:
            name: forgejo-runner-config
            items:
              - key: "config.yaml"
                path: "config.yaml"
        - name: docker-certs
          emptyDir: {}
        - name: runner-data
          emptyDir: {}
        - name: tmp
          emptyDir: {}
