如何在 BUILD.bazel 中获取 git commit SHA?

问题描述 投票:0回答:2

我的目标是使用 Bazel 创建一个 zip 文件,其名称中包含当前 HEAD 的 git commit SHA。
我知道有一种称为“冲压”的技术,但我很难理解如何使用它来满足我的需要。
我已经有一些代码(规则)来创建我需要的 zip 文件:

def go_binary_zip(name):
    go_binary(
        name = "{}_bin".format(name),
        embed = [":go_default_library"],
        goarch = "amd64",
        goos = "linux",
        visibility = ["//visibility:private"],
    )
    # How do I add the git commit SHA here?
    native.genrule(
        name = "{}_bin_archive".format(name),
        srcs = [":{}_bin".format(name)],
        outs = ["{}_bin.zip".format(name)],
        cmd = "$(location @bazel_tools//tools/zip:zipper/zipper) c $@ {}=$<".format(name),
        tools = ["@bazel_tools//tools/zip:zipper/zipper"],
        visibility = ["//visibility:public"],
    )

bazel
2个回答
2
投票

在 bazel 中不可能在宏或规则实现中获取 git commit SHA。输出路径旨在是确定性的。您可以在规则中可用的文件中获取该信息(请参阅下面的选项 2。)

如果您的目标是创建文件名中包含 git commit SHA 的 zip,您有几个选择。

  1. 编写一个在 bazel 外部运行的脚本,将 zip 从 bazel 复制到您的工作区。

    #!/bin/bash
    bazel build //:example
    cp $(bazel info bazel-bin)/example.zip example-$(git rev-parse HEAD).zip
    
  2. 创建一个使用标记生成脚本文件的规则,您可以通过

    bazel run
    运行该脚本文件来为您复制存档。在规则实现中,您可以使用
    stable-status.txt
    volatile-status.txt
    访问 ctx.info_file
    ctx.version_file
    文件(在此链接中记录:
    https://bazel.build/docs/user-manual#workspace-status
    )分别。

    请注意,

    ctx.info_file
    ctx.version_file
    未记录,因此尚不清楚此 API 是否稳定或将来是否会受到支持(请参阅 github 问题。)


0
投票

您可以使用 git 功能,该功能会替换

$Id$
文件中指定的文件中出现的任何
.gitattributes
https://stackoverflow.com/a/1792913/4638604.

© www.soinside.com 2019 - 2024. All rights reserved.