Cleanup GHCR Images #3
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 GHCR Images | |
| on: | |
| workflow_dispatch: # Allows manual triggering | |
| schedule: | |
| - cron: '0 2 * * 0' # Runs every Sunday at 2:00 AM UTC | |
| jobs: | |
| cleanup-images: | |
| name: Cleanup GHCR Images | |
| runs-on: ubuntu-latest | |
| permissions: | |
| packages: write | |
| contents: read | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Get all container packages in the organization | |
| id: get-packages | |
| run: | | |
| set +e # Don't exit on error | |
| echo "Attempting to fetch container packages..." | |
| # Try organization packages first | |
| package_names=$(gh api /orgs/DataSQRL/packages?package_type=container --jq '.[].name' 2>&1) | |
| exit_code=$? | |
| if [ $exit_code -ne 0 ]; then | |
| echo "Organization endpoint failed, trying repository endpoint..." | |
| package_names=$(gh api /repos/DataSQRL/flink-sql-runner/packages?package_type=container --jq '.[].name' 2>&1) | |
| exit_code=$? | |
| fi | |
| if [ $exit_code -ne 0 ]; then | |
| echo "Failed to fetch packages from both endpoints" | |
| echo "Error: $package_names" | |
| package_names="" | |
| else | |
| echo "Successfully fetched packages:" | |
| echo "$package_names" | |
| fi | |
| # Save packages to output (will be empty if both failed) | |
| { | |
| echo "packages<<EOF" | |
| echo "$package_names" | |
| echo "EOF" | |
| } >> $GITHUB_OUTPUT | |
| env: | |
| GH_TOKEN: ${{ secrets.PACKAGES_BOT_PAT }} | |
| - name: Delete untagged images | |
| run: | | |
| packages="${{ steps.get-packages.outputs.packages }}" | |
| if [ -z "$packages" ]; then | |
| echo "No packages found to clean up" | |
| exit 0 | |
| fi | |
| for pkg in $packages; do | |
| echo "--- Processing package: $pkg ---" | |
| # Try both organization and repository endpoints | |
| for endpoint in "/orgs/DataSQRL" "/repos/DataSQRL/flink-sql-runner"; do | |
| echo "Trying endpoint: $endpoint" | |
| # Delete untagged images | |
| echo "Fetching untagged images for $pkg..." | |
| untagged_ids=$(gh api --paginate "$endpoint/packages/container/$pkg/versions" | jq '.[] | select(.metadata.container.tags | length == 0) | .id' 2>/dev/null || echo "") | |
| if [ -n "$untagged_ids" ]; then | |
| echo "Deleting untagged images for $pkg:" | |
| echo "$untagged_ids" | xargs -I {} gh api --method DELETE "$endpoint/packages/container/$pkg/versions/{}" --silent || echo "Could not delete some untagged images for $pkg. This might be due to download counts or other restrictions." | |
| else | |
| echo "No untagged images found for $pkg." | |
| fi | |
| # If we found versions, break out of the endpoint loop | |
| if [ -n "$untagged_ids" ]; then | |
| break | |
| fi | |
| done | |
| echo "--- Finished processing $pkg ---" | |
| done | |
| env: | |
| GH_TOKEN: ${{ secrets.PACKAGES_BOT_PAT }} |