-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelete-ghcr-image.sh
executable file
·106 lines (81 loc) · 2.78 KB
/
delete-ghcr-image.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/bash
set -e
# Colorize terminal
red='\e[0;31m'
no_color='\033[0m'
# Default
MODE="users"
# Get versions
DOCKER_VERSION="$(docker --version)"
DOCKER_BUILDX_VERSION="$(docker buildx version)"
# Declare script helper
TEXT_HELPER="\nThis script aims to delete an image with all its subsequent images in ghcr.
Following flags are available:
-g Github token to perform api calls.
-i Image name used for api calls.
-m Mode to use (`orgs` or `users`).
-o Github owner (organization or user) used for api calls.
-t Image tage to delete.
-h Print script help.\n\n"
print_help() {
printf "$TEXT_HELPER"
}
# Parse options
while getopts hg:i:m:o:t: flag; do
case "${flag}" in
g)
GITHUB_TOKEN=${OPTARG};;
i)
IMAGE_NAME=${OPTARG};;
m)
MODE=${OPTARG};;
o)
OWNER=${OPTARG};;
t)
TAG=${OPTARG};;
h | *)
print_help
exit 0;;
esac
done
checkDockerRunning () {
if [ ! -x "$(command -v docker)" ]; then
printf "\nThis script uses docker, and it isn't running - please start docker and try again!\n"
exit 1
fi
}
checkBuildxPlugin () {
if [ ! "$DOCKER_BUILDX_VERSION" ]; then
printf "\nThis script uses docker buildx plugin, and it isn't installed - please install docker buildx plugin and try again!\n"
exit 1
fi
}
# Settings
printf "\nScript settings:
-> docker version: ${DOCKER_VERSION}
-> docker buildx version: ${DOCKER_BUILDX_VERSION}\n"
if [ -z "$GITHUB_TOKEN" ] || [ -z "$OWNER" ] || [ -z "$IMAGE_NAME" ] || [ -z "$TAG" ]; 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/${MODE}/${OWNER}/packages/container/${IMAGE_NAME_URL_ENCODED}/versions?per_page=100")
MAIN_IMAGE_ID=$(echo "$IMAGES" | jq -r --arg t "$TAG" '.[] | select(.metadata.container.tags[] | contains($t)) | .id')
# Delete subsequent images
while read -r SHA; do
IMAGE_ID=$(echo "$IMAGES" | jq -r --arg s "$SHA" '.[] | select(.name == $s) | .id')
printf "\n${red}[Delete ghcr image].${no_color} Deleting subsequent image '$OWNER/$IMAGE_NAME@$SHA'\n"
curl -s \
-X DELETE \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
"https://api.github.com/${MODE}/${OWNER}/packages/container/${IMAGE_NAME_URL_ENCODED}/versions/${IMAGE_ID}"
done <<< "$(docker buildx imagetools inspect ghcr.io/${OWNER}/${IMAGE_NAME}:${TAG} --raw | jq -r '.manifests[] | .digest')"
# Delete main image
printf "\n${red}[Delete ghcr image].${no_color} Deleting image '$OWNER/$IMAGE_NAME:$TAG'\n"
curl -s \
-X DELETE \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
"https://api.github.com/${MODE}/${OWNER}/packages/container/${IMAGE_NAME_URL_ENCODED}/versions/${MAIN_IMAGE_ID}"