-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpurge-ghcr-tags.sh
executable file
·72 lines (56 loc) · 1.72 KB
/
purge-ghcr-tags.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/bash
set -e
# Colorize terminal
red='\e[0;31m'
no_color='\033[0m'
# Default
if [ $(uname) = 'Darwin' ]; then
DATE="$(date -v -1m '+%Y-%m-%d')"
else
DATE="$(date -d '1 month ago' '+%Y-%m-%d')"
fi
# Declare script helper
TEXT_HELPER="\nThis script aims to delete ghcr image tags with all its subsequent images older than a given date.
Following flags are available:
-d Date from which images will be deleted (format: 'yyyy-mm-dd', default is 1 month ago).
-g Github token to perform api calls.
-i Image name used for api calls.
-u Github user used for api calls.
-h Print script help.\n\n"
print_help() {
printf "$TEXT_HELPER"
}
# Parse options
while getopts hd:g:i:u: flag; do
case "${flag}" in
d)
DATE=${OPTARG};;
g)
GITHUB_TOKEN=${OPTARG};;
i)
IMAGE_NAME=${OPTARG};;
u)
USER=${OPTARG};;
h | *)
print_help
exit 0;;
esac
done
if [ -z "$GITHUB_TOKEN" ] || [ -z "$USER" ] || [ -z "$IMAGE_NAME" ] || [ -z "$DATE" ]; then
echo "\nYMissing arguments ...\n"
print_help
exit 1
fi
IMAGE_NAME_URL_ENCODED="$(jq -rn --arg x ${IMAGE_NAME} '$x | @uri')"
IMAGES=$(curl -s \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
"https://api.github.com/users/${USER}/packages/container/${IMAGE_NAME_URL_ENCODED}/versions?per_page=100" \
| jq -c --arg d "$DATE" '.[] | select(.created_at < $d)')
for IMAGE in $IMAGES; do
IMAGE_ID="$(echo $IMAGE | jq '.id')"
printf "\n${red}[Delete ghcr image].${no_color} Deleting image '$USER/$IMAGE_NAME' with id '$IMAGE_ID'\n"
curl -s \
-X DELETE \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
"https://api.github.com/users/${USER}/packages/container/${IMAGE_NAME_URL_ENCODED}/versions/${IMAGE_ID}"
done