-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean_repo.sh
executable file
·38 lines (32 loc) · 1.04 KB
/
clean_repo.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env bash
# a script to delete all the ancient versions of a repository generally generated by continious integration tools
if [[ $# -ne 1 ]] ; then
echo "put your repository to clean as argument (you can just put a part of its name to find it)"
exit 1
fi
# find max tag of a repository
tags=`docker images | sed -n /$1/p | awk '{print $2}'`
max=0
for i in $tags; do
# [, ], [[, ]], and operators should be surrounded by space
# =~ regular expression match operator
if [[ $i =~ ^[0-9]+$ && $i -gt $max ]] ; then
max=$i
fi
done
# delete all the ancient tags
# awk -v max="$max": assign the value of shell variable max to awk variable max
ids=`docker images | sed -n /dashboard/p | awk -v max="$max" '{if($2 < max) print $3}'`
cnt=0
for id in $ids; do
docker rmi $id
# use expr to do arithmetic operations
cnt=`expr $cnt + 1`
done
if [[ $cnt -gt 1 ]] ; then
echo $cnt images deleted.
elif [[ $cnt -eq 1 ]] ; then
echo $cnt image deleted.
else
echo repository was already clean, no image deleted.
fi