RemoveGitLabRunners

3rd November 2017 at 11:25am
Bash GitLab jq

Sometimes you need to remove a GitLab runner that no longer exists. You might automatically provision and then tear down GitLab runners in a cloud environment like OpenStack, AWS or GCE.

Use the GitLab CE API. Documentation can be found at http://docs.gitlab.com/ce/api/runners.html.

Using jq for some nice formatting, you might get a list of runners using:

curl -sSL \
  --header "PRIVATE-TOKEN: $GITLAB_API_PRIVATE_TOKEN" \
  "$GITLAB_API_ENDPOINT/runners" | jq

However, assume for the moment that you already know the IDs of the runners, because you provisioned them automatically using Terraform or autoscaled them perhaps?

set -euvxo pipefail

function gitlab () {
  curl -sSL \
    --header "PRIVATE-TOKEN: $GITLAB_API_PRIVATE_TOKEN" \
    "$@"
}

function query () {
  gitlab "${GITLAB_API_ENDPOINT%%/}/$@"
}

function delete () {
  gitlab --request DELETE "${GITLAB_API_ENDPOINT%%/}/$@"
}

for runner_id in 285 283 212
do
  for project_id in $(query "runners/$runner_id" \
                      | jq '.projects[] | .id')
  do
    delete "projects/$project_id/runners/$runner_id"
  done
  delete "runners/$runner_id"
done