add: check.sh

This commit is contained in:
2023-03-11 20:02:00 +01:00
parent bb00af0368
commit 7f38199c45

53
check.sh Normal file
View File

@@ -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