Cleanup Old Cache Images #6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Cleanup Old Cache Images | |
on: | |
schedule: | |
# Run weekly on Sundays at 2 AM UTC | |
- cron: '0 2 * * 0' | |
workflow_dispatch: | |
env: | |
REGISTRY: ghcr.io | |
BASE_IMAGE_NAME: ${{ github.repository }}/base | |
jobs: | |
cleanup: | |
runs-on: ubuntu-latest | |
if: github.repository_owner == 'jazzband' | |
steps: | |
- name: Log in to Container Registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Delete old images | |
run: | | |
# Get all image versions | |
PACKAGE_NAME=$(echo "${{ env.BASE_IMAGE_NAME }}" | cut -d'/' -f2-) | |
# Get all versions of the base image package | |
VERSIONS=$(gh api \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
"/orgs/${{ github.repository_owner }}/packages/container/$PACKAGE_NAME/versions" \ | |
--jq '.[].id' | head -n -5) # Keep the 5 most recent versions | |
# Delete old versions (keep the latest 5) | |
for version_id in $VERSIONS; do | |
echo "Deleting version ID: $version_id" | |
gh api \ | |
--method DELETE \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
"/orgs/${{ github.repository_owner }}/packages/container/$PACKAGE_NAME/versions/$version_id" || true | |
done | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |