Read Buildifier version from presubmit config. (#542)

Users can now specify a particular version of Buildifier (or "latest")
in the presubmit configuration.
Merging this commit won't have any effect until I merge
https://github.com/bazelbuild/continuous-integration/pull/541/files
and create a new Docker image.

https://github.com/bazelbuild/continuous-integration/issues/530
diff --git a/buildkite/bazelci.py b/buildkite/bazelci.py
index 226c3ec..7d9bb97 100644
--- a/buildkite/bazelci.py
+++ b/buildkite/bazelci.py
@@ -400,6 +400,8 @@
 
 BUILD_LABEL_PATTERN = re.compile(r"^Build label: (\S+)$", re.MULTILINE)
 
+BUILDIFIER_VERSION_ENV_VAR = "BUILDIFIER_VERSION"
+
 
 class BuildkiteException(Exception):
     """
@@ -984,7 +986,9 @@
     print_collapsed_group("Setup (Run Targets)")
     # When using bazelisk --migrate to test incompatible flags,
     # incompatible flags set by "INCOMPATIBLE_FLAGS" env var will be ignored.
-    incompatible_flags_to_use = [] if (use_bazelisk_migrate() or not incompatible_flags) else incompatible_flags
+    incompatible_flags_to_use = (
+        [] if (use_bazelisk_migrate() or not incompatible_flags) else incompatible_flags
+    )
     for target in targets:
         try:
             execute_command(
@@ -1199,11 +1203,16 @@
         # incompatible flags set by "INCOMPATIBLE_FLAGS" env var will be ignored.
         [] if (use_bazelisk_migrate() or not incompatible_flags) else incompatible_flags,
         bep_file,
-        enable_remote_cache=True
+        enable_remote_cache=True,
     )
     try:
         execute_command(
-            [bazel_binary] + bazelisk_flags() + common_startup_flags(platform) + ["build"] + aggregated_flags + targets
+            [bazel_binary]
+            + bazelisk_flags()
+            + common_startup_flags(platform)
+            + ["build"]
+            + aggregated_flags
+            + targets
         )
     except subprocess.CalledProcessError as e:
         handle_bazel_failure(e, "build")
@@ -1236,12 +1245,17 @@
         # incompatible flags set by "INCOMPATIBLE_FLAGS" env var will be ignored.
         [] if (use_bazelisk_migrate() or not incompatible_flags) else incompatible_flags,
         bep_file,
-        enable_remote_cache=not monitor_flaky_tests
+        enable_remote_cache=not monitor_flaky_tests,
     )
 
     try:
         execute_command(
-            [bazel_binary] + bazelisk_flags() + common_startup_flags(platform) + ["test"] + aggregated_flags + targets
+            [bazel_binary]
+            + bazelisk_flags()
+            + common_startup_flags(platform)
+            + ["test"]
+            + aggregated_flags
+            + targets
         )
     except subprocess.CalledProcessError as e:
         handle_bazel_failure(e, "test")
@@ -1366,7 +1380,11 @@
         }
 
 
-def create_docker_step(label, image, commands=None):
+def create_docker_step(label, image, commands=None, additional_env_vars=None):
+    env = ["BUILDKITE_ARTIFACT_UPLOAD_DESTINATION", "BUILDKITE_GS_ACL"]
+    if additional_env_vars:
+        env += ["{}={}".format(k, v) for k, v in additional_env_vars.items()]
+
     step = {
         "label": label,
         "command": commands,
@@ -1375,7 +1393,7 @@
             "philwo/docker": {
                 "always-pull": True,
                 "debug": True,
-                "environment": ["BUILDKITE_ARTIFACT_UPLOAD_DESTINATION", "BUILDKITE_GS_ACL"],
+                "environment": env,
                 "image": image,
                 "network": "host",
                 "privileged": True,
@@ -1414,8 +1432,15 @@
     is_downstream_project = (use_but or incompatible_flags) and git_repository and project_name
 
     # Skip Buildifier when we test downstream projects.
-    if not is_downstream_project and configs.get("buildifier"):
-        pipeline_steps.append(create_docker_step("Buildifier", image=BUILDIFIER_DOCKER_IMAGE))
+    buildifier_version = configs.get("buildifier")
+    if not is_downstream_project and buildifier_version:
+        pipeline_steps.append(
+            create_docker_step(
+                "Buildifier",
+                image=BUILDIFIER_DOCKER_IMAGE,
+                additional_env_vars={BUILDIFIER_VERSION_ENV_VAR: buildifier_version},
+            )
+        )
 
     # In Bazel Downstream Project pipelines, we should test the project at the last green commit.
     git_commit = None