Delete Old Container 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: Delete Old Container Images | |
| on: | |
| workflow_dispatch: # Allow manual run | |
| schedule: | |
| - cron: "0 0 * * 0" # Run weekly on Sunday at midnight | |
| jobs: | |
| clean-ghcr: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| packages: write | |
| steps: | |
| - name: Bulk Delete Untagged Images | |
| env: | |
| # Use GH_TOKEN explicitly for the CLI to avoid ambiguity | |
| GH_TOKEN: ${{ secrets.GHCR_DELETE_PAT }} | |
| run: | | |
| # Disable immediate exit to handle errors manually | |
| set +e | |
| PKG_NAMES=("meshrf" "meshrf-rf-engine" "meshrf-opentopodata") | |
| OWNER="${{ github.repository_owner }}" | |
| echo "Checking owner type for: $OWNER" | |
| TYPE=$(gh api "users/$OWNER" --jq '.type' 2>/dev/null || echo "Organization") | |
| echo "Owner Type: $TYPE" | |
| if [[ "$TYPE" == "Organization" ]]; then | |
| ENDPOINT="orgs/$OWNER" | |
| else | |
| ENDPOINT="users/$OWNER" | |
| fi | |
| echo "Using endpoint base: $ENDPOINT" | |
| for PKG in "${PKG_NAMES[@]}"; do | |
| echo "----------------------------------------" | |
| echo "Cleaning package: $PKG" | |
| echo "----------------------------------------" | |
| for batch in {1..10}; do | |
| echo "Processing Batch $batch..." | |
| # Fetch IDs | |
| IDS=$(gh api "$ENDPOINT/packages/container/$PKG/versions?per_page=100" \ | |
| --jq '.[] | select(.metadata.container.tags | length == 0) | .id' 2>/dev/null) | |
| if [ -z "$IDS" ]; then | |
| echo "✅ No more untagged versions found for $PKG." | |
| break | |
| fi | |
| count=0 | |
| for ID in $IDS; do | |
| echo "Attempting to delete version ID: $ID" | |
| # Capture output and exit code | |
| OUTPUT=$(gh api --method DELETE "$ENDPOINT/packages/container/$PKG/versions/$ID" 2>&1) | |
| EXIT_CODE=$? | |
| if [ $EXIT_CODE -ne 0 ]; then | |
| echo "❌ Failed to delete $ID. Exit Code: $EXIT_CODE" | |
| echo " Error Output: $OUTPUT" | |
| echo " Troubleshooting: Ensure GHCR_DELETE_PAT has 'delete:packages' scope." | |
| else | |
| echo " Deleted successfully." | |
| ((count++)) | |
| fi | |
| done | |
| echo "batch finished: Processed $count successful deletions." | |
| done | |
| done | |
| # Always succeed the step so we can see the logs | |
| exit 0 |