Created
April 10, 2025 15:56
-
-
Save manzke/f67be624dc63986a39898184d9f53fb9 to your computer and use it in GitHub Desktop.
Extract the information about images in your harbor, so you can identify images, which haven't been used and get rid of them
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
#!/bin/bash | |
# Harbor Configuration | |
HARBOR_URL="https://myharbor.com" | |
HARBOR_USER="my_robot_user" | |
HARBOR_PASS="my_robot_users_password" | |
OUTPUT_CSV="harbor_images.csv" | |
SORTED_CSV="sorted_harbor_images.csv" | |
LOG_FILE="harbor_debug.log" | |
# Initialize files | |
echo "project,repository,tag,last_pull_time,push_time,size_bytes,digest" > "$OUTPUT_CSV" | |
echo "Debug Log - $(date)" > "$LOG_FILE" | |
# Fetch all projects | |
projects=$(curl -s -u "${HARBOR_USER}:${HARBOR_PASS}" \ | |
"${HARBOR_URL}/api/v2.0/projects?page_size=100" \ | |
| jq -r '.[].name') | |
# Loop through each project | |
for PROJECT in $projects; do | |
# Fetch all repositories for the project | |
repos=$(curl -s -u "${HARBOR_USER}:${HARBOR_PASS}" \ | |
"${HARBOR_URL}/api/v2.0/projects/${PROJECT}/repositories?page_size=100" \ | |
| jq -r '.[].name' | sed "s|^${PROJECT}/||") | |
# Loop through each repository | |
for REPO in $repos; do | |
# Fetch artifact details | |
RESPONSE=$(curl -s -u "${HARBOR_USER}:${HARBOR_PASS}" \ | |
"${HARBOR_URL}/api/v2.0/projects/${PROJECT}/repositories/${REPO}/artifacts?page_size=100") | |
# Check for errors in response | |
if echo "$RESPONSE" | jq -e '.errors' > /dev/null; then | |
echo "API Error for project: $PROJECT, repo: $REPO" >> "$LOG_FILE" | |
echo "$RESPONSE" >> "$LOG_FILE" | |
echo "----" >> "$LOG_FILE" | |
continue | |
fi | |
# Parse JSON safely with additional fields | |
echo "$RESPONSE" | jq -r --arg PROJECT "$PROJECT" --arg REPO "$REPO" ' | |
.[] | [ | |
$PROJECT, | |
$REPO, | |
(.tags // [] | if length > 0 then .[0].name else "untagged" end), | |
(.pull_time // "1970-01-01T00:00:00Z"), | |
(.push_time // "1970-01-01T00:00:00Z"), | |
(.size // 0), | |
(.digest // "N/A") | |
] | @csv | |
' >> "$OUTPUT_CSV" 2>> "$LOG_FILE" | |
done | |
done | |
# Sort CSV by last_pull_time (newest first) | |
sort -t, -k4 -r "$OUTPUT_CSV" > "$SORTED_CSV" | |
# Output result | |
echo "✅ Report successfully generated: $SORTED_CSV" | |
echo "📌 Check '$LOG_FILE' for any remaining issues." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment