variable "grt" {
  type = string
  description = "Gitea runner token"
}

variable "domain" {
  type = string
  description = "Gitea Domain Name"
}

locals {
  GITEA_RUNNER_TOKEN = var.grt # Replace with raw token surrounded by quotes if you don't want to pass via cli or using web ui
  GITEA_DOMAIN = var.domain # Replace with domain surrounded by quotes if you don't want to pass via cli or using web ui
  GITEA_RUNNER_NAME = "${NOMAD_TASK_NAME}-${NOMAD_ALLOC_INDEX}"
}

job "gitea-runner" {
  datacenters = ["dc1"]
  type = "service"

  group "application" {
    count = 1

    scaling {
      enabled = true
      min = 1
      max = 5
    }

    network {
      mode = "bridge"
    }

    service {
      name = "gitea-runner"

      connect {
        sidecar_service {
          proxy {
            upstreams {
              destination_name   = "gitea"
              local_bind_address = "127.0.0.1"
              local_bind_port    = 3000
            }
          } 
          tags = ["traefik.enable=false"] # Hide envoy from traefik
        }
      }
    }

    task "gitea-runner" {
      driver = "docker"

      config {
        image = "${local.GITEA_DOMAIN}/caleb/nomad_act_runner:0.0.1"
        privileged = true
      }

      env = {
        GITEA_INSTANCE_URL="http://${NOMAD_UPSTREAM_ADDR_gitea}"
        GITEA_RUNNER_REGISTRATION_TOKEN="${local.GITEA_RUNNER_TOKEN}"
        GITEA_RUNNER_NAME="${local.GITEA_RUNNER_NAME}"
      }

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