如何将变量传递给Bazel目标构建? [英] How to pass variables to Bazel target build?
问题描述
我正在尝试使用以下代码构建Docker映像:
I am trying to build a Docker image with this code:
container_image(
name = "docker_image",
base = "@java_base//image",
files = [":executable_deploy.jar"],
cmd = ["java", "-jar", "executable_deploy.jar"],
env = { "VERSION" : "$(VERSION)" }
)
我想将变量传递给构建的目标,以便可以在$(VERSION)中将其替换.这可能吗?
I want to pass a variable to the target built so it can be replaced in $(VERSION). Is this possible?
我尝试使用VERSION=1.0.0 bazel build :docker_image
,但出现错误:
I have tried with VERSION=1.0.0 bazel build :docker_image
, but I get an error:
未定义$(VERSION).
$(VERSION) not defined.
如何传递该变量?
根据文档:
此字段(env)的值支持make变量(例如$(FOO))和 邮票变量;键支持也使变量. 但是我不明白那是什么意思.
The values of this field (env) support make variables (e.g., $(FOO)) and stamp variables; keys support make variables as well. But I don't understand exactly what that means.
推荐答案
Those variables can be set via the --define
flag.
rules_docker页面上有关于标记的部分,涵盖了这一点.
There is a section on the rules_docker page about stamping which covers this.
基本上,您可以执行以下操作:
bazel build --define=VERSION=1.0.0 //:docker_image
Essentially you can do something like:
bazel build --define=VERSION=1.0.0 //:docker_image
还可以从stable-status.txt
和volatile-status.txt
文件中获取这些键/值对. bazel的用户手册页显示了如何使用这些文件,并使用 --workspace_status_command
填充它们.
It is also possible to source these key / value pairs from the stable-status.txt
and volatile-status.txt
files. The user manual page for bazel shows how to use these files, and the use of the --workspace_status_command
to populate them.
要设置默认值,您可以使用 .bazelrc
文件,内容如下:
For setting defaults, you could use a .bazelrc
file, with something like the following as the contents:
build --define=VERSION=0.0.0-PLACEHOLDER
在命令行中传递的标志将优先于.bazelrc文件中的标志.
The flags passed on the command line will take precedence over those in the .bazelrc file.
值得一提的是,更改define
值将使bazel再次分析所有内容,这取决于图表,可能需要一些时间,但只会执行受影响的操作.
It's worth mentioning, that changing define
values will cause bazel to analyze everything again, which depending on the graph may take some time, but only affected actions will be executed.
这篇关于如何将变量传递给Bazel目标构建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!