Last active
September 23, 2018 01:58
-
-
Save timhughes/e2b9da42608a25f644a1 to your computer and use it in GitHub Desktop.
Cleanup docker instances that have been created by gitlab-runner (gitlab-ci-multi-runner)
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 | |
# Copyright: Tim Hughes <[email protected]> | |
# License: MIT | |
PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin | |
DOCKER_EXE=$(which docker) | |
GITLABRUNNER_EXE=$(which gitlab-runner) | |
RUNNER_ID_LIST=$($GITLABRUNNER_EXE verify 2>&1 | sed -n 's/.*\([a-zA-Z0-9]\{8\}\) Veryfing.*/\1/p') | |
CONTAINERS_TO_CLEANUP=() | |
for RUNNER_ID in $RUNNER_ID_LIST | |
do | |
while read line | |
do | |
CONTAINERS_TO_CLEANUP+=( $(echo $line|awk '{print $1}') ) | |
done < <($DOCKER_EXE ps -a -f status=exited|grep -e "Exited.*\(hours\|days\) ago.*runner-${RUNNER_ID}-project" ) | |
done | |
for CONTAINER_ID in "${CONTAINERS_TO_CLEANUP[@]}" | |
do | |
$DOCKER_EXE rm -v $CONTAINER_ID | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment