Merge branch 'stable-3.7'

* stable-3.7:
  follow-up to "Add helper script to perform multi-platform..."
  Add helper script to perform multi-platform images builds

Change-Id: I45b0af23478d7b737b0653d08f4c1be43ede5fb2
diff --git a/README.md b/README.md
index 9181cea..b1ef8c6 100644
--- a/README.md
+++ b/README.md
@@ -60,6 +60,29 @@
 The build argument defaults to the URL pointing to the last successful build of the Gerrit master
 branch on the [Gerrit CI](https://gerrit-ci.gerritforge.com).
 
+## Build multi-platform images
+
+For the official releases one can build both `amd64` and `arm64` images at once and either
+load them to the local docker registry or push them to the `gerritcodereview` dockerhub account.
+In order to do that one simply calls:
+
+```
+./build_multiplatform.sh --load
+```
+
+And multiplatform images will be created and loaded locally. Calling:
+
+```
+./build_multiplatform.sh --push
+```
+
+pushes images to the dockerhub instead.
+
+Notes:
+* in the `--load` target only the current system architecture image is pushed to the local
+  registry
+* the almalinux image is additionally tagged as the default release image.
+
 ## Using persistent volumes
 
 Use docker persistent volumes to keep Gerrit data across restarts.
diff --git a/build_multiplatform.sh b/build_multiplatform.sh
new file mode 100755
index 0000000..ad784f0
--- /dev/null
+++ b/build_multiplatform.sh
@@ -0,0 +1,80 @@
+#!/bin/bash
+
+help() {
+  echo "Helper script to build single or multiplatform (depending on the input parameter)"
+  echo "images for both almalinux and ubuntu distributions."
+  echo
+  echo "Syntax: $0 [--load|--push]"
+  echo "options:"
+  echo "load     Builds single platform (of runner's system type) images and loads them into"
+  echo "         into local docker registry (they are visible through the 'docker images'"
+  echo "         command."
+  echo "push     Builds both 'amd64' and 'arm64' architectures images and pushes them to the."
+  echo "         dockerhub. Note that in this case one needs to be logged in to"
+  echo "         'gerritcodereview' account and images are not visible in the local registry."
+  echo
+}
+
+DESTINATION=$1
+
+if ! [[ "$DESTINATION" =~ ^(--load|--push)$ ]]; then
+  help
+  exit
+fi
+
+if [[ $DESTINATION == *load ]]; then
+  if [[ $(uname -m) == *arm64* ]]; then
+    PLATFORMS="linux/arm64"
+  else
+    PLATFORMS="linux/amd64"
+  fi
+else
+  PLATFORMS="linux/amd64,linux/arm64"
+fi
+
+echo "### Building images for $DESTINATION destination and $PLATFORMS platforms"
+
+BUILDER=gerrit-multiplatform-image-builder
+DOCKER_USER=gerritcodereview/gerrit
+
+function create_builder() {
+  if [[ "$OSTYPE" == "linux"* ]]; then
+    docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
+  fi
+  docker buildx create --name $BUILDER --platform "$PLATFORMS" --driver docker-container --use
+  docker buildx inspect --bootstrap
+}
+
+STATUS="$(docker buildx inspect $BUILDER 2>&1 | grep Status:)"
+if [ "$?" -ne "0" ]; then
+  set -e
+  echo "### Multi-platform builder $BUILDER doesn't exist and will be created."
+  create_builder
+else
+  set -e
+  STATUS="${STATUS##* }"
+  if [[ $STATUS == *running* ]]; then
+    echo "### Multi-platform builder $BUILDER is up and running."
+  else
+    echo "### Multi-platform builder $BUILDER exists but it doesn't run. It will be re-created."
+    docker buildx rm $BUILDER
+    create_builder
+  fi
+fi
+
+VERSION=$(git describe)
+VERSION=${VERSION:1}
+
+echo
+echo "### Building almalinux multi-platform: [$PLATFORMS] iamges"
+(cd almalinux/9 && docker buildx build --platform "$PLATFORMS" --no-cache -t "$DOCKER_USER:${VERSION}-almalinux8" -t "$DOCKER_USER:$VERSION" "$DESTINATION" .)
+
+echo
+echo "### Building ubuntu multi-platform: [$PLATFORMS] iamges"
+(cd ubuntu/22 && docker buildx build --platform "$PLATFORMS" --no-cache -t "$DOCKER_USER:${VERSION}-ubuntu20" "$DESTINATION" .)
+
+echo
+echo "### Removing multi-platform builder"
+echo y | docker buildx prune
+docker buildx stop $BUILDER
+docker buildx rm $BUILDER