-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleangcr.sh
37 lines (34 loc) · 969 Bytes
/
cleangcr.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
#!/bin/bash
IFS=$'\n\t'
set -eou pipefail
if [[ "$#" -ne 2 || "${1}" == '-h' || "${1}" == '--help' ]]; then
cat >&2 <<"EOF"
gcrgc.sh cleans up tagged or untagged images pushed before specified date
for a given repository (an image name without a tag/digest).
USAGE:
cleangcr.sh REPOSITORY DATE
EXAMPLE
cleangcr.sh gcr.io/test/my-app 2019-04-14
would clean up everything under the gcr.io/test/my-app repository
pushed before 2019-04-14.
EOF
exit 1
elif [[ "${#2}" -ne 10 ]]; then
echo "wrong DATE format; use YYYY-MM-DD." >&2
exit 1
fi
main(){
local C=0
IMAGE="${1}"
DATE="${2}"
for digest in $(gcloud container images list-tags ${IMAGE} --limit=999999 --sort-by=TIMESTAMP \
--filter="timestamp.datetime < '${DATE}'" --format='get(digest)'); do
(
set -x
gcloud container images delete -q --force-delete-tags "${IMAGE}@${digest}"
)
let C=C+1
done
echo "Deleted ${C} images in ${IMAGE}." >&2
}
main "${1}" "${2}"