def upload_bazel_binaries()

in buildkite/bazelci.py [0:0]


def upload_bazel_binaries():
    """
    Uploads all Bazel binaries to a deterministic URL based on the current Git commit.

    Returns maps of platform names to sha256 hashes of the corresponding bazel and bazel_nojdk binaries.
    """
    bazel_hashes = {}
    bazel_nojdk_hashes = {}
    for platform_name, platform in PLATFORMS.items():
        if not should_publish_binaries_for_platform(platform_name):
            continue
        tmpdir = tempfile.mkdtemp()
        try:
            bazel_binary_path = download_bazel_binary(tmpdir, platform_name)
            # One platform that we build on can generate binaries for multiple platforms, e.g.
            # the centos7 platform generates binaries for the "centos7" platform, but also
            # for the generic "linux" platform.
            for target_platform_name in platform["publish_binary"]:
                execute_command(
                    [
                        gsutil_command(),
                        "cp",
                        bazel_binary_path,
                        bazelci_builds_gs_url(target_platform_name, os.environ["BUILDKITE_COMMIT"]),
                    ]
                )
                bazel_hashes[target_platform_name] = sha256_hexdigest(bazel_binary_path)

            # Also publish bazel_nojdk binaries.
            bazel_nojdk_binary_path = download_bazel_nojdk_binary(tmpdir, platform_name)
            for target_platform_name in platform["publish_binary"]:
                execute_command(
                    [
                        gsutil_command(),
                        "cp",
                        bazel_nojdk_binary_path,
                        bazelci_builds_nojdk_gs_url(
                            target_platform_name, os.environ["BUILDKITE_COMMIT"]
                        ),
                    ]
                )
                bazel_nojdk_hashes[target_platform_name] = sha256_hexdigest(bazel_nojdk_binary_path)
        except subprocess.CalledProcessError as e:
            # If we're not on the main branch, we're probably building a custom one-off binary and
            # ignore failures for individual platforms (it's possible that we didn't build binaries
            # for all platforms).
            if not current_branch_is_main_branch():
                eprint(
                    "Ignoring failure to download and publish Bazel binary for platform {}: {}".format(
                        platform_name, e
                    )
                )
            else:
                raise e
        finally:
            shutil.rmtree(tmpdir)
    return bazel_hashes, bazel_nojdk_hashes