How to pass variables to Bazel target build?

左心房为你撑大大i 提交于 2020-06-27 18:11:04

问题


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)" }
)

I want to pass a variable to the target built so it can be replaced in $(VERSION). Is this possible?

I have tried with VERSION=1.0.0 bazel build :docker_image, but I get an error:

$(VERSION) not defined.

How can I pass that variable?

According docs:

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.


回答1:


Those variables can be set via the --define flag.

There is a section on the rules_docker page about stamping which covers this.

Essentially you can do something like: bazel build --define=VERSION=1.0.0 //:docker_image

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.

For setting defaults, you could use a .bazelrc file, with something like the following as the contents:

build --define=VERSION=0.0.0-PLACEHOLDER

The flags passed on the command line will take precedence over those in the .bazelrc file.

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.



来源:https://stackoverflow.com/questions/61045101/how-to-pass-variables-to-bazel-target-build

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!