From 0787b6d40d5a73145c7dddda3b7bdeac69f47b31 Mon Sep 17 00:00:00 2001 From: Jordan Dubrick Date: Thu, 6 Jun 2024 16:00:18 -0400 Subject: [PATCH] Multi Arch Image Building for Devfile Index (#414) * update dockerfile to dynamically set arch Signed-off-by: Jordan Dubrick * update build.sh to allow for different arch building Signed-off-by: Jordan Dubrick * update documentation for build script Signed-off-by: Jordan Dubrick * update workflows for multi arch building Signed-off-by: Jordan Dubrick --------- Signed-off-by: Jordan Dubrick --- .ci/Dockerfile | 5 ++- .ci/Dockerfile.offline | 5 ++- .ci/build-multi-arch.sh | 52 ++++++++++++++++++++++++++++ .ci/build.sh | 14 ++++++-- .github/workflows/ci.yaml | 4 ++- .github/workflows/pushimge-next.yaml | 8 ++--- README.md | 11 +++++- 7 files changed, 89 insertions(+), 10 deletions(-) create mode 100644 .ci/build-multi-arch.sh diff --git a/.ci/Dockerfile b/.ci/Dockerfile index 6c6c5505..efe69dcf 100644 --- a/.ci/Dockerfile +++ b/.ci/Dockerfile @@ -17,9 +17,12 @@ FROM registry.access.redhat.com/ubi8/go-toolset:1.19 AS builder # Set user as root USER root +# Automatically set when --platform flag is set, will default to amd64 if no platform is given +ARG TARGETARCH=amd64 + # Install yq ENV YQ_VERSION=v4.44.1 -RUN curl -sL -O https://github.com/mikefarah/yq/releases/download/${YQ_VERSION}/yq_linux_amd64 -o /usr/local/bin/yq && mv ./yq_linux_amd64 /usr/local/bin/yq && chmod +x /usr/local/bin/yq +RUN curl -sL -O https://github.com/mikefarah/yq/releases/download/${YQ_VERSION}/yq_linux_${TARGETARCH} -o /usr/local/bin/yq && mv ./yq_linux_${TARGETARCH} /usr/local/bin/yq && chmod +x /usr/local/bin/yq COPY . /registry diff --git a/.ci/Dockerfile.offline b/.ci/Dockerfile.offline index 777026e5..7115df72 100644 --- a/.ci/Dockerfile.offline +++ b/.ci/Dockerfile.offline @@ -17,9 +17,12 @@ FROM registry.access.redhat.com/ubi8/go-toolset:1.19 AS builder # Set user as root USER root +# Automatically set when --platform flag is set, will default to amd64 if no platform is given +ARG TARGETARCH=amd64 + # Install yq ENV YQ_VERSION=v4.44.1 -RUN curl -sL -O https://github.com/mikefarah/yq/releases/download/${YQ_VERSION}/yq_linux_amd64 -o /usr/local/bin/yq && mv ./yq_linux_amd64 /usr/local/bin/yq && chmod +x /usr/local/bin/yq +RUN curl -sL -O https://github.com/mikefarah/yq/releases/download/${YQ_VERSION}/yq_linux_${TARGETARCH} -o /usr/local/bin/yq && mv ./yq_linux_${TARGETARCH} /usr/local/bin/yq && chmod +x /usr/local/bin/yq COPY . /registry diff --git a/.ci/build-multi-arch.sh b/.ci/build-multi-arch.sh new file mode 100644 index 00000000..d646aad6 --- /dev/null +++ b/.ci/build-multi-arch.sh @@ -0,0 +1,52 @@ +#!/bin/sh + +# +# Copyright Red Hat +# +# 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. + +# Build the index container for the registry +ABSOLUTE_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +# Due to command differences between podman and docker we need to separate the process +# for creating and adding images to a multi-arch manifest +podman=${USE_PODMAN:-false} +# Base Repository +BASE_REPO="quay.io/devfile/devfile-index" +BASE_TAG="next" +DEFAULT_IMG="$BASE_REPO:$BASE_TAG" +# Platforms to build for +PLATFORMS="linux/amd64,linux/arm64" + +if [ ${podman} == true ]; then + echo "Executing with podman" + + podman manifest create "$DEFAULT_IMG" + + podman build --platform="$PLATFORMS" --manifest "$DEFAULT_IMG" --no-cache -f $ABSOLUTE_PATH/Dockerfile $ABSOLUTE_PATH/.. + + podman manifest push "$DEFAULT_IMG" + + podman manifest rm "$DEFAULT_IMG" + +else + echo "Executing with docker" + + docker buildx create --name index-builder + + docker buildx use index-builder + + docker buildx build --push --platform="$PLATFORMS" --tag "$DEFAULT_IMG" --provenance=false --no-cache -f $ABSOLUTE_PATH/Dockerfile $ABSOLUTE_PATH/.. + + docker buildx rm index-builder + +fi \ No newline at end of file diff --git a/.ci/build.sh b/.ci/build.sh index 1043e3c5..83fb7104 100755 --- a/.ci/build.sh +++ b/.ci/build.sh @@ -16,6 +16,16 @@ shopt -s expand_aliases ABSOLUTE_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" USE_PODMAN=${USE_PODMAN:-false} +DEFAULT_ARCH="linux/amd64" + +# Check if different architecture was passed for image build +# Will default to $DEFAULT_ARCH if unset +if [ ! -z "$1" ] + then + arch="$1" +else + arch="$DEFAULT_ARCH" +fi if [[ ${USE_PODMAN} == true ]]; then alias docker=podman @@ -24,7 +34,7 @@ fi if [ $# -eq 1 ] && [ $1 == "offline" ] then - docker build --no-cache -t devfile-index -f $ABSOLUTE_PATH/Dockerfile.offline $ABSOLUTE_PATH/.. + docker build --no-cache --platform "${arch}" -t devfile-index -f $ABSOLUTE_PATH/Dockerfile.offline $ABSOLUTE_PATH/.. else - docker build --no-cache -t devfile-index -f $ABSOLUTE_PATH/Dockerfile $ABSOLUTE_PATH/.. + docker build --no-cache --platform "${arch}" -t devfile-index -f $ABSOLUTE_PATH/Dockerfile $ABSOLUTE_PATH/.. fi diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 42f0f58b..e5e59341 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -37,6 +37,8 @@ jobs: uses: actions/setup-go@cdcb36043654635271a94b9a6d1392de5bb323a7 # v5.0.1 with: go-version: 1.19 + - name: Set up QEMU # Enables arm64 image building + uses: docker/setup-qemu-action@68827325e0b33c7199eb31dd4e31fbe9023e06e3 - name: Check if devfile registry build is working - run: bash registry-repo/.ci/build.sh + run: bash registry-repo/.ci/build.sh && bash registry-repo/.ci/build.sh linux/arm64 diff --git a/.github/workflows/pushimge-next.yaml b/.github/workflows/pushimge-next.yaml index 5d1bd8ce..c0cad52e 100644 --- a/.github/workflows/pushimge-next.yaml +++ b/.github/workflows/pushimge-next.yaml @@ -42,13 +42,13 @@ jobs: uses: actions/setup-go@cdcb36043654635271a94b9a6d1392de5bb323a7 # v5.0.1 with: go-version: 1.13 + - name: Set up QEMU # Enables arm64 image building + uses: docker/setup-qemu-action@68827325e0b33c7199eb31dd4e31fbe9023e06e3 - name: Login to Quay uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0 with: registry: quay.io username: ${{ secrets.QUAY_USERNAME }} password: ${{ secrets.QUAY_PASSWORD }} - - name: Build the devfile-index docker image - run: bash registry-repo/.ci/build.sh - - name: Push the devfile-index docker image - run: bash registry-support/build-tools/push.sh quay.io/devfile/devfile-index:next + - name: Build and push the devfile-index docker image + run: bash registry-repo/.ci/build-multi-arch.sh diff --git a/README.md b/README.md index 60375169..359e0177 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,16 @@ If you are a stack owner and need to request an urgent refresh of