Use bazelisk for gerrit and plugins builds

Bazelisk allows to automatically switch between bazel versions
using the .bazelversion file on the Gerrit home folder.

Change-Id: Ie1bdae2a2f350e900cb666b8e5b8d0ee42c15a4a
diff --git a/jenkins/gerrit-bazel-build-account-plugin.sh b/jenkins/gerrit-bazel-build-account-plugin.sh
index b532a22..e5c4603 100644
--- a/jenkins/gerrit-bazel-build-account-plugin.sh
+++ b/jenkins/gerrit-bazel-build-account-plugin.sh
@@ -23,11 +23,12 @@
 cp -Rf bower_components/angular/*js src/main/resources/static/js/.
 popd
 
-bazel build --spawn_strategy=standalone --genrule_strategy=standalone $TARGETS
+bazelisk version
+bazelisk build --spawn_strategy=standalone --genrule_strategy=standalone $TARGETS
 
 if [ "$TEST_TARGET" != "" ]
 then
-    bazel test --test_env DOCKER_HOST=$DOCKER_HOST plugins/account:$TEST_TARGET
+    bazelisk test --test_env DOCKER_HOST=$DOCKER_HOST plugins/account:$TEST_TARGET
 fi
 
 for JAR in $(find bazel-bin/plugins/account -name account*.jar)
diff --git a/jenkins/gerrit-bazel-build-its-plugin-branch.sh b/jenkins/gerrit-bazel-build-its-plugin-branch.sh
index 40d7031..7d6a70d 100644
--- a/jenkins/gerrit-bazel-build-its-plugin-branch.sh
+++ b/jenkins/gerrit-bazel-build-its-plugin-branch.sh
@@ -22,11 +22,12 @@
 
 . set-java.sh 8
 
-bazel build --spawn_strategy=standalone --genrule_strategy=standalone $TARGETS
+bazelisk version
+bazelisk build --spawn_strategy=standalone --genrule_strategy=standalone $TARGETS
 
 if [ "$TEST_TARGET" != "" ]
 then
-    bazel test --test_env DOCKER_HOST=$DOCKER_HOST plugins/its-{name}:$TEST_TARGET
+    bazelisk test --test_env DOCKER_HOST=$DOCKER_HOST plugins/its-{name}:$TEST_TARGET
 fi
 
 for JAR in $(find bazel-bin/plugins/its-{name} -name its-{name}*.jar)
diff --git a/jenkins/gerrit-bazel-build-its-plugin.sh b/jenkins/gerrit-bazel-build-its-plugin.sh
index fb0744b..ae925cd 100644
--- a/jenkins/gerrit-bazel-build-its-plugin.sh
+++ b/jenkins/gerrit-bazel-build-its-plugin.sh
@@ -18,11 +18,12 @@
 
 . set-java.sh 8
 
-bazel build --spawn_strategy=standalone --genrule_strategy=standalone $TARGETS
+bazelisk version
+bazelisk build --spawn_strategy=standalone --genrule_strategy=standalone $TARGETS
 
 if [ "$TEST_TARGET" != "" ]
 then
-    bazel test --test_env DOCKER_HOST=$DOCKER_HOST plugins/its-{name}:$TEST_TARGET
+    bazelisk test --test_env DOCKER_HOST=$DOCKER_HOST plugins/its-{name}:$TEST_TARGET
 fi
 
 for JAR in $(find bazel-bin/plugins/its-{name} -name its-{name}*.jar)
diff --git a/jenkins/gerrit-bazel-build-owners-plugin-branch.sh b/jenkins/gerrit-bazel-build-owners-plugin-branch.sh
index c9e0c43..582092d 100644
--- a/jenkins/gerrit-bazel-build-owners-plugin-branch.sh
+++ b/jenkins/gerrit-bazel-build-owners-plugin-branch.sh
@@ -14,7 +14,8 @@
 
 . set-java.sh 8
 
-bazel build --spawn_strategy=standalone --genrule_strategy=standalone $TARGETS
+bazelisk version
+bazelisk build --spawn_strategy=standalone --genrule_strategy=standalone $TARGETS
 
 for JAR in $(find bazel-bin/plugins/ -name {name}*.jar)
 do
diff --git a/jenkins/gerrit-bazel-build-owners-plugin.sh b/jenkins/gerrit-bazel-build-owners-plugin.sh
index 02f2c89..dcbac1d 100644
--- a/jenkins/gerrit-bazel-build-owners-plugin.sh
+++ b/jenkins/gerrit-bazel-build-owners-plugin.sh
@@ -14,7 +14,8 @@
 
 . set-java.sh 8
 
-bazel build --spawn_strategy=standalone --genrule_strategy=standalone $TARGETS
+bazelisk version
+bazelisk build --spawn_strategy=standalone --genrule_strategy=standalone $TARGETS
 
 for JAR in $(find bazel-bin/plugins/ -name {name}*.jar)
 do
diff --git a/jenkins/gerrit-bazel-build-plugin-branch.sh b/jenkins/gerrit-bazel-build-plugin-branch.sh
index 3ed7622..f8d2e06 100644
--- a/jenkins/gerrit-bazel-build-plugin-branch.sh
+++ b/jenkins/gerrit-bazel-build-plugin-branch.sh
@@ -16,7 +16,8 @@
 TARGETS=$(echo "{targets}" | sed -e 's/{{name}}/{name}/g')
 BUILD_TARGETS=$(echo "$TARGETS" | tr ' ' '\n' | grep -v test)
 
-bazel build --spawn_strategy=standalone --genrule_strategy=standalone $BUILD_TARGETS
+bazelisk version
+bazelisk build --spawn_strategy=standalone --genrule_strategy=standalone $BUILD_TARGETS
 
 if TEST_TARGETS=$(echo "$TARGETS" | tr ' ' '\n' | grep test)
 then
@@ -27,7 +28,7 @@
                    --test_timeout 3600 \
                    --test_tag_filters=-flaky \
                    --test_env DOCKER_HOST=$DOCKER_HOST"
-    bazel test $BAZEL_OPTS $TEST_TARGETS
+    bazelisk test $BAZEL_OPTS $TEST_TARGETS
 fi
 
 for JAR in $(find bazel-bin/plugins/{name} -name {name}*.jar)
diff --git a/jenkins/gerrit-bazel-build-plugin-manual.sh b/jenkins/gerrit-bazel-build-plugin-manual.sh
index 76eaa63..551a9c6 100644
--- a/jenkins/gerrit-bazel-build-plugin-manual.sh
+++ b/jenkins/gerrit-bazel-build-plugin-manual.sh
@@ -14,7 +14,8 @@
 
 . set-java.sh 8
 
-bazel build --spawn_strategy=standalone --genrule_strategy=standalone $TARGETS
+bazelisk version
+bazelisk build --spawn_strategy=standalone --genrule_strategy=standalone $TARGETS
 
 for JAR in $(find bazel-bin/plugins/{name} -name {name}*.jar)
 do
diff --git a/jenkins/gerrit-bazel-build-plugin.sh b/jenkins/gerrit-bazel-build-plugin.sh
index 9cb9bc7..860b5c3 100644
--- a/jenkins/gerrit-bazel-build-plugin.sh
+++ b/jenkins/gerrit-bazel-build-plugin.sh
@@ -14,11 +14,12 @@
 
 . set-java.sh 8
 
-bazel build --spawn_strategy=standalone --genrule_strategy=standalone $TARGETS
+bazelisk version
+bazelisk build --spawn_strategy=standalone --genrule_strategy=standalone $TARGETS
 
 if [ "$TEST_TARGET" != "" ]
 then
-    bazel test --test_env DOCKER_HOST=$DOCKER_HOST plugins/{name}:$TEST_TARGET
+    bazelisk test --test_env DOCKER_HOST=$DOCKER_HOST plugins/{name}:$TEST_TARGET
 fi
 
 for JAR in $(find bazel-bin/plugins/{name} -name {name}*.jar)
diff --git a/jenkins/gerrit-bazel-build.sh b/jenkins/gerrit-bazel-build.sh
index 7fc7ebb..380561c 100644
--- a/jenkins/gerrit-bazel-build.sh
+++ b/jenkins/gerrit-bazel-build.sh
@@ -5,5 +5,6 @@
 
 export BAZEL_OPTS="--spawn_strategy=standalone --genrule_strategy=standalone --java_toolchain //tools:error_prone_warnings_toolchain"
 
-bazel build $BAZEL_OPTS plugins:core release api
-tools/eclipse/project.py
+bazelisk version
+bazelisk build $BAZEL_OPTS plugins:core release api
+tools/eclipse/project.py --bazel bazelisk
diff --git a/jenkins/gerrit-bazel-test.sh b/jenkins/gerrit-bazel-test.sh
index 1cdb166..f500186 100755
--- a/jenkins/gerrit-bazel-test.sh
+++ b/jenkins/gerrit-bazel-test.sh
@@ -11,18 +11,20 @@
                    --test_tag_filters=-flaky,-docker \
                    --test_env DOCKER_HOST=$DOCKER_HOST"
 
+bazelisk version
+
 if [ "{branch}" == "stable-2.16" ] || [ "{branch}" == "stable-2.15" ] || [ "{branch}" == "stable-2.14" ]
 then
   echo 'Test in ReviewDb mode'
   echo '----------------------------------------------'
-  bazel test --test_env=GERRIT_NOTEDB=OFF $BAZEL_OPTS //...
+  bazelisk test --test_env=GERRIT_NOTEDB=OFF $BAZEL_OPTS //...
 fi
 
 if [ "{branch}" == "master" ] || [ "{branch}" == "stable-3.0" ] || [ "{branch}" == "stable-2.16" ] || [ "{branch}" == "stable-2.15" ]
 then
   echo 'Test in NoteDb mode'
   echo '----------------------------------------------'
-  bazel test --test_env=GERRIT_NOTEDB=ON $BAZEL_OPTS //...
+  bazelisk test --test_env=GERRIT_NOTEDB=ON $BAZEL_OPTS //...
 fi
 
 if [ "{branch}" == "master" ] || [ "{branch}" == "stable-3.0" ] || [ "{branch}" == "stable-2.16" ] || [ "{branch}" == "stable-2.15" ] || [ "{branch}" == "stable-2.14" ]
diff --git a/jenkins/gerrit-bazel-verifier-test.sh b/jenkins/gerrit-bazel-verifier-test.sh
index 96d3167..307e8ec 100755
--- a/jenkins/gerrit-bazel-verifier-test.sh
+++ b/jenkins/gerrit-bazel-verifier-test.sh
@@ -15,16 +15,18 @@
                  --test_tag_filters=-flaky,-docker \
                  --test_env DOCKER_HOST=$DOCKER_HOST"
 
+bazelisk version
+
 if [[ "$MODE" == *"reviewdb"* ]]
 then
   GERRIT_NOTEDB="--test_env=GERRIT_NOTEDB=OFF"
-  bazel test $BAZEL_OPTS //...
+  bazelisk test $BAZEL_OPTS //...
 fi
 
 if [[ "$MODE" == *"notedb"* ]]
 then
   GERRIT_NOTEDB="--test_env=GERRIT_NOTEDB=ON"
-  bazel test $GERRIT_NOTEDB $BAZEL_OPTS //...
+  bazelisk test $GERRIT_NOTEDB $BAZEL_OPTS //...
 fi
 
 if [[ "$TARGET_BRANCH" == "master" || "$TARGET_BRANCH" == "stable-3.0" || "$TARGET_BRANCH" == "stable-2.16" || "$TARGET_BRANCH" == "stable-2.15" || "$TARGET_BRANCH" == "stable-2.14" ]]
diff --git a/jenkins/gerrit-mvn-build-plugin.sh b/jenkins/gerrit-mvn-build-plugin.sh
index e5c717a..011ec07 100644
--- a/jenkins/gerrit-mvn-build-plugin.sh
+++ b/jenkins/gerrit-mvn-build-plugin.sh
@@ -7,7 +7,8 @@
   . set-java.sh 8
 
   pushd gerrit
-  bazel build api
+  bazelisk version
+  bazelisk build api
   ./tools/maven/api.sh install
   popd
 fi
diff --git a/jenkins/gerrit-sbt-build-branch-plugin.sh b/jenkins/gerrit-sbt-build-branch-plugin.sh
index 91e08a0..e735cdc 100644
--- a/jenkins/gerrit-sbt-build-branch-plugin.sh
+++ b/jenkins/gerrit-sbt-build-branch-plugin.sh
@@ -6,7 +6,8 @@
 if [ -f "gerrit/BUILD" ]
 then
   pushd gerrit
-  bazel build api
+  bazelisk version
+  bazelisk build api
   ./tools/maven/api.sh install
   popd
 fi
diff --git a/jenkins/gerrit-sbt-build-plugin.sh b/jenkins/gerrit-sbt-build-plugin.sh
index a62cda1..23ee2af 100644
--- a/jenkins/gerrit-sbt-build-plugin.sh
+++ b/jenkins/gerrit-sbt-build-plugin.sh
@@ -6,7 +6,8 @@
 if [ -f "gerrit/BUILD" ]
 then
   pushd gerrit
-  bazel build api
+  bazelisk version
+  bazelisk build api
   ./tools/maven/api.sh install
   popd
 fi
diff --git a/jenkins/gerrit-verifier.yaml b/jenkins/gerrit-verifier.yaml
index 8e2664a..5d672e8 100644
--- a/jenkins/gerrit-verifier.yaml
+++ b/jenkins/gerrit-verifier.yaml
@@ -172,12 +172,13 @@
       - shell: |
          #!/bin/bash -xe
          cd gerrit
+         bazelisk version
          if ([ "$TARGET_BRANCH" == "master" ] || [ "$TARGET_BRANCH" == "stable-3.0" ] || [ "$TARGET_BRANCH" == "stable-2.16" ] || [ "$TARGET_BRANCH" == "stable-2.15" ]) && git show --diff-filter=AM --name-only --pretty="" HEAD | grep polygerrit-ui
          then
            echo 'Running PolyGerrit lint check...'
            . set-java.sh 8
-           bazel test //polygerrit-ui/app:lint_test --test_output errors
-           bazel test //polygerrit-ui/app:polylint_test --test_output errors
+           bazelisk test //polygerrit-ui/app:lint_test --test_output errors
+           bazelisk test //polygerrit-ui/app:polylint_test --test_output errors
          fi
       - shell: |
          #!/bin/bash -xe
@@ -186,7 +187,7 @@
          then
            echo 'Running PolyGerrit template test...'
            . set-java.sh 8
-           bazel test //polygerrit-ui/app:all --test_tag_filters=template --test_output errors
+           bazelisk test //polygerrit-ui/app:all --test_tag_filters=template --test_output errors
          fi
       - shell: |
          #!/bin/bash -e
diff --git a/jenkins/gitiles-webapp.yaml b/jenkins/gitiles-webapp.yaml
index d9eb5b9..1649855 100644
--- a/jenkins/gitiles-webapp.yaml
+++ b/jenkins/gitiles-webapp.yaml
@@ -33,9 +33,10 @@
           set +x
           echo "Building gitiles webapp master"
           echo '=============================='
-          bazel clean --expunge
-          bazel build //...
-          bazel test //...
+          bazelisk clean --expunge
+          bazelisk version
+          bazelisk build //...
+          bazelisk test //...
     publishers:
       - archive:
          artifacts: 'bazel-bin/gitiles.war'