我的目标是使用 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 中不可能在宏或规则实现中获取 git commit SHA。输出路径旨在是确定性的。您可以在规则中可用的文件中获取该信息(请参阅下面的选项 2。)
如果您的目标是创建文件名中包含 git commit SHA 的 zip,您有几个选择。
编写一个在 bazel 外部运行的脚本,将 zip 从 bazel 复制到您的工作区。
#!/bin/bash
bazel build //:example
cp $(bazel info bazel-bin)/example.zip example-$(git rev-parse HEAD).zip
创建一个使用标记生成脚本文件的规则,您可以通过
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 问题。)