From 9312ac9f1cdb833673005c52e40d6affb46b5dea Mon Sep 17 00:00:00 2001
From: Alexander Wellbrock <a.wellbrock@mailbox.org>
Date: Mon, 23 May 2022 12:10:32 +0200
Subject: [PATCH] add cross-build functionality

With this change, if a container build job is tagged with a runner on a
different architecture than requested with CI_REGISTRY_IMAGE_ARCH, the
build will make use of qemu and cross-build the container for the
requested architecture.

A single test of a python container build on my notebook yielded
sobering results: 3min native vs. 22min cross build. That makes a
performance loss of around 700%. Nevertheless it may be preferred to
hosting special runners for extra archs.

One step closer to #4
---
 Dockerfile             | 2 +-
 gitlab-ci-template.yml | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/Dockerfile b/Dockerfile
index fee2ef9..fdf9043 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -6,7 +6,7 @@ COPY resources/shell-tools/ /shell-tools
 
 RUN true\
    && dnf -y upgrade \
-   && dnf -y install podman buildah findutils git \
+   && dnf -y install podman buildah qemu-user-static findutils git \
    && dnf clean all \
    && sed -e 's/.* cgroup_manager =.*/cgroup_manager = "cgroupfs"/' /usr/share/containers/containers.conf > /etc/containers/containers.conf \
    && /shell-tools/install.sh \
diff --git a/gitlab-ci-template.yml b/gitlab-ci-template.yml
index 5d5231f..6dad8c1 100644
--- a/gitlab-ci-template.yml
+++ b/gitlab-ci-template.yml
@@ -26,6 +26,7 @@ variables:
       --label "org.opencontainers.image.revision=$CI_COMMIT_SHA"
       --label "org.opencontainers.image.created=$(date --rfc-3339 ns)"
       --label "org.opencontainers.image.title=$CI_PROJECT_TITLE"
+      --arch $CI_REGISTRY_IMAGE_ARCH
       $CI_REGISTRY_BUILD_ARGS
       -t "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-$CI_COMMIT_SHORT_SHA-$CI_REGISTRY_IMAGE_ARCH"
       --format docker
@@ -54,7 +55,7 @@ container-build-arm64:
 container-build-aarch64:
   extends: .container-build
   variables:
-    CI_REGISTRY_IMAGE_ARCH: aarch64
+    CI_REGISTRY_IMAGE_ARCH: arm64
   tags:
     - aaarch64
   rules:
-- 
GitLab