run_script: Set USE_RBE=false when disabling RBE

This change doesn't affect most builders but should help the one project
that reads this variable.

Bug: b/457783233, b/473816876
Change-Id: I44facc9d4aa04da0ff24cb0654e1092b8bd1306b
Reviewed-on: https://pigweed-review.googlesource.com/c/infra/recipes/+/374632
diff --git a/recipes/run_script.expected/run_script.json b/recipes/run_script.expected/run_script.json
index 534dd39..e162f84 100644
--- a/recipes/run_script.expected/run_script.json
+++ b/recipes/run_script.expected/run_script.json
@@ -7,13 +7,25 @@
       "--json-output",
       "/path/to/tmp/json",
       "copy",
-      "{\"bucket\": \"bucket\", \"builder\": \"builder\", \"id\": \"0\", \"number\": \"0\", \"project\": \"project\", \"tags\": []}",
+      "{\"bucket\": \"ci\", \"builder\": \"builder\", \"id\": \"8945511751514863184\", \"number\": \"0\", \"project\": \"project\", \"tags\": []}",
       "[START_DIR]/buildbucket-metadata.json"
     ],
     "infra_step": true,
+    "luci_context": {
+      "realm": {
+        "name": "project:ci"
+      },
+      "resultdb": {
+        "current_invocation": {
+          "name": "invocations/build:8945511751514863184",
+          "update_token": "token"
+        },
+        "hostname": "rdbhost"
+      }
+    },
     "name": "write bb metadata",
     "~followup_annotations": [
-      "@@@STEP_LOG_LINE@buildbucket-metadata.json@{\"bucket\": \"bucket\", \"builder\": \"builder\", \"id\": \"0\", \"number\": \"0\", \"project\": \"project\", \"tags\": []}@@@",
+      "@@@STEP_LOG_LINE@buildbucket-metadata.json@{\"bucket\": \"ci\", \"builder\": \"builder\", \"id\": \"8945511751514863184\", \"number\": \"0\", \"project\": \"project\", \"tags\": []}@@@",
       "@@@STEP_LOG_END@buildbucket-metadata.json@@@"
     ]
   },
@@ -25,9 +37,9 @@
     ],
     "cwd": "[START_DIR]/co",
     "env": {
-      "BUILDBUCKET_ID": "0",
+      "BUILDBUCKET_ID": "8945511751514863184",
       "BUILDBUCKET_METADATA_JSON": "[START_DIR]/buildbucket-metadata.json",
-      "BUILDBUCKET_NAME": "project:bucket:builder",
+      "BUILDBUCKET_NAME": "project:ci:builder",
       "BUILD_NUMBER": "0",
       "CAS_DIGEST_IN": "hashy-hashy-hashy",
       "CCACHE_DIR": "[CACHE]/ccache",
@@ -38,14 +50,27 @@
       "GOCACHE": "[CACHE]/go",
       "NO_COLOR": "1",
       "PIP_CACHE_DIR": "[CACHE]/pip",
-      "PROPERTIES_OUT": "[CLEANUP]/tmp_tmp_2/output_properties.json",
+      "PROPERTIES_OUT": "[CLEANUP]/tmp_tmp_1/output_properties.json",
       "PW_ENVIRONMENT_NO_ERROR_ON_UNRECOGNIZED": "1",
       "PW_ENVSETUP_DISABLE_SPINNER": "1",
       "PW_PRESUBMIT_DISABLE_SUBPROCESS_CAPTURE": "1",
       "PW_TEST_VAR": "test_value",
       "PW_USE_COLOR": "",
       "TEST_TMPDIR": "[CACHE]/bazel",
-      "TRIGGERING_CHANGES_JSON": "[CLEANUP]/tmp_tmp_1"
+      "TRIGGERING_CHANGES_JSON": null,
+      "USE_RBE": "false"
+    },
+    "luci_context": {
+      "realm": {
+        "name": "project:ci"
+      },
+      "resultdb": {
+        "current_invocation": {
+          "name": "invocations/build:8945511751514863184",
+          "update_token": "token"
+        },
+        "hostname": "rdbhost"
+      }
     },
     "name": "run foo/bar/run-tests.sh"
   },
@@ -57,13 +82,13 @@
       "--json-output",
       "/path/to/tmp/json",
       "copy",
