Bazel: Activate RBE configuration on GCP

Create dedicated checker "rbe" to build and test on GCP platform on
master branch only. This should significantly reduce build and test
time for CI verification.

Given that Docker tests (Elasticsearch) and git wire protocol v2
(requires very recent git-core version) cannot run on RBE environment,
exclude those tests in RBE mode and include only those tests in NoteDb
mode.

Also exlude targets to publish to local maven repository and generate
.classpath file fo Eclipse IDE in RBE mode.  That's not an problem,
because those targets are still executed in NoteDb mode.

Javadoc generation doesn't work on RBE. Add a TODO-comment for it and
build the api-skip-javadoc target instead and consider to fix it later.

Bug: Issue 13753
Change-Id: Ie14a093c38f48e97d386749e9f5801bd516d104e
diff --git a/jenkins/checker_definitions.txt b/jenkins/checker_definitions.txt
index 37dd066..f2cbf79 100644
--- a/jenkins/checker_definitions.txt
+++ b/jenkins/checker_definitions.txt
@@ -1,6 +1,17 @@
 POST /plugins/checks/checkers/ HTTP/1.0
   Content-Type: application/json; charset=UTF-8
   {
+    "uuid": "gerritforge:rbe-a6a0e4682515f3521897c5f950d1394f4619d928",
+    "name": "RBE Build/Tests",
+    "description": "Builds the code base and executes unit/integration tests on RBE",
+    "repository": "gerrit",
+    "query": "branch:master",
+    "blocking": []
+  }
+
+POST /plugins/checks/checkers/ HTTP/1.0
+  Content-Type: application/json; charset=UTF-8
+  {
     "uuid": "gerritforge:notedb-a6a0e4682515f3521897c5f950d1394f4619d928",
     "name": "Build/Tests",
     "description": "Builds the code base and executes unit/integration tests",
diff --git a/jenkins/gerrit-bazel-build.sh b/jenkins/gerrit-bazel-build.sh
index 39049e4..fc0b7a2 100644
--- a/jenkins/gerrit-bazel-build.sh
+++ b/jenkins/gerrit-bazel-build.sh
@@ -4,6 +4,9 @@
 
 cd gerrit
 
+echo "Build with mode=$MODE"
+echo '----------------------------------------------'
+
 if git show --diff-filter=AM --name-only --pretty="" HEAD | grep -q .bazelversion
 then
   export BAZEL_OPTS=""
@@ -11,6 +14,14 @@
 
 java -fullversion
 bazelisk version
-bazelisk build $BAZEL_OPTS plugins:core release api
-tools/maven/api.sh install
-tools/eclipse/project.py --bazel bazelisk
+
+if [[ "$MODE" == *"rbe"* ]]
+then
+    # TODO(davido): Figure out why javadoc part of api-rule doesn't work on RBE.
+    # See: https://github.com/bazelbuild/bazel/issues/12765 for more background.
+  bazelisk build $BAZEL_RBE_OPTS plugins:core release api-skip-javadoc
+else
+  bazelisk build $BAZEL_OPTS plugins:core release api
+  tools/maven/api.sh install
+  tools/eclipse/project.py --bazel bazelisk
+fi
diff --git a/jenkins/gerrit-bazel-verifier-test.sh b/jenkins/gerrit-bazel-verifier-test.sh
index 8851b4d..bcce197 100755
--- a/jenkins/gerrit-bazel-verifier-test.sh
+++ b/jenkins/gerrit-bazel-verifier-test.sh
@@ -7,7 +7,14 @@
 echo "Test with mode=$MODE"
 echo '----------------------------------------------'
 
-case $TARGET_BRANCH in
+case $TARGET_BRANCH$MODE in
+  masterrbe)
+    TEST_TAG_FILTER="-flaky,-elastic,-git-protocol-v2"
+    BAZEL_OPTS="$BAZEL_RBE_OPTS"
+    ;;
+  masterNoteDb)
+    TEST_TAG_FILTER="-flaky,elastic,git-protocol-v2"
+    ;;
   stable-2.*)
     TEST_TAG_FILTER="-flaky,-elastic"
     ;;
@@ -30,7 +37,7 @@
 if [[ "$MODE" == *"reviewdb"* ]]
 then
   GERRIT_NOTEDB="--test_env=GERRIT_NOTEDB=OFF"
-  bazelisk test $BAZEL_OPTS //...
+  bazelisk test $GERRIT_NOTEDB $BAZEL_OPTS //...
 fi
 
 if [[ "$MODE" == *"notedb"* ]]
@@ -39,6 +46,11 @@
   bazelisk test $GERRIT_NOTEDB $BAZEL_OPTS //...
 fi
 
+if [[ "$MODE" == *"rbe"* ]]
+then
+  bazelisk test $BAZEL_OPTS //...
+fi
+
 if [[ "$MODE" == *"polygerrit"* ]]
 then
 
diff --git a/jenkins/gerrit-verifier.yaml b/jenkins/gerrit-verifier.yaml
index 150738c..4080e31 100644
--- a/jenkins/gerrit-verifier.yaml
+++ b/jenkins/gerrit-verifier.yaml
@@ -64,6 +64,7 @@
             - reviewdb
             - notedb
             - polygerrit
+            - rbe
           description: 'Type of verification to execute'
     properties:
       - authorization:
@@ -77,6 +78,8 @@
           properties-content: >
             BAZEL_OPTS=--remote_cache=https://gerrit-ci.gerritforge.com/cache
             --java_toolchain=//tools:error_prone_warnings_toolchain
+            BAZEL_RBE_OPTS=--config=remote
+            --remote_instance_name=projects/api-project-164060093628/instances/default_instance
     scm:
       - git:
           url: https://gerrit.googlesource.com/a/gerrit
diff --git a/vars/gerritPipeline.groovy b/vars/gerritPipeline.groovy
index 6f25cd5..b5711e8 100644
--- a/vars/gerritPipeline.groovy
+++ b/vars/gerritPipeline.groovy
@@ -130,7 +130,9 @@
 
 def collectBuildModes() {
     Builds.modes = []
-    if (env.GERRIT_BRANCH ==~ /stable-3.*/ || env.GERRIT_BRANCH == "master") {
+    if (env.GERRIT_BRANCH == "master") {
+        Builds.modes = ["notedb", "rbe"]
+    } else if (env.GERRIT_BRANCH ==~ /stable-3.*/) {
         Builds.modes = ["notedb"]
     } else if (env.GERRIT_BRANCH == "stable-2.16") {
         Builds.modes = ["notedb", "reviewdb"]