Skip to content

Commit dd00ea5

Browse files
committed
perf: add force delete repo support
1 parent 79e3205 commit dd00ea5

File tree

2 files changed

+82
-15
lines changed

2 files changed

+82
-15
lines changed

.github/workflows/dc_registry_delete_bulk.yml

+33-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ on:
88
type: string
99
description: "Delete older than (days)"
1010
default: ''
11+
app_name:
12+
required: false
13+
type: string
14+
description: "Application Name"
15+
default: ''
16+
force_delete_repo:
17+
required: false
18+
type: boolean
19+
description: "Force Delete"
20+
default: false
1121

1222
env:
1323
GH_TOKEN: ${{ secrets.GH_TOKEN }}
@@ -30,13 +40,25 @@ jobs:
3040
uses: actions/checkout@v4
3141
with:
3242
repository: 'code-kern-ai/cicd-deployment-scripts'
43+
44+
- name: Validate Input
45+
run: |
46+
if [ "${{ github.event.inputs.force_delete_repo }}" = true ] && [ -z "${{ inputs.app_name }}" ]; then
47+
echo "::error::Application Name is required when force deleting repository"
48+
exit 1
49+
fi
3350
3451
- name: Generate Matrix
3552
id: generate-matrix
3653
run: |
37-
app_names=$(curl -s -u "${{ env.DEV_LOGIN_USERNAME }}:${{ env.DEV_LOGIN_PASSWORD }}" \
38-
https://${{ env.DEV_CONTAINER_REGISTRY }}/v2/_catalog \
39-
| jq -c '[.repositories[] | split("/") | .[1]]')
54+
if [ "${{ github.event.inputs.force_delete_repo }}" = false ]; then
55+
app_names=$(curl -s -u "${{ env.DEV_LOGIN_USERNAME }}:${{ env.DEV_LOGIN_PASSWORD }}" \
56+
https://${{ env.DEV_CONTAINER_REGISTRY }}/v2/_catalog \
57+
| jq -c '[.repositories[] | split("/") | .[1]]')
58+
else
59+
app_names='["${{ inputs.app_name}}"]'
60+
fi
61+
4062
echo "app_name=$app_names" >> $GITHUB_OUTPUT
4163
4264
dc-registry-delete-bulk:
@@ -49,6 +71,9 @@ jobs:
4971
DEV_CONTAINER_REGISTRY: ${{ vars.DEV_CONTAINER_REGISTRY }}
5072
DEV_LOGIN_USERNAME: ${{ secrets.DEV_LOGIN_USERNAME }}
5173
DEV_LOGIN_PASSWORD: ${{ secrets.DEV_LOGIN_PASSWORD }}
74+
SSH_PRIVATE_KEY: ${{ secrets.DEV_SERVER_SSH_PRIVATE_KEY }}
75+
SSH_USER: ${{ secrets.DEV_SERVER_SSH_USERNAME }}
76+
SSH_HOST: ${{ secrets.DEV_SERVER_SSH_HOST }}
5277
strategy:
5378
matrix:
5479
app_name: ${{ fromJson(needs.dc-generate-matrix.outputs.app_name) }}
@@ -70,4 +95,8 @@ jobs:
7095
-a ${{ matrix.app_name }} \
7196
-t "" \
7297
-u "${{ env.DEV_LOGIN_USERNAME }}:${{ env.DEV_LOGIN_PASSWORD }}" \
73-
-d "${{ inputs.delete_since_days }}"
98+
-d "${{ inputs.delete_since_days }}" \
99+
-p "${{ env.SSH_PRIVATE_KEY }}" \
100+
-s "${{ env.SSH_USER }}" \
101+
-h "${{ env.SSH_HOST }}" \
102+
-f "${{ github.event.inputs.force_delete_repo }}"

dc/registry_delete.sh

+49-11
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ APP_NAME="hosted-inference-api"
1212
DELETE_TAG=""
1313
HTTPS_USERNAME=""
1414
DELETE_SINCE_DAYS=""
15+
SSH_KEY=""
16+
SSH_USER=""
17+
SSH_HOST=""
18+
FORCE_DELETE_REPO=false
1519

16-
while getopts e:g:r:a:t:u:d: flag
20+
while getopts e:g:r:a:t:u:d:p:s:h:f: flag
1721
do
1822
case "${flag}" in
1923
e) ENVIRONMENT_NAME=${OPTARG};;
@@ -23,6 +27,10 @@ do
2327
t) DELETE_TAG=${OPTARG};;
2428
u) HTTPS_USERNAME=${OPTARG};;
2529
d) DELETE_SINCE_DAYS=${OPTARG};;
30+
p) SSH_KEY=${OPTARG};;
31+
s) SSH_USER=${OPTARG};;
32+
h) SSH_HOST=${OPTARG};;
33+
f) FORCE_DELETE_REPO=${OPTARG};;
2634
esac
2735
done
2836

