Skip to content

Instantly share code, notes, and snippets.

@agarzon
Created March 30, 2023 13:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agarzon/5856def48e6073dd5c96246a197f5d8c to your computer and use it in GitHub Desktop.
Save agarzon/5856def48e6073dd5c96246a197f5d8c to your computer and use it in GitHub Desktop.
gitlab-runner clear cache script
#!/bin/bash
#Variables
gitlabUrl="https://gitlab.yourdomain.com"
configFile="/etc/gitlab-runner/config.toml"
gitlabTolken="" # Get it from https://gitlabUrl/profile/account API read access token
# Get GitLab Runner tokens from config.toml
mapfile -t runner_tokens < <(sudo grep -oP 'token = "\K[^"]+' ${configFile})
# Check if any GitLab Runner is busy
check_busy() {
for token in "${runner_tokens[@]}"; do
runner_status=$(curl --silent --header "PRIVATE-TOKEN: ${gitlabTolken}" "${gitlabUrl}/api/v4/runners?token=${token}" | jq '.[].status')
if [ "$runner_status" == "\"running\"" ]; then
return 0
fi
done
return 1
}
# Wait for all GitLab Runners to finish
while check_busy; do
echo "One or more GitLab Runners are busy, waiting for them to finish..."
sleep 60
done
# Stop all GitLab Runners
echo "Stopping all GitLab Runners..."
sudo gitlab-runner stop
# Prune Docker system
echo "Pruning Docker system..."
sudo docker system prune -a -f --volumes
# Remove contents of /var/lib/docker/overlay2
echo "Cleaning /var/lib/docker/overlay2 folder..."
sudo rm -rf /var/lib/docker/overlay2/*
# Start all GitLab Runners
echo "Starting all GitLab Runners..."
sudo gitlab-runner start
echo "Cache cleaning completed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment