Add helper script to perform multi-platform images builds

The `build_multiplatform.sh` file was introduced to automate the
multi-platform images build process.
One calls:

  ./build_multiplatform.sh --load

to build both ubuntu and almalinux images for the current system
platform (either `amd64` or `arm64`) and load it to the local
docker registry.

One calls:

  ./build_multiplatform.sh --push

in order to build (for both platforms) and push to the dockerhub's
gerritcodereview account.

Notes:
* script takes care of multi-platform builder creation and clean-up
* almalinux is additionlly tagged as the default release image
* Gerrit installation packages need to be build and published using the
  changes made in Iedcaf6511 as otherwise image build will fail for
  arm64 platform

Change-Id: I5baaed04ec8a82652c46d1f4ef151928690d7cfb
Depends-On: Iedcaf6511fb091775c1f2d0fb1596531efbba7e1
diff --git a/README.md b/README.md
index d648262..0b79174 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 `amd64` images at once and either
+load them to the local docker registry (default) or push them to the `gerritcodereview`
+dockerhub account. In order to do that one simply calls:
+
+```
+./build_multiplatform.sh
+```
+
+And multiplatform images will be created and loaded locally. Calling:
+
+```
+./build_multiplatform.sh --push
+```
+
+pushes images to the dockerhub instead.
+
+Notes:
+* in the default (--load) target only the current system architecture image is pushed
+  to the local registry
+* almalinux images 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..085974d
--- /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 loged 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/8 && 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/20 && 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