Add Gerrit-version to image tags

The images were automatically tagged with the output of 'git describe
--dirty'. The version of Gerrit built into the image, which could be
configured by setting the download URL of the war-file, was not visible
without accessing the image.

With this change the build script gets the Gerrit version from the
war-file in the gerrit-base image and appends it to the version tag.

Change-Id: I4bf6fadd383fa05a21311ea1c1d74bc00ac13145
diff --git a/build b/build
index 8dca157..f56172c 100755
--- a/build
+++ b/build
@@ -33,26 +33,30 @@
 source container-images/publish_list
 IMAGES=$(get_image_list)
 
-GIT_REV=$(git describe --dirty)
+if test -n "$GERRIT_WAR_URL"; then
+    BUILD_ARGS="--build-arg GERRIT_WAR_URL=$GERRIT_WAR_URL"
+fi
 
 test -z "$TAG" && TAG=latest
 
+export GIT_REV_TAG="$(git describe --always --dirty)-unknown"
+
 docker_build(){
     IMAGE=$1
 
-    docker build -t k8sgerrit/$IMAGE:$TAG -t k8sgerrit/$IMAGE:$GIT_REV ./container-images/$IMAGE
+    docker build -t k8sgerrit/$IMAGE:$TAG \
+        -t k8sgerrit/$IMAGE:$GIT_REV_TAG \
+        ./container-images/$IMAGE
+
     if test $? -ne 0; then
         REPORT="$REPORT Failed: k8sgerrit/$IMAGE.\n"
         RETURN_CODE=1
     else
-        REPORT="$REPORT Success: k8sgerrit/$IMAGE:$TAG\n Success: k8sgerrit/$IMAGE:$GIT_REV\n"
+        REPORT="$REPORT Success: k8sgerrit/$IMAGE:$TAG\n Success: k8sgerrit/$IMAGE:$GIT_REV_TAG\n"
     fi
 }
 
 docker_build_gerrit_base(){
-    if test -n "$GERRIT_WAR_URL"; then
-        BUILD_ARGS="--build-arg GERRIT_WAR_URL=$GERRIT_WAR_URL"
-    fi
     docker build $BUILD_ARGS -t gerrit-base ./container-images/gerrit-base
     if test $? -ne 0; then
         echo -e "\n\nFailed to build gerrit-base image."
@@ -60,6 +64,13 @@
     fi
 }
 
+create_image_tag(){
+    GERRIT_VERSION=$(docker run --entrypoint "/bin/sh" gerrit-base \
+        -c "java -jar /var/gerrit/bin/gerrit.war version")
+    GERRIT_VERSION=$(echo "${GERRIT_VERSION##*$'\n'}" | cut -d' ' -f3 | tr -d '[:space:]')
+    GIT_REV_TAG="$(git describe --dirty)-$GERRIT_VERSION"
+}
+
 REPORT="Build results: \n"
 RETURN_CODE=0
 
@@ -71,6 +82,7 @@
 
 if test $# -eq 0 ; then
     docker_build_gerrit_base
+    create_image_tag
     for IMAGE in $IMAGES; do
         docker_build $IMAGE
     done
@@ -78,6 +90,11 @@
     while test $# -gt 0 ; do
         if [[ $1 = gerrit-* ]]; then
             docker_build_gerrit_base
+            create_image_tag
+        else
+            echo -e "\nNo Image containing Gerrit will be built." \
+                    "The Gerrit-version can thus not be determinded." \
+                    "Using tag $GIT_REV_TAG\n"
         fi
         docker_build $1
         shift