Skip to content

Commit

Permalink
Cleanup e2e images periodically
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandr Demicev <[email protected]>
  • Loading branch information
alexander-demicev committed Dec 18, 2024
1 parent e535531 commit a625ece
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/cleanup-e2e-images.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Cleanup e2e images

on:
schedule:
- cron: '0 0 * * 0'
workflow_dispatch:

env:
ORG: rancher
REPO: turtles
PACKAGE: turtles-e2e

jobs:
delete_old_versions:
runs-on: ubuntu-latest
permissions:
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Run delete old versions script
run: ./scripts/cleanup-test-images.sh ${{ env.ORG }} ${{ env.REPO }} ${{ env.PACKAGE }} ${{ secrets.GITHUB_TOKEN }}
33 changes: 33 additions & 0 deletions scripts/cleanup-test-images.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash

if [ "$#" -ne 4 ]; then
echo "Usage: $0 <org> <repo> <package-name> <github-token>"
exit 1
fi

ORG="$1"
REPO="$2"
PACKAGE_NAME="$3"
GITHUB_TOKEN="$4"

API_URL="https://api.github.com/orgs/$ORG/packages/container/$PACKAGE_NAME/versions?per_page=100"

CURRENT_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ")

response=$(curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"$API_URL")

echo "$response" | jq -r \
--arg current_date "$CURRENT_DATE" \
'map(select(.created_at < $current_date)) | .[] | .id' | while read version_id; do
echo "Deleting version: $version_id"
curl -s -X DELETE \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/orgs/$ORG/packages/container/$PACKAGE_NAME/versions/$version_id"
done

echo "Deletion process complete."

0 comments on commit a625ece

Please sign in to comment.