Skip to content

Instantly share code, notes, and snippets.

@siutin
Created October 30, 2024 07:46
Show Gist options
  • Save siutin/f18a101c694e35136df527e1fe8f7791 to your computer and use it in GitHub Desktop.
Save siutin/f18a101c694e35136df527e1fe8f7791 to your computer and use it in GitHub Desktop.
#!/bin/bash
if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then
echo "Usage: $0 <repository_name> [output_file]"
exit 1
fi
REPOSITORY_NAME="$1"
OUTPUT_FILE="$2"
BASE_URL="https://registry.hub.docker.com/v2/repositories/$REPOSITORY_NAME/tags"
PAGE=1
PAGE_SIZE=100
TOTAL_COUNT=$(curl -s "$BASE_URL" | jq -r '.count')
# --
declare -a TAGS_ARRAY
while [ $(( (PAGE - 1) * PAGE_SIZE )) -lt "$TOTAL_COUNT" ]; do
TAGS=$(curl -s "$BASE_URL?page=$PAGE&page_size=$PAGE_SIZE" | jq -r '.results[] | {name: .name, last_pushed: .tag_last_pushed}')
TAGS_ARRAY+=("$TAGS")
PAGE=$((PAGE + 1))
done
SORTED_TAGS=$(printf "%s\n" "${TAGS_ARRAY[@]}" | jq -s 'sort_by(.last_pushed) | reverse')
ALL_TAGS=""
for tag in $(echo "$SORTED_TAGS" | jq -r '.[] | .name'); do
ALL_TAGS+="$tag"$'\n'
done
# --
echo -e "$ALL_TAGS"
if [ -n "$OUTPUT_FILE" ]; then
echo -e "$ALL_TAGS" > "$OUTPUT_FILE"
echo "All tag names have been saved to $OUTPUT_FILE."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment