Skip to content

Instantly share code, notes, and snippets.

@akisys
Last active July 27, 2023 18:25
Show Gist options
  • Save akisys/c0b67ab4bee40fa5c725ed2fcd5cffad to your computer and use it in GitHub Desktop.
Save akisys/c0b67ab4bee40fa5c725ed2fcd5cffad to your computer and use it in GitHub Desktop.
docker-hub-tags-retrieval
#!/usr/bin/env bash
set -fue
[ "${DEBUG:-"NO"}" = "YES" ] && set -x
_ZIP=0
_DOCKERFILE=0
case "${1:-""}" in
"-m") # zip image name with tags
_ZIP=1
shift
;;
"-d") # generate a fake Dockerfile
_DOCKERFILE=1
_ZIP=1
shift
;;
esac
if [ "${1:-""}" = "" ]; then
(>&2 echo "Usage: ${0} [repo/]image") && exit 1
fi
# Full repo/image was supplied
if [[ "${1}" = *"/"* ]]; then
name="${1}"
# Only image was supplied, default to library/image
else
name="library/${1}"
fi
results=$(curl --silent "https://registry.hub.docker.com/v2/repositories/${name}/tags/?page=1" 2>/dev/null | jq -r '."results"[]["name"]' 2>/dev/null)
if [ -z "${results}" ]; then
(>&2 echo "Could not retrieve any data for image ${name}") && exit 2
fi
sorted=$(echo "${results}" | sort)
output=$sorted
if [ $_ZIP -eq 1 ]; then
zipped=$(
echo "${sorted}" | awk -v imagename="${name}" '{print imagename":"$1}'
)
output=$zipped
else
zipped=""
fi
if [ $_DOCKERFILE -eq 1 ]; then
_build_data=$(
echo "${zipped}" | awk '{sum+=1}; {print "FROM",$1,"as stage"sum}'
)
output=$_build_data
fi
printf "%s\n" "${output}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment