Add support for specifying the target platform in the Docker build script

This change provides a command line option, --platform, in the build
script to define the target platform for the docker image.

Change-Id: Iac8c1a6a1e360f13a5d4b8b8d813633f68069b73
diff --git a/README.md b/README.md
index 5c16b2e..b573b52 100644
--- a/README.md
+++ b/README.md
@@ -80,6 +80,14 @@
 The build script will in addition tag the image with the output of
 `git describe --dirty`.
 
+The target platform for the image build can be specified by using the `--platform PLATFORM` option.
+This allows you to build images for different architectures such as ARM or x86. By default, the
+platform is set to linux/amd64.
+
+```
+./build --platform linux/arm64
+```
+
 The single component images inherit a base image. The `Dockerfile` for the base
 image can be found in the `./base`-directory. It will be
 automatically built by the `./build`-script. If the component images are built
diff --git a/build b/build
index e4809c7..0913c23 100755
--- a/build
+++ b/build
@@ -16,7 +16,7 @@
 
 usage() {
     me=`basename "$0"`
-    echo >&2 "Usage: $me [--help] [--tag TAG] [--no-cache] [--gerrit-url URL] [--base-image IMAGE] [IMAGE]"
+    echo >&2 "Usage: $me [--help] [--tag TAG] [--no-cache] [--gerrit-url URL] [--base-image IMAGE] [--platform PLATFORM] [IMAGE]"
     exit 1
 }
 
@@ -55,6 +55,12 @@
     shift
     ;;
 
+  --platform)
+    shift
+    PLATFORM=$1
+    shift
+    ;;
+
   *)
     break
   esac
@@ -64,7 +70,8 @@
 source container-images/publish_list
 IMAGES=$(get_image_list)
 
-DOCKER_BUILD_OPTS="--platform=linux/amd64"
+PLATFORM=${PLATFORM:-linux/amd64}  # Default value if PLATFORM is not set
+DOCKER_BUILD_OPTS="--platform=$PLATFORM"
 test -n "$NO_CACHE" && DOCKER_BUILD_OPTS="$DOCKER_BUILD_OPTS --no-cache"
 
 if test -n "$GERRIT_WAR_URL"; then
@@ -75,7 +82,7 @@
     BUILD_ARGS="$BUILD_ARGS --build-arg HEALTHCHECK_JAR_URL=$HEALTHCHECK_JAR_URL"
 fi
 
-export REV="$(./get_version.sh --output K8SGERRIT)"
+export REV="$(./get_version.sh --output K8SGERRIT --platform $PLATFORM)"
 
 docker_build(){
     IMAGE=$1
@@ -107,7 +114,7 @@
     fi
 
     if test -z "$TAG"; then
-        export TAG="$(./get_version.sh)"
+        export TAG="$(./get_version.sh --platform $PLATFORM)"
     fi
 }
 
diff --git a/get_version.sh b/get_version.sh
index ffeecc9..8647ba8 100755
--- a/get_version.sh
+++ b/get_version.sh
@@ -32,6 +32,12 @@
     shift
     ;;
 
+  --platform)
+    shift
+    PLATFORM=$1
+    shift
+    ;;
+
   *)
     break
   esac
@@ -42,9 +48,10 @@
 }
 
 getGerritVersion() {
+    PLATFORM=${PLATFORM:-linux/amd64}  # Default value if PLATFORM is not set
     GERRIT_VERSION="$(
         docker run \
-            --platform=linux/amd64 \
+            --platform=$PLATFORM \
             --entrypoint '/bin/sh' \
             gerrit-base:$(getK8sVersion) \
             -c 'java -jar /var/gerrit/bin/gerrit.war version'