From 68e7586753387d785d5fecefc866c87518324ba1 Mon Sep 17 00:00:00 2001 From: ViViDboarder Date: Tue, 11 Feb 2020 13:30:39 -0800 Subject: [PATCH] Add hooks to push manifest --- hooks/build | 38 +++++++++++++++++++++++++ hooks/post_push | 55 ++++++++++++++++++++++++++++++++++++ hooks/pre_build | 6 +--- multi-arch-manifest-tmpl.yml | 38 +++++++++++++++++++++++++ 4 files changed, 132 insertions(+), 5 deletions(-) create mode 100755 hooks/build create mode 100755 hooks/post_push create mode 100644 multi-arch-manifest-tmpl.yml diff --git a/hooks/build b/hooks/build new file mode 100755 index 0000000..5fe53b1 --- /dev/null +++ b/hooks/build @@ -0,0 +1,38 @@ +#!/bin/bash +set -ex + +# Extract arch repo from a docker tag suffix +REPO="" +case "$DOCKER_TAG" in + *amd64) + REPO="library" + ;; + *arm32v5) + REPO="arm32v5" + ;; + *arm32v6) + REPO="arm32v6" + ;; + *arm32v7) + REPO="arm32v7" + ;; + *arm64v8) + REPO="arm64v8" + ;; + *i386) + REPO="i386" + ;; + *ppc64le) + REPO="ppc64le" + ;; + *s390x) + REPO="s390x" + ;; + *) + REPO="library" + ;; +esac + +docker build \ + --build-arg REPO="$REPO" \ + --tag "$IMAGE_NAME" . diff --git a/hooks/post_push b/hooks/post_push new file mode 100755 index 0000000..7168ff6 --- /dev/null +++ b/hooks/post_push @@ -0,0 +1,55 @@ +#!/bin/bash + +# Use manifest-tool to create the manifest, given the experimental +# "docker manifest" command isn't available yet on Docker Hub. + +TAG_ROOT=${DOCKER_TAG%-*} +TARGET_ARCH="" +case "$DOCKER_TAG" in + *amd64) + TARGET_ARCH="amd64" + ;; + *arm32v5) + TARGET_ARCH="arm/v5" + ;; + *arm32v6) + TARGET_ARCH="arm/v6" + ;; + *arm32v7) + TARGET_ARCH="arm/v7" + ;; + *arm64v8) + TARGET_ARCH="arm64/v8" + ;; + *i386) + TARGET_ARCH="i386" + ;; + *ppc64le) + TARGET_ARCH="ppc64le" + ;; + *s390x) + TARGET_ARCH="s390x" + ;; + *) + echo "Unknown target arch, skip manifest push" + exit 0 + ;; +esac + +# If the tag is essentially only the arch, we use latest +if [[ "$TAG_ROOT" == "$TARGET_ARCH" ]]; then + TAG_ROOT="latest" +fi + +curl -Lo manifest-tool https://github.com/estesp/manifest-tool/releases/download/v1.0.0/manifest-tool-linux-amd64 +chmod +x manifest-tool + +# Generate manifest file for this repo and tag root +sed "s#{DOCKER_REPO}#${DOCKER_REPO%:*}#;s#{TAG_ROOT}#${TAG_ROOT}#" multi-arch-manifest-tmpl.yml > multi-arch-manifest.yml +./manifest-tool push from-spec --ignore-missing multi-arch-manifest.yml + +# Fix arch for image tags +./manifest-tool push from-args \ + --platforms "linux/$TARGET_ARCH" \ + --template "$DOCKER_REPO:$DOCKER_TAG" \ + --target "$DOCKER_REPO:$DOCKER_TAG" diff --git a/hooks/pre_build b/hooks/pre_build index 5444851..e76a49a 100755 --- a/hooks/pre_build +++ b/hooks/pre_build @@ -1,8 +1,4 @@ #!/bin/bash - -BUILD_ARCH=$(echo "${DOCKERFILE_PATH}" | cut -d '.' -f 2) - -[ "${BUILD_ARCH}" == "Dockerfile" ] && \ - { echo 'qemu-user-static: Registration not required for current arch'; exit 0; } +set -ex docker run --rm --privileged multiarch/qemu-user-static:register --reset diff --git a/multi-arch-manifest-tmpl.yml b/multi-arch-manifest-tmpl.yml new file mode 100644 index 0000000..c2fb853 --- /dev/null +++ b/multi-arch-manifest-tmpl.yml @@ -0,0 +1,38 @@ +--- +image: "{DOCKER_REPO}:{TAG_ROOT}" +manifests: + - image: "{DOCKER_REPO}:{TAG_ROOT}" + platform: + architecture: amd64 + os: linux + - image: "{DOCKER_REPO}:{TAG_ROOT}-amd64" + platform: + architecture: amd64 + os: linux + - image: "{DOCKER_REPO}:{TAG_ROOT}-arm32v6" + platform: + architecture: arm + os: linux + variant: v6 + - image: "{DOCKER_REPO}:{TAG_ROOT}-arm32v7" + platform: + architecture: arm + os: linux + variant: v7 + - image: "{DOCKER_REPO}:{TAG_ROOT}-arm64v8" + platform: + architecture: arm64 + os: linux + variant: v8 + - image: "{DOCKER_REPO}:{TAG_ROOT}-i386" + platform: + architecture: i386 + os: linux + - image: "{DOCKER_REPO}:{TAG_ROOT}-s390x" + platform: + architecture: s390x + os: linux + - image: "{DOCKER_REPO}:{TAG_ROOT}-ppc64le" + platform: + architecture: ppc64le + os: linux \ No newline at end of file