Skip to content

Commit 7d6eab2

Browse files
feat: DEV Registry delete workflows (#19)
* style: formats and phrases * style(logs): skip notifying tag age * perf: add delete_tag input support * fix: remove unneeded restrictions * fix: always run dc-registry-delete on dev * test: clone cicd-deployment-scripts@dc-registry-delete * perf: use gh runner * fix: use DEV_CONTAINER_REGISTRY instead of CONTAINER_REGISTRY * style(logs): verbose error logging * fix: add shell=bash to dc-registry-delete run step * fix: repo_tags iteration syntax * perf: add validate_image_tag function * fix: syntax error * fix: fetching manifest created dttm * test: debug manifest * test: debug manifest * fix: argument passing to validate_image_tag * perf: function failure exit * fix: quote if test clause vars * style(logs): improve error messages * perf: silent curl DELETE * fix: digest calculation * perf: app_name input * perf: checkout cicd repo instead of cloning it * perf: fetch-depth on registry delete * fix: cicd repo checkout path * perf: align dc_registry_delete to dev ref
1 parent 520bb68 commit 7d6eab2

File tree

4 files changed

+148
-5
lines changed

4 files changed

+148
-5
lines changed
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: 'DC: Delete Registry Images'
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
delete_since_days:
7+
required: false
8+
type: string
9+
description: "Delete images older than the specified number of days and exit gracefully"
10+
default: ''
11+
delete_tag:
12+
required: false
13+
type: string
14+
description: "Delete images with the specified tag and exit gracefully"
15+
default: ''
16+
app_name:
17+
required: false
18+
type: string
19+
description: "Application name - provided by the caller unless called from an application repository"
20+
default: ''
21+
22+
env:
23+
GH_TOKEN: ${{ secrets.GH_TOKEN }}
24+
25+
jobs:
26+
dc-registry-delete:
27+
name: 'Docker Compose: Delete Registry Images'
28+
runs-on: ubuntu-latest
29+
environment: dev
30+
env:
31+
ENVIRONMENT_NAME: dev
32+
REGISTRY_HTTPS_USERNAME: "${{ secrets.REGISTRY_HTTPS_USERNAME }}"
33+
DEV_CONTAINER_REGISTRY: ${{ vars.DEV_CONTAINER_REGISTRY }}
34+
DEV_LOGIN_USERNAME: ${{ secrets.DEV_LOGIN_USERNAME }}
35+
DEV_LOGIN_PASSWORD: ${{ secrets.DEV_LOGIN_PASSWORD }}
36+
steps:
37+
# Checkout the repository to the GitHub Actions runner
38+
- name: Checkout
39+
uses: actions/checkout@v4
40+
with:
41+
repository: 'code-kern-ai/cicd-deployment-scripts'
42+
43+
- name: Perform Registry Image Deletion
44+
shell: bash
45+
run: |
46+
bash ./dc/registry_delete.sh \
47+
-e ${{ env.ENVIRONMENT_NAME }} \
48+
-g ${{ github.repository_owner }} \
49+
-r ${{ env.DEV_CONTAINER_REGISTRY }} \
50+
-a ${{ inputs.app_name || github.event.repository.name }} \
51+
-t "${{ inputs.delete_tag }}" \
52+
-u "${{ env.DEV_LOGIN_USERNAME }}:${{ env.DEV_LOGIN_PASSWORD }}" \
53+
-d "${{ inputs.delete_since_days }}"

.github/workflows/pi_merge_parent_image.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ jobs:
9797
platform=linux/arm64
9898
label=dockerfile-path=https://github.com/refinery-${{ env.PARENT_IMAGE_TYPE }}-parent-image/blob/${{ github.sha }}/Dockerfile
9999
100-
- name: Build & Push ${{ env.PARENT_IMAGE_NAME }}:sha-${{ env.PARENT_IMAGE_TYPE }}
100+
- name: Build & Push ${{ env.PARENT_IMAGE_NAME }}:${{ github.sha }}-${{ env.PARENT_IMAGE_TYPE }}
101101
uses: docker/build-push-action@v5
102102
with:
103103
context: .
@@ -111,7 +111,7 @@ jobs:
111111
platform=linux/amd64
112112
label=dockerfile-path=https://github.com/refinery-${{ env.PARENT_IMAGE_TYPE }}-parent-image/blob/${{ github.sha }}/Dockerfile
113113
114-
- name: Build & Push ${{ env.PARENT_IMAGE_NAME }}:sha-${{ env.PARENT_IMAGE_TYPE }}-arm64
114+
- name: Build & Push ${{ env.PARENT_IMAGE_NAME }}:${{ github.sha }}-${{ env.PARENT_IMAGE_TYPE }}-arm64
115115
uses: docker/build-push-action@v5
116116
with:
117117
context: .

.github/workflows/pi_merge_submodule.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ jobs:
8686
needs: [pi-matrix, pi-update-submodule]
8787
if: ${{ !failure() }}
8888
runs-on: ubuntu-latest
89-
strategy:
90-
matrix:
91-
parent_image_type: ${{ fromJson(needs.pi-matrix.outputs.parent_image_type) }}
89+
# strategy:
90+
# matrix:
91+
# parent_image_type: ${{ fromJson(needs.pi-matrix.outputs.parent_image_type) }}
9292
steps:
9393
- name: Checkout repository
9494
uses: actions/checkout@v4

dc/registry_delete.sh

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# !/bin/bash
2+
3+
trap "exit 1" TERM
4+
export TOP_PID=$$
5+
6+
set -e
7+
8+
ENVIRONMENT_NAME="dev"
9+
GITHUB_OWNER="code-kern-ai"
10+
REGISTRY_URL="registry.dev.kern.ai"
11+
APP_NAME="hosted-inference-api"
12+
DELETE_TAG=""
13+
HTTPS_USERNAME=""
14+
DELETE_SINCE_DAYS=""
15+
16+
while getopts e:g:r:a:t:u:d: flag
17+
do
18+
case "${flag}" in
19+
e) ENVIRONMENT_NAME=${OPTARG};;
20+
g) GITHUB_OWNER=${OPTARG};;
21+
r) REGISTRY_URL=${OPTARG};;
22+
a) APP_NAME=${OPTARG};;
23+
t) DELETE_TAG=${OPTARG};;
24+
u) HTTPS_USERNAME=${OPTARG};;
25+
d) DELETE_SINCE_DAYS=${OPTARG};;
26+
esac
27+
done
28+
29+
30+
function validate_image_tag() {
31+
manifest=$1
32+
image=$2
33+
errors=$(echo $manifest | jq -r '.errors')
34+
if [ "$errors" != "null" ]; then
35+
echo "::error::$image => $(echo $errors | jq -r '.[0].code')"
36+
echo $manifest | jq -rc '.errors'
37+
kill -s TERM $TOP_PID
38+
fi
39+
}
40+
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+
while IFS= read -r tag; do
46+
manifest=$(curl -s -u $HTTPS_USERNAME \
47+
-H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
48+
https://$REGISTRY_URL/v2/$GITHUB_OWNER/$APP_NAME/manifests/$tag)
49+
50+
validate_image_tag "$manifest" "$REGISTRY_URL/$GITHUB_OWNER/$APP_NAME:$tag"
51+
52+
created=$(curl -s -u $HTTPS_USERNAME \
53+
https://$REGISTRY_URL/v2/$GITHUB_OWNER/$APP_NAME/manifests/$tag \
54+
| jq -r '.history[0].v1Compatibility' | jq -r '.created')
55+
created_date=$(date -d $created +%s)
56+
current_date=$(date +%s)
57+
days_since=$(( (current_date - created_date) / (60*60*24) ))
58+
59+
if [ $days_since -gt $DELETE_SINCE_DAYS ]; then
60+
digest=$(curl -s -u $HTTPS_USERNAME \
61+
-H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
62+
https://$REGISTRY_URL/v2/$GITHUB_OWNER/$APP_NAME/manifests/$tag \
63+
| sha256sum | cut -d ' ' -f 1)
64+
curl -X DELETE -u $HTTPS_USERNAME -s https://$REGISTRY_URL/v2/$GITHUB_OWNER/$APP_NAME/manifests/sha256:$digest
65+
echo "::warning::deleted $APP_NAME:$tag, $days_since days old"
66+
else
67+
echo "$APP_NAME:$tag is $days_since days old"
68+
fi
69+
done <<< "$repo_tags"
70+
exit 0
71+
fi
72+
73+
# Delete a specific image and exit 0
74+
if [ -n $DELETE_TAG ]; then
75+
manifest=$(curl -s -u $HTTPS_USERNAME \
76+
-H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
77+
https://$REGISTRY_URL/v2/$GITHUB_OWNER/$APP_NAME/manifests/$DELETE_TAG)
78+
79+
validate_image_tag "$manifest" "$REGISTRY_URL/$GITHUB_OWNER/$APP_NAME:$DELETE_TAG"
80+
81+
digest=$(curl -s -u $HTTPS_USERNAME \
82+
-H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
83+
https://$REGISTRY_URL/v2/$GITHUB_OWNER/$APP_NAME/manifests/$DELETE_TAG \
84+
| sha256sum | cut -d ' ' -f 1)
85+
curl -X DELETE -u $HTTPS_USERNAME -s \
86+
https://$REGISTRY_URL/v2/$GITHUB_OWNER/$APP_NAME/manifests/sha256:$digest
87+
88+
echo "::warning::deleted $REGISTRY_URL/$GITHUB_OWNER/$APP_NAME:$DELETE_TAG"
89+
exit 0
90+
fi

0 commit comments

Comments
 (0)