-      "[CLEANUP]/tmp_tmp_2/output_properties.json",
+      "[CLEANUP]/tmp_tmp_1/output_properties.json",
       "/path/to/tmp/"
     ],
     "env": {
-      "BUILDBUCKET_ID": "0",
+      "BUILDBUCKET_ID": "8945511751514863184",
       "BUILDBUCKET_METADATA_JSON": "[START_DIR]/buildbucket-metadata.json",
-      "BUILDBUCKET_NAME": "project:bucket:builder",
+      "BUILDBUCKET_NAME": "project:ci:builder",
       "BUILD_NUMBER": "0",
       "CCACHE_DIR": "[CACHE]/ccache",
       "CLICOLOR": "0",
@@ -79,9 +104,21 @@
       "PW_TEST_VAR": "test_value",
       "PW_USE_COLOR": "",
       "TEST_TMPDIR": "[CACHE]/bazel",
-      "TRIGGERING_CHANGES_JSON": "[CLEANUP]/tmp_tmp_1"
+      "TRIGGERING_CHANGES_JSON": null
     },
     "infra_step": true,
+    "luci_context": {
+      "realm": {
+        "name": "project:ci"
+      },
+      "resultdb": {
+        "current_invocation": {
+          "name": "invocations/build:8945511751514863184",
+          "update_token": "token"
+        },
+        "hostname": "rdbhost"
+      }
+    },
     "name": "read output properties",
     "~followup_annotations": [
       "@@@STEP_LOG_LINE@output_properties.json@{@@@",
diff --git a/recipes/run_script.py b/recipes/run_script.py
index ecadff2..757b6df 100644
--- a/recipes/run_script.py
+++ b/recipes/run_script.py
@@ -16,7 +16,7 @@
 from __future__ import annotations
 
 import re
-from collections.abc import Iterator
+from collections.abc import Iterator, Mapping
 from typing import Any
 
 from recipe_engine import (
@@ -35,6 +35,7 @@
     'pigweed/environment',
     'pigweed/limited_cq',
     'pigweed/raw_result',
+    'recipe_engine/buildbucket',
     'recipe_engine/context',
     'recipe_engine/file',
     'recipe_engine/path',
@@ -47,16 +48,12 @@
 
 @raw_result_api.wrap_run_steps
 def RunSteps(api: recipe_api.RecipeScriptApi, props: InputProperties) -> None:
-    checkout: api.checkout.CheckoutContext = api.checkout(
-        props.checkout_options
-    )
+    checkout = api.checkout(props.checkout_options)
 
     if res := api.limited_cq.exit_early_if_not_approved(checkout.changes):
         return res  # pragma: no cover
 
-    env: api.environment.Environment = api.environment.init(
-        checkout, props.environment_options
-    )
+    env = api.environment.init(checkout, props.environment_options)
 
     cmd: list[str | config_types.Path] = [checkout.root / props.script]
 
@@ -72,11 +69,14 @@
 
         props_out = api.path.mkdtemp() / 'output_properties.json'
         api.path.mock_add_file(props_out)
-        env = {'PROPERTIES_OUT': props_out}
+        additional_env = {'PROPERTIES_OUT': props_out}
         if props.cas_digest_in:
-            env['CAS_DIGEST_IN'] = props.cas_digest_in
+            additional_env['CAS_DIGEST_IN'] = props.cas_digest_in
 
-        with api.context(cwd=checkout.root, env=env):
+        if 'pigweed.disable_rbe' in api.buildbucket.build.input.experiments:
+            additional_env['USE_RBE'] = 'false'
+
+        with api.context(cwd=checkout.root, env=additional_env):
             api.step(f'run {props.script}', cmd)
 
         if api.path.isfile(props_out):
@@ -94,14 +94,22 @@
 def GenTests(
     api: recipe_test_api.RecipeTestApi,
 ) -> Iterator[recipe_test_api.TestData]:
-    def properties(**kwargs: Any) -> recipe_test_api.TestData:
+    def properties(
+        *,
+        checkout_options: Mapping[str, Any] | None = None,
+        **kwargs: Any,
+    ) -> recipe_test_api.TestData:
         props = InputProperties(**kwargs)
-        props.checkout_options.CopyFrom(api.checkout.git_options())
+        props.checkout_options.CopyFrom(
+            api.checkout.git_options(**(checkout_options or {})),
+        )
         return api.properties(props)
 
     yield api.test(
         'run_script',
+        api.buildbucket.ci_build(experiments=['pigweed.disable_rbe']),
         properties(
+            checkout_options={'use_trigger': False},
             script='foo/bar/run-tests.sh',
             arguments='foo bar'.split(),
             cas_digest_in='hashy-hashy-hashy',