diff --git a/entrypoint.sh b/entrypoint.sh index 959afa438dfe502dca8be2c652a2ca4a1fa69095..2bb04a0a00cef24446716fe4d9f26e2b3b6defa9 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,41 +1,83 @@ #!/bin/sh # To/Die/For =) -unset TO_DIE +unset SHOULD_DIE +should_die() { + RED='\033[0;31m' + NC='\033[0m' # No Color + echo -e "${RED}$1${NC}" + SHOULD_DIE=1 +} die() { RED='\033[0;31m' NC='\033[0m' # No Color echo -e "${RED}$1${NC}" - TO_DIE=1 + test -z "${RETAG_DEBUG}" || exit 1 } + ########################################################################################################################## -test -z "${CI_REGISTRY}" && die "CI_REGISTRY is missing" -test -z "${CI_REGISTRY_USER}" && die "CI_REGISTRY_USER is missing" -test -z "${CI_REGISTRY_PASSWORD}" && die "CI_REGISTRY_PASSWORD is missing" -test -z "${CI_PROJECT_PATH}" && die "CI_PROJECT_PATH is missing" -test -z "${REGISTRY_TAG_OLD}" && die "REGISTRY_TAG_OLD is missing" +# Check if env vars were set +test -z "${CI_SERVER_HOST}" && should_die "CI_SERVER_HOST is missing" +test -z "${CI_REGISTRY}" && should_die "CI_REGISTRY is missing" +test -z "${CI_REGISTRY_USER}" && should_die "CI_REGISTRY_USER is missing" +test -z "${CI_REGISTRY_PASSWORD}" && should_die "CI_REGISTRY_PASSWORD is missing" +test -z "${CI_PROJECT_PATH}" && should_die "CI_PROJECT_PATH is missing" +test -z "${CI_COMMIT_SHA}" && should_die "CI_COMMIT_SHA is missing" +# Define default custom image name +test -z "${CUSTOM_IMAGENAME}" && test -z "${CI_COMMIT_TAG}" && CUSTOM_IMAGENAME="${CI_COMMIT_REF_SLUG}" test -z "${CUSTOM_IMAGENAME}" && IMAGE_PATH="${CI_PROJECT_PATH}" || IMAGE_PATH="${CI_PROJECT_PATH}/${CUSTOM_IMAGENAME}" + +# Define default image old tag +test -z "${REGISTRY_TAG_OLD}" && test -z "${CI_COMMIT_TAG}" && REGISTRY_TAG_OLD="${CI_COMMIT_SHA}" || REGISTRY_TAG_OLD="${CI_COMMIT_TAG}" + +# Define default image new tag test -z "${REGISTRY_TAG_NEW}" && REGISTRY_TAG_NEW=latest ########################################################################################################################## -test -n "${TO_DIE}" && exit 1 +test -n "${SHOULD_DIE}" && die "Incomplete config!" +########################################################################################################################## + +# Set content type +CONTENT_TYPE="Content-Type: application/vnd.docker.distribution.manifest.v2+json" +# Set manifest root url +MANIFEST_URL=${REGISTRY_SCHEMA:-https}://${CI_REGISTRY}/v2/${IMAGE_PATH}/manifests + +########################################################################################################################## +TEMPFILE=$(mktemp) ########################################################################################################################## -TEMPFILE="$(mktemp)" -CONTENT_TYPE="application/vnd.docker.distribution.manifest.v2+json" +# DEBUG +test -z "${RETAG_DEBUG}" || echo "Old image: ${CI_REGISTRY}/${IMAGE_PATH}:${REGISTRY_TAG_OLD}" -TOKEN="$(curl --user \"${CI_REGISTRY_USER}:${CI_REGISTRY_PASSWORD}\" \"${SERVER_HOST_SCHEMA:-https}://${CI_SERVER_HOST}/jwt/auth?offline_token=true&service=container_registry&scope=repository:${CI_PROJECT_PATH}:push,pull\" 2> \"${TEMPFILE}\" | jq -r .token)" -test -z "${TOKEN}" && ( cat "${TEMPFILE}"; ( test -z "${RETAG_DEBUG}" || die "Couldn't get token" ) ) +# Fetch JWT token +TOKEN=$(curl -s \ + --user ${CI_REGISTRY_USER}:${CI_REGISTRY_PASSWORD} \ + -G ${GITLAB_SCHEMA:-https}://${CI_SERVER_HOST}/jwt/auth \ + -d service=container_registry \ + -d scope="repository:${IMAGE_PATH}:push,pull,delete" \ + | jq -r '.token') +test -n "${TOKEN}" || die "Couldn't get token" test -z "${RETAG_DEBUG}" || echo "Token: [${TOKEN}]" +BEARER="Authorization:Bearer $TOKEN" -MANIFEST="$(curl -H \"Content-Type: ${CONTENT_TYPE}\" -H \"Authorization: Bearer ${TOKEN}\" \"${REGISTRY_SCHEMA:-https}://${CI_REGISTRY}/v2/${IMAGE_PATH}/manifests/${REGISTRY_TAG_OLD}\" 2> \"${TEMPFILE}\")" -test -z "${MANIFEST}" && ( cat "${TEMPFILE}"; ( test -z "${RETAG_DEBUG}" || die "Couldn't get manifest" ) ) +# Fetch image manifest +MANIFEST=$(curl -s \ + -H "${BEARER}" \ + -H "${CONTENT_TYPE}" \ + -G ${MANIFEST_URL}/${REGISTRY_TAG_OLD}) +test -n "${MANIFEST}" || die "Couldn't get manifest" test -z "${RETAG_DEBUG}" || echo "Manifest: [${MANIFEST}]" -RESPONCE="$(curl -H \"Content-Type: ${CONTENT_TYPE}\" -H \"Authorization: Bearer ${TOKEN}\" \"${REGISTRY_SCHEMA:-https}://${CI_REGISTRY}/v2/${IMAGE_PATH}/manifests/${REGISTRY_TAG_NEW}\" -X PUT -d \"${MANIFEST}\" 2> \"${TEMPFILE}\")" -test "$?" -gt 0 && ( cat "${TEMPFILE}"; ( test -z "${RETAG_DEBUG}" || die "Couldn't retag image" ) ) +# Push image manifest +RESPONCE=$(curl -s \ + -H "${BEARER}" \ + -H "${CONTENT_TYPE}" \ + -G ${MANIFEST_URL}/${REGISTRY_TAG_NEW} \ + -X PUT \ + -d "$MANIFEST") +test -z "${RESPONCE}" || die "Couldn't get responce" test -z "${RETAG_DEBUG}" || echo "Responce: [${RESPONCE}]" echo "Image tag: ${CI_REGISTRY}/${IMAGE_PATH}:${REGISTRY_TAG_NEW}"