Skip to content

Instantly share code, notes, and snippets.

@hqrd
Last active March 20, 2024 18:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hqrd/fe01cebc0fab2d3c0e5c1c869e92ebaf to your computer and use it in GitHub Desktop.
Save hqrd/fe01cebc0fab2d3c0e5c1c869e92ebaf to your computer and use it in GitHub Desktop.
This script is used to remove offline gitlab runners for every projects
#!/bin/bash
###
# This script is used to remove offline gitlab runners for every projects
###
set -o nounset
set -o errexit
die () {
echo >&2 "$@"
exit 1
}
# Required tools:
# curl
# jq
# tr
[ "$#" -ge 1 ] || die "1 argument required, $# provided"
runnerId=$1
gitlabUrl=https://gitlaburl/gitlab # todo: point to gitlab url to cleanup
gitlabApiToken="myapitoken" # todo: fill with admin api token
# Which runners are online?
onlineRunnerIds=$(curl --fail --silent --header "PRIVATE-TOKEN: ${gitlabApiToken}" "${gitlabUrl}/api/v4/runners?scope=online&per_page=10000000000000000000000" | jq -r '.[].id' | sed 's|[^0-9]||g')
for id in ${onlineRunnerIds}; do
if [ "$id" == "$runnerId" ]; then
die "skipping deletion of ${runnerId} because runner is online"
fi
done
# Remove runners
echo "unregistering ${runnerId}"
url="${gitlabUrl}/api/v4/runners/${runnerId}"
projects=$(curl --fail --silent --header "PRIVATE-TOKEN: ${gitlabApiToken}" "${url}" | jq ".projects[].id"| sed 's|[^0-9]||g')
for project in ${projects}; do
echo "${gitlabUrl}/api/v4/projects/${project}/runners/${runnerId}"
echo $(curl --fail --silent --request DELETE --header "PRIVATE-TOKEN: ${gitlabApiToken}" "${gitlabUrl}/api/v4/projects/${project}/runners/${runnerId}")
done
echo "deleting ${runnerId}"
echo "${gitlabUrl}/api/v4/runners/${runnerId}"
echo $(curl --fail --silent --request DELETE --header "PRIVATE-TOKEN: ${gitlabApiToken}" "${gitlabUrl}/api/v4/runners/${runnerId}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment