From 4eecb08c64fd06bcf61e426ef62a6297fd15565a Mon Sep 17 00:00:00 2001 From: Guy Sheffer Date: Sun, 17 Nov 2019 12:06:50 +0200 Subject: [PATCH] Handle multi-arch docker image tagging --- src/hooks/post_push | 73 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/src/hooks/post_push b/src/hooks/post_push index 1c4d1f31..d28c434a 100755 --- a/src/hooks/post_push +++ b/src/hooks/post_push @@ -6,4 +6,75 @@ curl -Lo manifest-tool https://github.com/estesp/manifest-tool/releases/download/v0.9.0/manifest-tool-linux-amd64 chmod +x manifest-tool -./manifest-tool push from-spec multi-arch-manifest.yaml +# Generate the manifest file. +# Parameter 1 is the multi-arch image name, e.g. ckulka/baikal:0.5.1 +# Parameter 2 is the base-image for the variants, e.g. ckulka/baikal:0.5.1-apache +function generate_manifest { + NAME=$1 + RELEASE_TAG=$2 + + cat > manifest-generated.yaml << EOF +image: ${NAME}:${RELEASE_TAG} +manifests: + - image: ${NAME}:${RELEASE_TAG}-amd64 + platform: + architecture: amd64 + os: linux + - image: ${NAME}:${RELEASE_TAG}-arm32v7 + platform: + architecture: arm + os: linux + variant: v7 +EOF + + echo "manifest: " + cat manifest-generated.yaml +} + +function docker_tag_exists() { + curl --silent -f -lSL https://index.docker.io/v1/repositories/$1/tags/$2 > /dev/null +} + +# Example: values +# ${DOCKER_REPO} = index.docker.io/guysoft/custompios-test +# ${IMAGE_NAME} = index.docker.io/guysoft/custompios-test:1.2.4-amd64 +# ${DOCKER_TAG} = 1.2.4-amd64 + + +# If we're building the "devel" branch, then also push it as "latest" +if [[ "$DOCKER_TAG" == "amd64" ]] || [[ "$DOCKER_TAG" == "arm32v7" ]] +then + echo "Pushing multi-arch manifest $DOCKER_REPO:latest" + ./manifest-tool push from-spec multi-arch-manifest.yaml + +# Handle release +else + echo "handeling: ${DOCKER_REPO}|${IMAGE_NAME}|${DOCKER_TAG}" + + RELEASE_TAG=${DOCKER_TAG%%-*} + + REPO_NAME=${IMAGE_NAME##*/} ; REPO_NAME=${REPO_NAME%%:*} + REPO_NAME_AND_USER=${IMAGE_NAME%%/${REPO_NAME}*} + USER_NAME=${REPO_NAME_AND_USER##*/} + + NAME=${USER_NAME}/${REPO_NAME} + + # Example: values + # ${NAME} = guysoft/custompios + # ${RELEASE_TAG} = 1.2.3 + + generate_manifest "${NAME}" "${RELEASE_TAG}" + + if docker_tag_exists ${NAME} ${RELEASE_TAG}-amd64 ;then + if docker_tag_exists ${NAME} ${RELEASE_TAG}-arm32v7;then + echo "Pushing multi-arch manifest" + ./manifest-tool push from-spec manifest-generated.yaml + else + echo "arm32v7 image is missing, not pushing manifest" + fi + + else + echo "amd64 image is missing, not pushing manifest" + fi + +fi