Skip to content

Instantly share code, notes, and snippets.

@nobiit
Last active July 27, 2022 14:26
Show Gist options
  • Save nobiit/7afd334981d9765b5175b6195b99d1f3 to your computer and use it in GitHub Desktop.
Save nobiit/7afd334981d9765b5175b6195b99d1f3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -e
github_token=$(kubectl get -n runner-system secrets controller-manager -o json | jq -r .data.github_token | base64 -d)
function list_organization {
kubectl get runner -A -o json | jq -r .items[].spec.organization
kubectl get runnerset -A -o json | jq -r .items[].spec.organization
}
for item in $(list_organization | uniq); do
page=0
while true; do
page=$(expr ${page} + 1)
runners=$(curl -s -u token:${github_token} "https://api.github.com/orgs/${item}/actions/runners?page=${page}")
if [ $(echo ${runners} | jq -r '.runners | length') == 0 ]; then
break
fi
for runner in $(echo ${runners} | jq -r '.runners[] | select(.status == "offline") | tojson'); do
echo ${runner} | jq -r .
runner_id=$(echo ${runner} | jq -r .id)
curl -s -u token:${github_token} -X DELETE "https://api.github.com/orgs/${item}/actions/runners/${runner_id}"
done
done
done
@MichaelSp
Copy link

Here is the same thing using yq

#!/usr/bin/env bash
set -e

github_token=$(kubectl get -n actions-runner-system secrets controller-manager -o json | jq -r .data.github_token | base64 -d)

function list_organization {
  kubectl get runner -A -o yaml | yq eval '.items[].spec.organization'
  kubectl get runnerset -A -o yaml | yq eval '.items[].spec.organization'
}

for item in $(list_organization | uniq); do
  page=0
  while true; do
    page=$(expr ${page} + 1)
    runners=$(curl -s -u token:${github_token} "https://api.github.com/orgs/${item}/actions/runners?page=${page}")
    if [ $(echo ${runners} | yq eval '.runners | length') == 0 ]; then
      break
    fi
    for runner in $(echo ${runners} | yq eval '.runners[] | select(.status == "offline") | tojson'); do
      echo ${runner} | yq eval .
      runner_id=$(echo ${runner} | yq eval .id)
      curl -s -u token:${github_token} -X DELETE "https://api.github.com/orgs/${item}/actions/runners/${runner_id}"
    done
  done
done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment