From 7a1f23e2e4813b61bbe4ed0f75c84974c8db456f Mon Sep 17 00:00:00 2001 From: Evan Lezar Date: Fri, 5 May 2023 14:30:12 +0200 Subject: [PATCH 1/2] Skip publishing of images if these already exist Signed-off-by: Evan Lezar --- build/container/Makefile | 9 ++++++ build/container/multi-arch.mk | 11 ------- build/container/native-only.mk | 8 ----- scripts/publish-image.sh | 59 ++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 19 deletions(-) create mode 100755 scripts/publish-image.sh 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} From 07c89fa9759ecde7fa8c89826b80fd082c243611 Mon Sep 17 00:00:00 2001 From: Evan Lezar Date: Fri, 5 May 2023 14:31:03 +0200 Subject: [PATCH 2/2] Always publish external images Signed-off-by: Evan Lezar --- .common-ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.common-ci.yml b/.common-ci.yml index 926854c3..2b754154 100644 --- a/.common-ci.yml +++ b/.common-ci.yml @@ -245,6 +245,8 @@ test-packaging: .release:external: extends: - .release + variables: + FORCE_PUBLISH_IMAGES: "yes" rules: - if: $CI_COMMIT_TAG variables: