Run `bazel --version` and retry in case of failure (#2152)

Fixes https://github.com/bazelbuild/continuous-integration/issues/2123
diff --git a/buildkite/bazelci.py b/buildkite/bazelci.py
index 469e45c..c92c2c1 100755
--- a/buildkite/bazelci.py
+++ b/buildkite/bazelci.py
@@ -1196,6 +1196,9 @@
 
         # Dirty workaround for #1660
         if initial_setup:
+            # Run `bazel --version` to make sure Bazel is fetched.
+            run_bazel_version(bazel_binary)
+
             # Set OUTPUT_BASE environment variable
             os.environ["OUTPUT_BASE"] = get_output_base(bazel_binary)
 
@@ -1979,6 +1982,18 @@
     return flags
 
 
+def run_bazel_version(bazel_binary, max_retry=5):
+    """Run `bazel --version` to make sure Bazel is fetched by Bazelisk."""
+    # Retry to address https://github.com/bazelbuild/continuous-integration/issues/2123
+    retry = 0
+    while retry < max_retry:
+        exit_code = execute_command([bazel_binary, "--version"], fail_if_nonzero=False)
+        if exit_code == 0:
+            return
+        retry += 1
+        time.sleep(1)
+
+
 def get_output_base(bazel_binary):
     return execute_command_and_get_output(
         [bazel_binary] + common_startup_flags() + ["info", "output_base"],