version.sh 1.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#!/bin/sh
echo "
         ######################################
         ###        Base image test          ##
         ######################################
"

# If parameter is not path do dockerfile
if [ "$1" = "" ]; then
    DOCKERFILE=$BASE/Dockerfile
else
    DOCKERFILE=$1
fi

# Check for command existence
# See: https://www.shivering-isles.com/helpful-shell-snippets-for-docker-testing-and-bootstrapping/
17
command_exists() { command -v $1 >/dev/null 2>&1 || { echo >&2 "I require $1 but it's not installed.  Aborting."; exit 1; }; }
18 19 20

# Docker latest version tag
# See: https://www.shivering-isles.com/helpful-shell-snippets-for-docker-testing-and-bootstrapping/
21
docker_image_latest_tag() { REPO=$1; [ $(echo $REPO | grep '/' | wc -l) -eq 0 ] && REPO=library/$1; wget -q -O- https://registry.hub.docker.com/v2/repositories/${REPO}/tags\?page\=1\&page_size\=250 | jq .results[].name | sort -V | sed -e 's/"//g' | grep -P "^8" | grep -P '^[[:digit:]]+((.[[:digit:]]+)?.[[:digit:]]+$)' | tail -1; }
22 23 24 25 26 27 28 29 30 31 32 33 34

# Version comparison greater or equal
# See: https://www.shivering-isles.com/helpful-shell-snippets-for-docker-testing-and-bootstrapping/
version_ge() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1" || test "$1" = "$2"; }

# Docker base image version
# See: https://www.shivering-isles.com/helpful-shell-snippets-for-docker-testing-and-bootstrapping/
docker_base_version() { cat "$1" | grep FROM | sed -e "s/FROM.*://g"; }

# Docker base image name
# See: https://www.shivering-isles.com/helpful-shell-snippets-for-docker-testing-and-bootstrapping/
docker_base_name() { cat "$1" | grep FROM | sed -e "s/FROM[^[:alpha:]]//g" -e "s/:.*//g"; }

35
command_exists wget
36

37 38 39
LATEST=$(docker_image_latest_tag `docker_base_name "$DOCKERFILE"`)
CURRENT=$(docker_base_version "$DOCKERFILE")

Sheogorath's avatar
Sheogorath committed
40
version_ge "$CURRENT" "$LATEST" && echo "$DOCKERFILE: Base image is up to date! Test successful." || { echo >&2 "$DOCKERFILE: A newer base image is available! Please update to version ${LATEST}."; exit 1; }