diff --git a/build/container/Makefile b/build/container/Makefile index 3f5ebd3c..64d99806 100644 --- a/build/container/Makefile +++ b/build/container/Makefile @@ -14,6 +14,7 @@ BUILD_MULTI_ARCH_IMAGES ?= false DOCKER ?= docker +REGCTL ?= regctl BUILDX = ifeq ($(BUILD_MULTI_ARCH_IMAGES),true) @@ -74,6 +75,14 @@ endif push-%: DIST = $(*) push-short: DIST = $(DEFAULT_PUSH_TARGET) +# Define the push targets +$(PUSH_TARGETS): push-%: + $(CURDIR)/scripts/publish-image.sh $(IMAGE) $(OUT_IMAGE) + +push-short: + $(CURDIR)/scripts/publish-image.sh $(IMAGE) $(OUT_IMAGE) + + build-%: DIST = $(*) build-%: DOCKERFILE = $(CURDIR)/build/container/Dockerfile.$(DOCKERFILE_SUFFIX) diff --git a/build/container/multi-arch.mk b/build/container/multi-arch.mk index 445ea0d0..8f6168e5 100644 --- a/build/container/multi-arch.mk +++ b/build/container/multi-arch.mk @@ -16,17 +16,6 @@ PUSH_ON_BUILD ?= false DOCKER_BUILD_OPTIONS = --output=type=image,push=$(PUSH_ON_BUILD) DOCKER_BUILD_PLATFORM_OPTIONS = --platform=linux/amd64,linux/arm64 -REGCTL ?= regctl -$(PUSH_TARGETS): push-%: - $(REGCTL) \ - image copy \ - $(IMAGE) $(OUT_IMAGE) - -push-short: - $(REGCTL) \ - image copy \ - $(IMAGE) $(OUT_IMAGE_NAME):$(OUT_IMAGE_VERSION) - # We only have x86_64 packages for centos7 build-centos7: DOCKER_BUILD_PLATFORM_OPTIONS = --platform=linux/amd64 diff --git a/build/container/native-only.mk b/build/container/native-only.mk index aacde563..d15b78a4 100644 --- a/build/container/native-only.mk +++ b/build/container/native-only.mk @@ -13,11 +13,3 @@ # limitations under the License. DOCKER_BUILD_PLATFORM_OPTIONS = --platform=linux/amd64 - -$(PUSH_TARGETS): push-%: - $(DOCKER) tag "$(IMAGE)" "$(OUT_IMAGE)" - $(DOCKER) push "$(OUT_IMAGE)" - -push-short: - $(DOCKER) tag "$(IMAGE_NAME):$(VERSION)-$(DEFAULT_PUSH_TARGET)" "$(OUT_IMAGE_NAME):$(OUT_IMAGE_VERSION)" - $(DOCKER) push "$(OUT_IMAGE_NAME):$(OUT_IMAGE_VERSION)" diff --git a/scripts/publish-image.sh b/scripts/publish-image.sh new file mode 100755 index 00000000..9387ce76 --- /dev/null +++ b/scripts/publish-image.sh @@ -0,0 +1,59 @@ +#!/usr/bin/env bash + +# Copyright (c) NVIDIA CORPORATION. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This script is used to publish images to a registry. It checks whether the image +# already exists in the registry and skips publishing if it does. This can be overridden +# using the FORCE_PUBLISH_IMAGES environment variable. + +set -ex + +: ${DOCKER=docker} +: ${REGCTL=regctl} + +INPUT_IMAGE=$1 +OUTPUT_IMAGE=$2 + +function publish_docker() { + ${DOCKER} tag ${1} ${2} + ${DOCKER} push ${2} +} + +function publish_regctl() { + ${REGCTL} image copy ${1} ${2} +} + +function publish() { + if [[ x"${BUILD_MULTI_ARCH_IMAGES}" == x"true" || $(command -v ${REGCTL}) ]]; then + publish_regctl $@ + else + publish_docker $@ + fi +} + +# image_exists returns 0 if the image exists in a registry and a non-zero return +# code if this is not the case. +function image_exists() { + local image=$1 + ${DOCKER} manifest inspect ${image} +} + +if [[ -z ${FORCE_PUBLISH_IMAGES} && $(image_exists ${OUTPUT_IMAGE}) ]]; then + echo "Skipping publishing of ${INPUT_IMAGE} as ${OUTPUT_IMAGE} already exists" + exit 0 +fi + +echo "Publishing ${INPUT_IMAGE} as ${OUTPUT_IMAGE}" +publish ${INPUT_IMAGE} ${OUTPUT_IMAGE}