# ABOUTME: Gitea Actions runner for CI/CD pipelines.
# ABOUTME: Runs containerized actions with Docker-in-Docker support.

# Setup required before running:
#   sudo mkdir -p /data/services/act-runner
#   nomad var put secrets/act-runner registration_token="<token-from-gitea-ui>"

job "act-runner" {
  datacenters = ["alo"]
  type        = "service"

  group "runner" {
    network {
      mode = "host"
    }

    task "runner" {
      driver = "docker"

      config {
        image        = "gitea/act_runner:latest"
        network_mode = "host"
        privileged   = true
        volumes = [
          "/var/run/docker.sock:/var/run/docker.sock",
          "/data/services/act-runner:/data",
          "local/config.yaml:/.runner/config.yaml",
        ]
      }

      template {
        destination = "local/config.yaml"
        data        = <<EOH
log:
  level: info
runner:
  file: /data/.runner
  capacity: 2
  timeout: 3h
  labels:
    - "ubuntu-latest:docker://node:20-bookworm"
    - "nix:docker://nixos/nix:latest"
cache:
  enabled: true
  dir: /data/cache
container:
  network: "host"
  privileged: true
  valid_volumes:
    - /data/services/**
EOH
      }

      env {
        GITEA_INSTANCE_URL = "https://gitea.v.paler.net"
        GITEA_RUNNER_LABELS = "ubuntu-latest:docker://node:20-bookworm,nix:docker://gitea.v.paler.net/ppetru/nix-runner:v4"
      }

      # Template needed for nomadVar interpolation (secrets) and Nomad runtime vars
      template {
        destination = "secrets/env.env"
        env         = true
        data        = <<EOH
GITEA_RUNNER_REGISTRATION_TOKEN={{ with nomadVar "secrets/act-runner" }}{{ .registration_token }}{{ end }}
GITEA_RUNNER_NAME={{ env "NOMAD_ALLOC_ID" }}
EOH
      }

      resources {
        cpu    = 2000
        memory = 2048
      }
    }
  }
}
