Skip to content

Instantly share code, notes, and snippets.

@timhughes
Last active September 23, 2018 01:58
Show Gist options
  • Save timhughes/e2b9da42608a25f644a1 to your computer and use it in GitHub Desktop.
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)
#!/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