#!/usr/bin/env bash set -euo pipefail # ============================================================================= # phase3_runners.sh — Deploy Gitea Actions runners from runners.conf # Depends on: Phase 1 complete (Gitea running on Unraid with admin token) # Steps: # 1. Get runner registration token from Gitea admin API # 2. Save token to .env for reuse by manage_runner.sh # 3. Deploy each runner defined in runners.conf via manage_runner.sh # Idempotent: skips already-deployed runners, reuses valid tokens. # ============================================================================= SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" source "${SCRIPT_DIR}/lib/common.sh" load_env require_vars GITEA_INTERNAL_URL GITEA_ADMIN_TOKEN ACT_RUNNER_VERSION phase_header 3 "Gitea Actions Runners" RUNNERS_CONF="${SCRIPT_DIR}/runners.conf" # --------------------------------------------------------------------------- # Pre-check: runners.conf must exist with at least one entry # --------------------------------------------------------------------------- if [[ ! -f "$RUNNERS_CONF" ]]; then log_error "runners.conf not found — copy runners.conf.example and fill in values" exit 1 fi # Count non-comment, non-blank lines to verify there are runners to deploy RUNNER_COUNT=$(grep -Evc '^[[:space:]]*($|#)' "$RUNNERS_CONF") if [[ "$RUNNER_COUNT" -eq 0 ]]; then log_error "No runners defined in runners.conf" exit 1 fi log_info "Found ${RUNNER_COUNT} runner(s) in runners.conf" # --------------------------------------------------------------------------- # Step 1: Get or reuse runner registration token # The registration token is a shared secret that all runners use to register # themselves with the Gitea instance. It's retrieved from the admin API and # saved to .env so manage_runner.sh can use it independently. # --------------------------------------------------------------------------- log_step 1 "Getting runner registration token..." if [[ -n "${GITEA_RUNNER_REGISTRATION_TOKEN:-}" ]]; then log_info "Registration token already in .env — reusing" else # Gitea returns the token as a JSON object: {"token": "..."} TOKEN_RESPONSE=$(gitea_api GET "/admin/runners/registration-token") GITEA_RUNNER_REGISTRATION_TOKEN=$(printf '%s' "$TOKEN_RESPONSE" | jq -r '.token') if [[ -z "$GITEA_RUNNER_REGISTRATION_TOKEN" ]] || [[ "$GITEA_RUNNER_REGISTRATION_TOKEN" == "null" ]]; then log_error "Failed to get runner registration token from Gitea" exit 1 fi save_env_var "GITEA_RUNNER_REGISTRATION_TOKEN" "$GITEA_RUNNER_REGISTRATION_TOKEN" log_success "Registration token saved to .env" fi # --------------------------------------------------------------------------- # Step 2: Deploy each runner via manage_runner.sh # Iterates over every non-comment line in runners.conf, extracts the name # (first pipe-delimited field), and invokes manage_runner.sh add. # manage_runner.sh handles its own idempotency (skips already-running runners). # --------------------------------------------------------------------------- log_step 2 "Deploying runners..." DEPLOYED=0 FAILED=0 while IFS='|' read -r name rest; do # Skip comments and blank lines [[ "$name" =~ ^[[:space:]]*# ]] && continue [[ -z "$name" ]] && continue name=$(echo "$name" | xargs) log_info "Processing runner: ${name}" if "${SCRIPT_DIR}/manage_runner.sh" add --name "$name"; then DEPLOYED=$((DEPLOYED + 1)) else log_error "Failed to deploy runner: ${name}" FAILED=$((FAILED + 1)) fi done < "$RUNNERS_CONF" # --------------------------------------------------------------------------- # Summary # --------------------------------------------------------------------------- printf '\n' log_info "Results: ${DEPLOYED} deployed, ${FAILED} failed (out of ${RUNNER_COUNT})" if [[ $FAILED -gt 0 ]]; then log_error "Some runners failed to deploy — check logs above" exit 1 fi log_success "Phase 3 complete — all ${RUNNER_COUNT} runner(s) deployed"