Skip to content

Instantly share code, notes, and snippets.

@horothesun
Last active June 29, 2023 13:00
Show Gist options
  • Save horothesun/4d35a51e195a42a2545d2bbf0e4c6208 to your computer and use it in GitHub Desktop.
Save horothesun/4d35a51e195a42a2545d2bbf0e4c6208 to your computer and use it in GitHub Desktop.
Get Docker Hub repo tags
#!/bin/bash
# Call by running
# ./get_docker_hub_tags.sh "library" "alpine"
[[ -z "${DOCKER_HUB_TOKEN}" ]] && echo "Error: DOCKER_HUB_TOKEN must be defined" && exit 100
export DOCKER_NAMESPACE="$1"
export DOCKER_REPOSITORY="$2"
[[ -z "${DOCKER_NAMESPACE}" ]] && echo "Error: a Docker namespace must be passed as first argument" && exit 10
[[ -z "${DOCKER_REPOSITORY}" ]] && echo "Error: a Docker repository must be passed as second argument" && exit 20
function get_repo_tags() {
PAGE_SIZE="$1"
PAGE_NUMBER="$2"
DOCKER_TAGS_URL="https://hub.docker.com/v2/namespaces/${DOCKER_NAMESPACE}/repositories/${DOCKER_REPOSITORY}/tags"
curl --silent \
--header "Authorization: Bearer ${DOCKER_HUB_TOKEN}" \
"${DOCKER_TAGS_URL}?page_size=${PAGE_SIZE}&page=${PAGE_NUMBER}"
}
export -f get_repo_tags
REPOSITORY_TAGS_COUNT=$(get_repo_tags 1 1 | jq --raw-output '.count')
export MAX_REPOSITORY_TAGS_PAGE_SIZE=100
export NUMBER_OF_PAGES=$(((REPOSITORY_TAGS_COUNT + MAX_REPOSITORY_TAGS_PAGE_SIZE - 1) / MAX_REPOSITORY_TAGS_PAGE_SIZE))
MAX_CONCURRENT_REQUESTS=32
seq "${NUMBER_OF_PAGES}" |\
xargs \
-P "${MAX_CONCURRENT_REQUESTS}" \
-I ^ \
bash -c "
get_repo_tags ${MAX_REPOSITORY_TAGS_PAGE_SIZE} ^ |\
jq --raw-output '.results[] | .name'
"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment