#!/usr/bin/env bash # deregister-runner.sh — remove a runner registration from Gitea via the API. # # act_runner has no "unregister" command, so deregistration is done through the # Gitea REST API. This requires an *API* token (not a registration token) with # admin rights, plus the runner id or name. Run this before retiring or renaming # a runner to keep the Gitea UI clean, then delete the local config to start fresh. # # Usage: # GITEA_INSTANCE_URL=https://git.example.com \ # GITEA_API_TOKEN= \ # ./scripts/deregister-runner.sh set -euo pipefail : "${GITEA_INSTANCE_URL:?GITEA_INSTANCE_URL must be set (e.g. https://git.example.com)}" : "${GITEA_API_TOKEN:?GITEA_API_TOKEN must be set (an admin API token, not a registration token)}" TARGET="${1:-}" if [ -z "${TARGET}" ]; then echo "[FATAL] Provide the runner name or id as the first argument." >&2 echo "Usage: $0 " >&2 exit 1 fi API="${GITEA_INSTANCE_URL%/}/api/v1" AUTH="Authorization: token ${GITEA_API_TOKEN}" # Resolve a runner name to an id if a non-numeric target was given. if [[ "${TARGET}" =~ ^[0-9]+$ ]]; then RUNNER_ID="${TARGET}" else echo "Resolving runner id for name '${TARGET}' ..." RUNNER_ID="$(curl -fsS -H "${AUTH}" "${API}/admin/actions/runners" \ | jq -r --arg n "${TARGET}" '.runners[] | select(.name==$n) | .id' | head -1)" if [ -z "${RUNNER_ID}" ]; then echo "[FATAL] No runner found with name '${TARGET}'." >&2 exit 1 fi fi echo "Deregistering runner id ${RUNNER_ID} from ${GITEA_INSTANCE_URL} ..." http_code="$(curl -fsS -o /dev/null -w '%{http_code}' \ -X DELETE -H "${AUTH}" "${API}/admin/actions/runners/${RUNNER_ID}")" if [ "${http_code}" = "204" ]; then echo "Deregistration complete (runner id ${RUNNER_ID} removed)." echo "Remove the local config to start fresh: rm config/runner.yaml" else echo "[FATAL] Unexpected HTTP status ${http_code} from delete request." >&2 exit 1 fi