Last active
March 20, 2024 18:13
-
-
Save hqrd/fe01cebc0fab2d3c0e5c1c869e92ebaf to your computer and use it in GitHub Desktop.
This script is used to remove offline gitlab runners for every projects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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