Allow overriding gerrit.war when build the image

Introduce a GERRIT_WAR_URL variable for overriding
the gerrit.war inside the Docker container.

This is particularly useful for nightly builds on
for testing the latest artifact built on the Gerrit-CI.

Also checks the compatibility of GERRIT_VERSION with
the gerrit.war pointed by GERRIT_WAR_URL.

Change-Id: Ibe5e780a98a7747a3201d7d2809ecb66f87e08b8
diff --git a/gerrit/Dockerfile b/gerrit/Dockerfile
index 5dae609..dd3ce2a 100644
--- a/gerrit/Dockerfile
+++ b/gerrit/Dockerfile
@@ -28,6 +28,8 @@
 
 USER gerrit
 
+RUN bash -c '[ "$GERRIT_WAR_URL" == "" ] || curl $GERRIT_WAR_URL > /var/gerrit/bin/gerrit.war'
+
 # Install AWS cli
 RUN pip3 install awscli --upgrade --user
 ENV PATH ${PATH}:/var/gerrit/.local/bin
diff --git a/gerrit/Makefile b/gerrit/Makefile
index ef80087..53d9b80 100644
--- a/gerrit/Makefile
+++ b/gerrit/Makefile
@@ -43,11 +43,24 @@
 		|| { echo >&2 "Cannot download $$mavenlib: Check internet connection. Aborting"; exit 1; } \
 	done
 
-gerrit-build:
+gerrit-build: check-gerrit-version-vs-war-url
 	cat Dockerfile | \
-		GERRIT_VERSION=$(GERRIT_VERSION) GERRIT_PATCH=$(GERRIT_PATCH) envsubst | \
+		GERRIT_VERSION=$(GERRIT_VERSION) GERRIT_PATCH=$(GERRIT_PATCH) GERRIT_WAR_URL=$(GERRIT_WAR_URL) envsubst | \
 		docker build -f - -t aws-gerrit/gerrit:$(IMAGE_TAG) .
 	docker tag aws-gerrit/gerrit:$(IMAGE_TAG) $(DOCKER_REGISTRY_URI)/aws-gerrit/gerrit:$(IMAGE_TAG)
 
+check-gerrit-version-vs-war-url:
+	. set-java.sh 11; \
+	if [[ "$(GERRIT_WAR_URL)" != "" ]]; \
+	then \
+		curl $(GERRIT_WAR_URL) > gerrit.war; \
+		GERRIT_VERSION_FROM_WAR=$$(java -jar gerrit.war version | awk '{print $$3}' | cut -d '.' -f 1,2); \
+		if [[ "$$GERRIT_VERSION_FROM_WAR" != "$(GERRIT_VERSION)" ]]; \
+		then \
+			echo "GERRIT_VERSION=$(GERRIT_VERSION) is not compatible with $$(java -jar gerrit.war version) from $(GERRIT_WAR_URL)"; \
+			exit -1; \
+		fi; \
+	fi
+
 gerrit-publish: gerrit-get-plugins docker-registry-login gerrit-build
 	docker push $(DOCKER_REGISTRY_URI)/aws-gerrit/gerrit:$(IMAGE_TAG)