From 00ec29dc190acd7b4a5ec8f475081363e82d6ac3 Mon Sep 17 00:00:00 2001
From: jurgenhaas <juergen.haas@lakedrops.com>
Date: Thu, 17 Aug 2023 16:38:31 +0200
Subject: [PATCH] Add support for auto-registration of runners

---
 tasks/main.yml        |  8 ++++++++
 templates/register.sh | 14 ++++++++++++--
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/tasks/main.yml b/tasks/main.yml
index 2202218..7232dc9 100644
--- a/tasks/main.yml
+++ b/tasks/main.yml
@@ -63,4 +63,12 @@
       tags:
         - cron
 
+    - name: Auto-register runners for projects
+      cron:
+        name: Auto-register runners
+        job: /usr/local/bin/register-gitlab-runner auto > /dev/null 2>&1 && {{ lookup('healthchecks', '', user='root', jobname='Auto-register runners', schedule='* * * * *', disabled=crons_disabled|default(false)) }}
+        disabled: '{{ crons_disabled|default(false) }}'
+      tags:
+        - cron
+
   when: not excluded_roles or "gitlab-runner" not in excluded_roles
diff --git a/templates/register.sh b/templates/register.sh
index 009182b..6a58456 100755
--- a/templates/register.sh
+++ b/templates/register.sh
@@ -1,12 +1,22 @@
 #!/bin/bash
 
+set -e
+
 if [[ "$1" == "" ]]; then
   echo "Project ID required as first argument."
   exit 1
+elif [[ "$1" == "auto" ]]; then
+  ID=$(curl -s -H "PRIVATE-TOKEN: {{ gitlab_issue.token }}" "https://gitlab.lakedrops.com/api/v4/projects/?topic=needs_runner_{{ inventory_hostname }}" | jq -r '.[0].id')
+  if [[ "$ID" == "null" ]]; then
+    echo "Nothing to do"
+    exit 0
+  fi
+  echo "Adding runner for project ID $ID"
+  curl -X PUT -s -H "PRIVATE-TOKEN: {{ gitlab_issue.token }}" "https://gitlab.lakedrops.com/api/v4/projects/${ID}" -d "topics=runner_{{ inventory_hostname }}"
+else
+  ID=$1
 fi
 
-ID=$1
-
 TOKEN=$(curl -s -H "PRIVATE-TOKEN: {{ gitlab_issue.token }}" "https://gitlab.lakedrops.com/api/v4/projects/${ID}" | jq -r .runners_token)
 NAME=$(curl -s -H "PRIVATE-TOKEN: {{ gitlab_issue.token }}" "https://gitlab.lakedrops.com/api/v4/projects/${ID}" | jq -r .path_with_namespace)
 
-- 
GitLab