@@ -38,14 +46,9 @@ function validate_image_tag() {
3846
fi
3947
}
4048

41-
# Delete images older than DELETE_SINCE_DAYS and exit 0
42-
if [ -n "$DELETE_SINCE_DAYS" ]; then
43-
echo "::notice::Deleting images older than $DELETE_SINCE_DAYS days"
44-
repo_tags=$(curl -s -u $HTTPS_USERNAME https://$REGISTRY_URL/v2/$GITHUB_OWNER/$APP_NAME/tags/list | jq -r '.tags[]?')
45-
if [ -z "$repo_tags" ]; then
46-
echo "::notice::No images found for $APP_NAME"
47-
exit 0
48-
fi
49+
function delete_since_days() {
50+
repo_tags=$1
51+
delete_since_days=$2
4952

5053
while IFS= read -r tag; do
5154
manifest=$(curl -s -u $HTTPS_USERNAME \
@@ -61,7 +64,7 @@ if [ -n "$DELETE_SINCE_DAYS" ]; then
6164
current_date=$(date +%s)
6265
days_since=$(( (current_date - created_date) / (60*60*24) ))
6366

64-
if [ $days_since -gt $DELETE_SINCE_DAYS ]; then
67+
if [ $days_since -gt $delete_since_days ]; then
6568
digest=$(curl -s -u $HTTPS_USERNAME \
6669
-H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
6770
https://$REGISTRY_URL/v2/$GITHUB_OWNER/$APP_NAME/manifests/$tag \
@@ -72,10 +75,43 @@ if [ -n "$DELETE_SINCE_DAYS" ]; then
7275
echo "$APP_NAME:$tag is $days_since days old"
7376
fi
7477
done <<< "$repo_tags"
78+
}
79+
80+
function force_delete_repo() {
81+
echo "$SSH_KEY" | ssh $SSH_USER@$SSH_HOST "cd ci-setup && docker exec -i -u root ci-setup_registry_1 bin/registry garbage-collect --delete-untagged /etc/docker/registry/config.yml"
82+
echo "$SSH_KEY" | ssh $SSH_USER@$SSH_HOST "cd ci-setup && docker exec -i -u root ci-setup_registry_1 rm -rf /var/lib/registry/docker/registry/v2/repositories/$GITHUB_OWNER/$APP_NAME"
83+
echo "::warning::force deleted $REGISTRY_URL/$GITHUB_OWNER/$APP_NAME"
84+
}
85+
86+
# Main thread
87+
88+
## Force delete image repository and exit 0
89+
if [ "$FORCE_DELETE_REPO" == "true" ]; then
90+
repo_tags=$(curl -s -u $HTTPS_USERNAME https://$REGISTRY_URL/v2/$GITHUB_OWNER/$APP_NAME/tags/list | jq -r '.tags[]?')
91+
if [ -n "$repo_tags" ]; then
92+
echo "::error::found existing manifests for $REGISTRY_URL/$GITHUB_OWNER/$APP_NAME"
93+
exit 1
94+
fi
95+
96+
delete_since_days "$repo_tags" "0"
97+
force_delete_repo
7598
exit 0
7699
fi
77100

78-
# Delete a specific image and exit 0
101+
## Delete images older than DELETE_SINCE_DAYS and exit 0
102+
if [ -n "$DELETE_SINCE_DAYS" ]; then
103+
echo "::notice::Deleting images older than $DELETE_SINCE_DAYS days"
104+
repo_tags=$(curl -s -u $HTTPS_USERNAME https://$REGISTRY_URL/v2/$GITHUB_OWNER/$APP_NAME/tags/list | jq -r '.tags[]?')
105+
if [ -z "$repo_tags" ]; then
106+
echo "::notice::No images found for $APP_NAME"
107+
exit 0
108+
fi
109+
110+
delete_since_days "$repo_tags" "$DELETE_SINCE_DAYS"
111+
exit 0
112+
fi
113+
114+
## Delete a specific image and exit 0
79115
if [ -n $DELETE_TAG ]; then
80116
manifest=$(curl -s -u $HTTPS_USERNAME \
81117
-H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
@@ -93,3 +129,5 @@ if [ -n $DELETE_TAG ]; then
93129
echo "::warning::deleted $REGISTRY_URL/$GITHUB_OWNER/$APP_NAME:$DELETE_TAG"
94130
exit 0
95131
fi
132+
133+
# End of main thread

0 commit comments

Comments
 (0)