From 7f38199c452dfe82bead94583f20d61c1eec654c Mon Sep 17 00:00:00 2001 From: vista Date: Sat, 11 Mar 2023 20:02:00 +0100 Subject: [PATCH] add: check.sh --- check.sh | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 check.sh diff --git a/check.sh b/check.sh new file mode 100644 index 0000000..be8857f --- /dev/null +++ b/check.sh @@ -0,0 +1,53 @@ +#! /usr/bin/env bash + +set -e + +if [ -z "${BASE}" ]; then + echo "the environment variable BASE is not set" + exit 1 +fi + +if [ -z "${TARGET}" ]; then + echo "the environment variable TARGET is not set" + exit 1 +fi + + +# add "library/" if its a "official" docker image (it has no username) +echo "${BASE}" | grep -q "/" || BASE="library/${BASE}" +echo "${TARGET}" | grep -q "/" || TARGET="library/${TARGET}" + + +# move tha tag to its own var and remove it from the repo name +BASE_TAG=$(sed 's/[^:]*://' <<< $BASE) +TARGET_TAG=$(sed 's/[^:]*://' <<< $TARGET) + +BASE_REPO=$(sed 's/:.*//' <<< $BASE) +TARGET_REPO=$(sed 's/:.*//' <<< $TARGET) + +BASE_URL="https://hub.docker.com/v2/repositories/${BASE_REPO}/tags/${BASE_TAG}" +TARGET_URL="https://hub.docker.com/v2/repositories/${TARGET_REPO}/tags/${TARGET_TAG}" + + +# login if credentias are passed +HEADER="" +if [[ ! -z "${DOCKER_USERNAME}" && ! -z "${DOCKER_PASSWORD}" ]]; then + TOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d '{"username": "'${DOCKER_USERNAME}'", "password": "'${DOCKER_PASSWORD}'"}' https://hub.docker.com/v2/users/login/ | jq -r .token) + HEADER="Authorization: JWT ${TOKEN}" +fi + + +# compare the update time of the BASE and TARGET images +BASE_DATE=$(curl -s -H "${HEADER}" "${BASE_URL}" | jq -r .last_updated ) +TARGET_DATE=$(curl -s -H "${HEADER}" "${TARGET_URL}" | jq -r .last_updated) + +BASE_TIMESTAMP=$(date -d "${BASE_DATE}" +%s) +TARGET_TIMESTAMP=$(date -d "${TARGET_DATE}" +%s) + +if [ $BASE_TIMESTAMP -le $TARGET_TIMESTAMP ]; then + echo "no update needed" + exit 1 +fi + +echo "$TARGET needs updating" +exit 0