From 85f0cab8b4c36bd05a3598593a1b90f07bb8d8a5 Mon Sep 17 00:00:00 2001 From: Jim Campbell Date: Fri, 23 Aug 2024 15:19:59 -0400 Subject: [PATCH] new tag release file for single repo apps --- bin/tag-image-singlerepo | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100755 bin/tag-image-singlerepo diff --git a/bin/tag-image-singlerepo b/bin/tag-image-singlerepo new file mode 100755 index 0000000..1e9cf15 --- /dev/null +++ b/bin/tag-image-singlerepo @@ -0,0 +1,33 @@ +#!/bin/bash +# tag an existing image without needing to pull it first +set -u + +REGISTRY_NAME="${REGISTRY_HOST}" +REPOSITORY="${REGISTRY_REPO}" +TAG_OLD="${CIRCLE_SHA1}" +CONTENT_TYPE="application/vnd.docker.distribution.manifest.v2+json" +DOCKER_USERNAME="${DOCKER_USERNAME}" +DOCKER_PASSWORD="${DOCKER_PASSWORD}" +TAG_NEW=${CIRCLE_TAG} + +# find latest image build +i=0 +message=$(git log --pretty=oneline --skip=$i -1 | grep 'skip ci') +while [[ $message ]] +do + ((i++)) + message=$(git log --pretty=oneline --skip=$i -1 | grep 'skip ci') +done +TAG_OLD=$(git log --pretty=oneline --skip=$i -1 --format=%H) + +# get manifest of latest image build +MANIFEST=$(curl -u "$DOCKER_USERNAME:$DOCKER_PASSWORD" --silent --fail -H "Accept: ${CONTENT_TYPE}" "https://${REGISTRY_NAME}/v2/${REPOSITORY}/manifests/${TAG_OLD}") + +if [ "$?" != "0" ]; then + echo "Failed to get manifest for ${REGISTRY_HOST}/${REGISTRY_REPO}:${TAG_OLD}" + exit 1 +fi + +echo "tagging ${REGISTRY_HOST}/${REGISTRY_REPO}:${TAG_OLD} with tag ${TAG_NEW}" + +curl --fail -u "$DOCKER_USERNAME:$DOCKER_PASSWORD" -X PUT -H "Content-Type: ${CONTENT_TYPE}" -d "${MANIFEST}" "https://${REGISTRY_NAME}/v2/${REPOSITORY}/manifests/${TAG_NEW}"