Skip to content

Instantly share code, notes, and snippets.

@hypernova7
Created December 31, 2022 00:49
Show Gist options
  • Save hypernova7/15e075ecf599a3f07c481465c114faf6 to your computer and use it in GitHub Desktop.
Save hypernova7/15e075ecf599a3f07c481465c114faf6 to your computer and use it in GitHub Desktop.
Check image updates from Docker Hub
#!/usr/bin/env sh
# Check image updates from Docker Hub
# Credits: Eddy Otsutsuki <https://github.com/hypernova7>
get_image_data() {
curl \
--silent \
"https://registry.hub.docker.com/v2/repositories/$1/tags?page_size=100&ordering=last_updated" | jq \
-r "$2"
}
new_update() {
local result=false
local limit_time=86400 # 24hrs in seconds
local current_time=`date +"%s"` # Convert current time to seconds
local user=$(echo $1 | cut -d '/' -f1) # Get user
local repo=$(echo $1 | cut -d '/' -f2) # Get repo
local version=$([ -z "$2" ] && echo "latest" || echo $2) # Use "latest" if a version is not specified
user=$([ "$user" = "_" ] && echo "library" || echo $user) # Convert "_" to "library"
local image="$user/$repo"
# Get last updated time by version
local last_updated=`get_image_data $image ".results[] | select(.name==\"$version\") | .last_updated"`
# Check if image date is not "null"
if [ "$last_updated" != "null" ]; then
local last_updated_time=`date -d "$last_updated" +"%s"` # Convert "last_updated" time to seconds
local days=$((($current_time - $last_updated_time) / $limit_time)) # days passed
# Check that "days" is less than 1 day
if [ $days -lt 1 ]; then
result=true
fi
else
result="IMAGE_NOT_FOUND"
fi
echo $result
}
# Usage:
is_new_update=`new_update "_/alpine"`
echo $is_new_update
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment