forked from thirtythreeforty/neolink
-
-
Notifications
You must be signed in to change notification settings - Fork 50
147 lines (127 loc) · 5.53 KB
/
dockerprune.yml
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
name: DockerHub
on: [push, pull_request, workflow_dispatch]
jobs:
pre_job:
# continue-on-error: true # Uncomment once integration is finished
runs-on: ubuntu-latest
# Map a step output to a job output
outputs:
should_skip: ${{ steps.skip_check.outputs.should_skip }}
steps:
- id: skip_check
uses: fkirc/skip-duplicate-actions@v5
with:
# All of these options are optional, so you can remove them if you are happy with the defaults
concurrent_skipping: "same_content_newer"
skip_after_successful_duplicate: "false"
native:
needs: pre_job
if: needs.pre_job.outputs.should_skip != 'true'
name: native
runs-on: ubuntu-latest
steps:
- name: Check token is set
id: vars
shell: bash
run: |
unset HAS_SECRET
if [ -n $SECRET ]; then HAS_SECRET='true' ; fi
echo "HAS_SECRET_TOKEN=${HAS_SECRET}" >> $GITHUB_OUTPUT
env:
SECRET: "${{ secrets.DOCKER_PRUNE_TOKEN }}"
- name: Convert username to lower case for docker
id: string_user
if: ${{ steps.vars.outputs.HAS_SECRET_TOKEN }}
uses: ASzc/change-string-case-action@v6
with:
string: ${{ github.repository_owner }}
- name: Convert repo to lower case for docker
id: string_repo
if: ${{ steps.vars.outputs.HAS_SECRET_TOKEN }}
uses: ASzc/change-string-case-action@v6
with:
string: ${{ github.event.repository.name }}
- name: Run Clean Script
shell: bash
if: ${{ steps.vars.outputs.HAS_SECRET_TOKEN }}
run: |
#!/bin/bash
#Script will delete all images in all repositories of your docker hub account which are older than x days
set -e
# set username and password
UNAME="${OWNER}"
TOKEN="${TOKEN}"
DAYS="${DAYS}"
REPO_NAME="${REPO_NAME}"
# get list of namespaces accessible by user (not in use right now)
#NAMESPACES=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/namespaces/ | jq -r '.namespaces|.[]')
#echo $TOKEN
echo
# get list of repos for that user account
echo "List of Repositories in ${UNAME} Docker Hub account"
REPO_LIST="$(curl -s -H "Authorization: JWT ${TOKEN}" "https://hub.docker.com/v2/repositories/${UNAME}/?page_size=10000" | jq -r '.results|.[]|.name')"
echo "$REPO_LIST"
echo
# build a list of all images & tags
for i in ${REPO_LIST}
do
# get tags for repo
IMAGE_TAGS="$(curl -s -H "Authorization: JWT ${TOKEN}" "https://hub.docker.com/v2/repositories/${UNAME}/${i}/tags/?page_size=10000" | jq -r '.results|.[]|.name')"
# build a list of images from tags
for j in ${IMAGE_TAGS}
do
# add each tag to list
FULL_IMAGE_LIST="${FULL_IMAGE_LIST} ${UNAME}/${i}:${j}"
done
done
# output list of all docker images
echo
echo "List of all docker images in ${UNAME} Docker Hub account"
for i in ${FULL_IMAGE_LIST}
do
echo "${i}"
done
echo
echo "Identifying and deleting images which are older than ${DAYS} days in ${UNAME} docker hub account"
# Note!!! Please un-comment below line if you wanna perform operation on all repositories of your Docker Hub account
#for i in ${REPO_LIST}
repos=(
"${REPO_NAME}"
)
for i in "${repos[@]}"; do
# get tags for repo
echo
echo "Looping Through $i repository in ${UNAME} account"
IMAGE_TAGS="$(curl -s -H "Authorization: JWT ${TOKEN}" "https://hub.docker.com/v2/repositories/${UNAME}/${i}/tags/?page_size=10000" | jq -r '.results|.[]|.name')"
# build a list of images from tags
for j in ${IMAGE_TAGS}
do
if [[ "${j}" =~ ^[v]?[0-9][.][0-9][.][0-9]$ ]]; then
echo "Version tag (${j}) will not be deleted"
continue
fi
echo
# add last_updated_time
updated_time="$(curl -s -H "Authorization: JWT ${TOKEN}" "https://hub.docker.com/v2/repositories/${UNAME}/${i}/tags/${j}/?page_size=10000" | jq -r '.last_updated')"
echo "$updated_time"
datetime=$updated_time
timeago="${DAYS} days ago"
dtSec=$(date --date "$datetime" +'%s')
taSec=$(date --date "$timeago" +'%s')
echo "INFO: dtSec=$dtSec, taSec=$taSec"
if [ "$dtSec" -lt "$taSec" ]
then
echo "This image ${UNAME}/${i}:${j} is older than ${DAYS} days, deleting this image"
## Please uncomment below line to delete docker hub images of docker hub repositories
curl -s -X DELETE -H "Authorization: JWT ${TOKEN}" "https://hub.docker.com/v2/repositories/${UNAME}/${i}/tags/${j}/"
else
echo "This image ${UNAME}/${i}:${j} is within ${DAYS} days time range, keep this image"
fi
done
done
echo "Script execution ends"
env:
DAYS: "100"
REPO_NAME: ${{ steps.string_repo.outputs.lowercase }}
OWNER: ${{ vars.DOCKER_USERNAME || steps.string_user.outputs.lowercase }}
TOKEN: ${{ secrets.DOCKER_PRUNE_TOKEN }}